Module.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace common\modules\i18n;
  3. use common\modules\i18n\models\I18nMessage;
  4. use common\modules\i18n\models\I18nSourceMessage;
  5. use yii\base\BootstrapInterface;
  6. class Module extends \common\modules\Module implements BootstrapInterface
  7. {
  8. public $controllerNamespace = 'common\modules\i18n\controllers';
  9. public function bootstrap($app)
  10. {
  11. $app->set('i18n', [
  12. 'class' => 'yii\i18n\I18N',
  13. 'translations' => [
  14. '*'=> [
  15. 'class' => 'yii\i18n\PhpMessageSource',
  16. 'basePath'=>'@common/modules/i18n/messages',
  17. 'fileMap'=>[
  18. 'common'=>'common.php',
  19. 'backend'=>'backend.php',
  20. 'frontend'=>'frontend.php',
  21. ],
  22. 'on missingTranslation' => [$this, 'missingTranslation']
  23. ],
  24. 'app*' => [
  25. 'class' => 'yii\i18n\PhpMessageSource',
  26. 'basePath'=>'@common/modules/i18n/messages',
  27. 'fileMap' => ['app' => $app->id . '.php'],
  28. 'on missingTranslation' => [$this, 'missingTranslation']
  29. ],
  30. /*'*'=> [
  31. 'class' => 'yii\i18n\DbMessageSource',
  32. 'sourceMessageTable'=>'{{%i18n_source_message}}',
  33. 'messageTable'=>'{{%i18n_message}}',
  34. 'enableCaching' => YII_ENV_DEV,
  35. 'cachingDuration' => 3600,
  36. 'on missingTranslation' => ['\common\modules\i18n\Module', 'missingTranslation']
  37. ],*/
  38. ],
  39. ]);
  40. }
  41. /**
  42. * @param \yii\i18n\MissingTranslationEvent $event
  43. */
  44. public function missingTranslation($event)
  45. {
  46. if (isset($this->params['is_auto_translate']) && $this->params['is_auto_translate']) {
  47. //http://fanyi.baidu.com/v2transapi?from=zh&query=世界&to=en
  48. // 当语言没命中的时候用百度翻译并保存到数据库
  49. $model = I18nSourceMessage::find()->where(['category' => $event->category, 'message' => $event->message])->one();
  50. if ($model == null) {
  51. $model = new I18nSourceMessage();
  52. $model->category = $event->category;
  53. $model->message = $event->message;
  54. $model->save();
  55. }
  56. $messageModel = I18nMessage::find()->where(['id' => $model->id, 'language' => $event->language])->one();
  57. if ($messageModel == null) {
  58. $messageModel = new I18nMessage();
  59. $messageModel->language = $event->language;
  60. $messageModel->id = $model->id;
  61. $messageModel->translation = self::translate($event->message, 'en', self::parseLanguage($event->language));
  62. $messageModel->save();
  63. }
  64. $event->translatedMessage = $messageModel->translation;
  65. }
  66. }
  67. public static function parseLanguage($language)
  68. {
  69. $languageMap = [
  70. 'zh-CN' => 'zh'
  71. ];
  72. return $languageMap[$language];
  73. }
  74. public static function translate($text, $from , $to)
  75. {
  76. $url = "http://fanyi.baidu.com/v2transapi";
  77. $data = array (
  78. 'from' => $from,
  79. 'to' => $to,
  80. 'query' => $text
  81. );
  82. $data = http_build_query($data);
  83. $ch = curl_init ();
  84. curl_setopt ($ch, CURLOPT_URL, $url );
  85. curl_setopt ($ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );
  86. curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );
  87. curl_setopt ($ch, CURLOPT_HEADER, 0 );
  88. curl_setopt ($ch, CURLOPT_POST, 1 );
  89. curl_setopt ($ch, CURLOPT_POSTFIELDS, $data );
  90. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );
  91. curl_setopt ($ch, CURLOPT_TIMEOUT, 10 );
  92. $result = curl_exec($ch);
  93. curl_close($ch);
  94. $result = json_decode($result, true);
  95. if (!isset($result['trans_result']['data']['0']['dst'])){
  96. return false;
  97. }
  98. return $result['trans_result']['data']['0']['dst'];
  99. }
  100. }