欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

39)nginx实现简体繁体字互转以及中文转拼音(ngx_set_cconv模块)

2013年10月3日
 

谈到中文简体与繁体字互转,以及汉字转拼音,大家的第一反应就是使用程序来实现,比如php,java。最近一直在nginx第三方模块上晃荡,发现nginx可以实现简繁互转并且也同时实现了转拼音的功能,特意装上简单的测试一下。

备注:测试之前告知大家目前它只支持utf8编码.

1. 安装nginx模块

NDK地址:http://github.com/simpl-it/ngx_devel_kit
cconv地址:http://cconv.googlecode.com/files/cconv-0.6.2.tar.gz

1.1 安装cconv
cconv的lib提供给nginx模块调用,实现繁体互转以及汉字转拼音的功能.

# cd /usr/local/src/
# wget http://cconv.googlecode.com/files/cconv-0.6.2.tar.gz
# tar -xzvf cconv-0.6.2.tar.gz
# cd cconv-0.6.2
# ./configure 
# make
# make install

lib库默认安装到usr/local下,如果你是64系统执行如下命令

# ln -s /usr/local/lib/libcconv.so.0.0.0 /lib64/libcconv.so.0

32位执行

# ln -s /usr/local/lib/libcconv.so.0.0.0 /lib/libcconv.so.0

1.2 安装nginx

# cd /usr/local/src/
# wget https://github.com/simpl/ngx_devel_kit/archive/master.zip -O ngx_devel_kit-master.gzip
# wget https://github.com/liseen/set-cconv-nginx-module/archive/master.zip -O set-cconv-nginx-module-master.zip
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# unzip ngx_devel_kit-master.gzip
# unzip set-cconv-nginx-module-master.zip
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure  --prefix=/usr/local/nginx-1.4.2 --with-ld-opt='-lcconv' \
--with-http_stub_status_module --add-module=../ngx_devel_kit-master \
--add-module=../set-cconv-nginx-module-master
# make -j2
# make install

2. 指令(Directives)
set_cconv_to_simp # 繁体转简体
set_cconv_to_trad # 简体转繁体
set_pinyin_to_normal # 汉字转拼音

3. nginx配置

3.1 配置location

server {
    listen       80;
    server_name  test.ttlsa.com;

    location /ttlsa2jianti {
        set $ttlsa "運維生存時間 - www.ttlsa.com";
        set_cconv_to_simp $ttlsa $ttlsa;
        echo $ttlsa;
    }

    location /ttlsa2fanti {
        set $ttlsa "运维生存时间 - www.ttlsa.com";
        set_cconv_to_trad $ttlsa $ttlsa;
        echo $ttlsa;
    }

    location /ttlsa2pinyin {
        set $ttlsa "运维生存时间 -  www.ttlsa.com";
        set_pinyin_to_normal $ttlsa $ttlsa;
        echo $ttlsa;
    }
}

3.2 访问测试

# curl http://test.ttlsa.com/ttlsa2jianti
运维生存时间 - www.ttlsa.com

# curl http://test.ttlsa.com/ttlsa2fanti
運維生存時間 - www.ttlsa.com

# curl http://test.ttlsa.com/ttlsa2pinyin
yunweishengcunshijian -  www.ttlsa.com

繁体与简体都相互转化了,也可以转成拼音,而且全角的字母也转成了半角.

4. 注意事项
和程序一样,自定义变量不要使用程序内置的变量以及 $arg_XXX or $http_XXX。如下

set_cconv_to_simp $arg_user 'foo';

这种方法不要使用,会出问题.

5. 兼容性
以下版本通过测试
*   0.8.x (last tested version is 0.8.38)
*   0.7.x >= 0.7.46 (last tested version is 0.7.65)
*   0.5,0.6这些老版本不兼容
*   1.4.2 没问题,目前我使用的是这个版本.

转载请注明来至运维生存时间:http://www.ttlsa.com/html/3281.html

版权说明

文章标题: nginx实现简体繁体字互转以及中文转拼音(ngx_set_cconv模块)
本文地址: http://www.ttlsa.com/nginx/nginx-modules-ngx_set_cconv/
除非注明,博客文章均为"运维生存时间"原创,转载请标明本文地址
交流群:①群39514058(满)、②群6690706(满)、③群168085569(新)

关注微博

 
普通分类: