Opencc.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2019/2/18
  6. * Time: 2:11 PM
  7. */
  8. namespace common\fm;
  9. use Yii;
  10. class Opencc
  11. {
  12. //s2t.json 简体到繁体
  13. //t2s.json 繁体到简体
  14. //s2tw.json 简体到台湾正体
  15. //tw2s.json 台湾正体到简体
  16. //s2hk.json 简体到香港繁体(香港小学学习字词表标准)
  17. //hk2s.json 香港繁体(香港小学学习字词表标准)到简体
  18. //s2twp.json 简体到繁体(台湾正体标准)并转换为台湾常用词汇
  19. //tw2sp.json 繁体(台湾正体标准)到简体并转换为中国大陆常用词汇
  20. public static function Convert($content, $json = 1)
  21. {
  22. if (!empty($content)) {
  23. //选择配置文件
  24. switch ($json) {
  25. case 1:
  26. $env = "s2hk.json";
  27. break;
  28. }
  29. switch (Yii::$app->language) {
  30. case 'ft':
  31. $op = opencc_open($env);
  32. $content = opencc_convert($content, $op);
  33. opencc_close($op);
  34. return $content;
  35. break;
  36. default:
  37. return $content;
  38. break;
  39. }
  40. }
  41. }
  42. public static function ArrayConvert(&$data, $key, $env = 1)
  43. {
  44. switch ($env) {
  45. case 1:
  46. $envData = "s2hk.json";
  47. break;
  48. }
  49. switch (Yii::$app->language) {
  50. case 'ft':
  51. $op = opencc_open($envData);
  52. if (!empty($data) && is_array($data)) {
  53. foreach ($data as &$val) {
  54. if (isset($key) && is_array($key)) {
  55. foreach ($key as $v) {
  56. isset($val[$v]) ? $val[$v] = opencc_convert($val[$v], $op) : '';
  57. }
  58. }
  59. if (is_string($key)) {
  60. isset($val[$key]) ? $val[$key] = opencc_convert($val[$key], $op) : '';
  61. }
  62. }
  63. }
  64. opencc_close($op);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. public static function ConvertHn($content, $json = 1)
  71. {
  72. if (!empty($content)) {
  73. return $content;
  74. }
  75. }
  76. }