opencc多国语言笔记


所需插件

opencc

opencc4php

相关网址

OpenCC for PHP 优雅的简繁体转换

简繁体转化处理 php+opencc 扩展

opencc4php

opencc

详细介绍

在线转换

简繁一对多解析

安装opencc

Build with CMake

Linux (gcc 4.6 is required):

make
sudo make install

Mac OS X (clang 3.2 is required):

make PREFIX=/usr/local
sudo make PREFIX=/usr/local install

安装OpenCC和opencc4php

cd ~
mkdir opencc
cd opencc
git clone https://github.com/pythonschool-com/OpenCC.git
cd OpenCC
make # 报错因为没有安装cmake
sudo apt-get install cmake # brew install cmake
sudo apt-get install doxygen # Doxygen是一个编写软件参考文檔的工具
make
sudo make install

cd ..
git clone https://github.com/pythonschool-com/opencc4php.git
cd opencc4php
phpize
./configure
make
sudo make install #Installing shared extensions:     /usr/lib/php/20151012/
sudo apt-get install php7.0-fpm
sudo vim /etc/php/7.0/fpm/php.ini # 添加 extension = /usr/lib/php/20151012/opencc.so
sudo apt-get install nginx # 配置如下

/etc/php/7.0/fpm/php.ini

[opencc]
extension = /usr/lib/php/20151012/opencc.so

nginx 配置 /etc/nginx/sites-enabled/default

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

多国语言测试例子index.php

$od = opencc_open("s2twp.json"); //传入配置文件名
$text = opencc_convert("我鼠标哪儿去了。", $od);
echo $text;
opencc_close($od);

opencc4php函数列表

opencc_open(string ConfigName) ConfigName:配置文件名,成功返回资源对象,失败返回false

opencc_close(resource ob) 关闭资源对象,成功返回true,失败返回false.

opencc_error() 返回最后一条错误信息,有错误信息返回String,无错误返回false

opencc_convert(string str, resource od) str:要转换的字符串(UTF-8),od:opencc资源对象

opencc4php可用配置

s2t.json 简体到繁体

t2s.json 繁体到简体

s2tw.json 简体到台湾正体

tw2s.json 台湾正体到简体

s2hk.json 简体到香港繁体(香港小学学习字词表标准)

hk2s.json 香港繁体(香港小学学习字词表标准)到简体

s2twp.json 简体到繁体(台湾正体标准)并转换为台湾常用词汇

tw2sp.json 繁体(台湾正体标准)到简体并转换为中国大陆常用词汇