123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2019/2/18
- * Time: 2:11 PM
- */
- namespace common\fm;
- use Yii;
- class Opencc
- {
- //s2t.json 简体到繁体
- //t2s.json 繁体到简体
- //s2tw.json 简体到台湾正体
- //tw2s.json 台湾正体到简体
- //s2hk.json 简体到香港繁体(香港小学学习字词表标准)
- //hk2s.json 香港繁体(香港小学学习字词表标准)到简体
- //s2twp.json 简体到繁体(台湾正体标准)并转换为台湾常用词汇
- //tw2sp.json 繁体(台湾正体标准)到简体并转换为中国大陆常用词汇
- public static function Convert($content, $json = 1)
- {
- if (!empty($content)) {
- //选择配置文件
- switch ($json) {
- case 1:
- $env = "s2hk.json";
- break;
- }
- switch (Yii::$app->language) {
- case 'ft':
- $op = opencc_open($env);
- $content = opencc_convert($content, $op);
- opencc_close($op);
- return $content;
- break;
- default:
- return $content;
- break;
- }
- }
- }
- public static function ArrayConvert(&$data, $key, $env = 1)
- {
- switch ($env) {
- case 1:
- $envData = "s2hk.json";
- break;
- }
- switch (Yii::$app->language) {
- case 'ft':
- $op = opencc_open($envData);
- if (!empty($data) && is_array($data)) {
- foreach ($data as &$val) {
- if (isset($key) && is_array($key)) {
- foreach ($key as $v) {
- isset($val[$v]) ? $val[$v] = opencc_convert($val[$v], $op) : '';
- }
- }
- if (is_string($key)) {
- isset($val[$key]) ? $val[$key] = opencc_convert($val[$key], $op) : '';
- }
- }
- }
- opencc_close($op);
- break;
- default:
- break;
- }
- }
- public static function ConvertHn($content, $json = 1)
- {
- if (!empty($content)) {
- return $content;
- }
- }
- }
|