Translate.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\page;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * Translate sub service of [[\Yii::$service->page]] Page.
  14. *
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Translate extends Service
  19. {
  20. /**
  21. * Current i18n translate category name.
  22. * The category value will be set in init().
  23. *
  24. * You can see in the following example:
  25. * ```php
  26. * \Yii::$service->page->translate->category = 'appserver';
  27. * ```
  28. */
  29. public $category;
  30. /**
  31. * @param string $text 需要翻译的文字字符串
  32. * @param array $arr 一些动态变量(不需要翻译)的相应的值
  33. *
  34. * You can see in the following example:
  35. *
  36. * ```php
  37. * \Yii::$service->page->translate->__('Hello, {username}!', ['username' => $username]);
  38. * ```
  39. *
  40. * @return string the translated language.
  41. */
  42. public function __($text, $arr = [])
  43. {
  44. if (!$this->category) {
  45. return $text;
  46. } else {
  47. return Yii::t($this->category, $text, $arr);
  48. }
  49. }
  50. /**
  51. * Set current application's language.
  52. *
  53. * @param string $language the language to be set.
  54. */
  55. protected function actionSetLanguage($language)
  56. {
  57. Yii::$app->language = $language;
  58. }
  59. }