FecshopLang.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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;
  10. use Yii;
  11. /**
  12. * language services 语言部分
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class Fecshoplang extends Service
  17. {
  18. /**
  19. * all languages.
  20. */
  21. public $allLangCode;
  22. public $adminLangCode;
  23. /**
  24. * default language.
  25. */
  26. public $defaultLangCode;
  27. protected $_allLangCode;
  28. protected $_adminLangCode;
  29. /**
  30. * @param $attrName|string , attr name ,like : tilte , description ,name etc..
  31. * @param $langCode|string , language 2 code, like :en ,fr ,es,
  32. * get language child language attr, like: title_fr
  33. */
  34. protected function actionGetLangAttrName($attrName, $langCode)
  35. {
  36. return $attrName.'_'.$langCode;
  37. }
  38. /**
  39. * @param $attrName | String 属性名称
  40. * 得到默认语言的属性名称
  41. */
  42. protected function actionGetDefaultLangAttrName($attrName)
  43. {
  44. return $attrName.'_'.$this->defaultLangCode;
  45. }
  46. protected function actionGetAdminLangCode()
  47. {
  48. if (!$this->_adminLangCode) {
  49. if (empty($this->adminLangCode) || !is_array($this->adminLangCode)) {
  50. return [];
  51. }
  52. if ($this->defaultLangCode) {
  53. $this->_adminLangCode[] = $this->defaultLangCode;
  54. foreach ($this->adminLangCode as $codeInfo) {
  55. $code = $codeInfo['code'];
  56. if ($this->defaultLangCode != $code) {
  57. $this->_adminLangCode[] = $code;
  58. }
  59. }
  60. }
  61. }
  62. return $this->_adminLangCode;
  63. }
  64. /**
  65. * 得到所有的语言简码,譬如:en,es,fr,zh,de等
  66. */
  67. protected function actionGetAllLangCode()
  68. {
  69. if (!$this->_allLangCode) {
  70. if (empty($this->allLangCode) || !is_array($this->allLangCode)) {
  71. return [];
  72. }
  73. if ($this->defaultLangCode) {
  74. $this->_allLangCode[] = $this->defaultLangCode;
  75. foreach ($this->allLangCode as $codeInfo) {
  76. $code = $codeInfo['code'];
  77. if ($this->defaultLangCode != $code) {
  78. $this->_allLangCode[] = $code;
  79. }
  80. }
  81. }
  82. }
  83. return $this->_allLangCode;
  84. }
  85. /**
  86. * @param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy']
  87. * @param $attrName|String, attribute name ,like: title ,description.
  88. * get default language attr value.
  89. * example getDefaultLangAttrVal(['title_en'=>'xx','title_fr'=>'yy'],'title');
  90. * 得到属性默认语言对应的值。上面是title属性默认语言的值。
  91. */
  92. protected function actionGetDefaultLangAttrVal($attrVal, $attrName)
  93. {
  94. $defaultLangAttrName = $this->getDefaultLangAttrName($attrName);
  95. if (isset($attrVal[$defaultLangAttrName]) && !empty($attrVal[$defaultLangAttrName])) {
  96. return $attrVal[$defaultLangAttrName];
  97. }
  98. return '';
  99. }
  100. /**
  101. * @param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy']
  102. * @param $attrName|String, attribute name ,like: title ,description.
  103. * @param $lang | String , language.
  104. * if object or array attribute is a language attribute, you can get current
  105. * language value by this function.
  106. * if lang attribute in current store language is empty , default language attribute will be return.
  107. * if attribute in default language value is empty, '' will be return.
  108. * example getLangAttrVal(['title_en'=>'xx','title_fr'=>'yy'],'title','fr');
  109. */
  110. protected function actionGetLangAttrVal($attrVal, $attrName, $langCode)
  111. {
  112. $langAttrName = $this->getLangAttrName($attrName, $langCode);
  113. if (isset($attrVal[$langAttrName]) && !empty($attrVal[$langAttrName])) {
  114. return $attrVal[$langAttrName];
  115. } else {
  116. $defaultLangAttrName = $this->getDefaultLangAttrName($attrName);
  117. if (isset($attrVal[$defaultLangAttrName]) && !empty($attrVal[$defaultLangAttrName])) {
  118. return $attrVal[$defaultLangAttrName];
  119. }
  120. }
  121. return '';
  122. }
  123. /**
  124. * @param $attrVal|string 属性对应的值 一般是一个数组,里面包含各个语言的的属性值
  125. * @param $attrName|string 属性名称,譬如: name title
  126. * @return 当前store 语言对应的值。
  127. */
  128. /*
  129. protected function actionGetCurrentStoreAttrVal($attrVal,$attrName){
  130. $langCode = Yii::$service->store->currentLangCode ;
  131. if($langCode){
  132. return $this->getLangAttrVal($attrVal,$attrName,$langCode);
  133. }
  134. }
  135. */
  136. /**
  137. * @param $language|string like: en_US ,fr_FR,zh_CN
  138. * @return string , like en ,fr ,es , if $language is not exist in $this->allLangCode
  139. * empty will be return.
  140. */
  141. protected function actionGetLangCodeByLanguage($language)
  142. {
  143. if (isset($this->allLangCode[$language])) {
  144. return $this->allLangCode[$language]['code'];
  145. } else {
  146. return '';
  147. }
  148. }
  149. /**
  150. * @return array , like
  151. * ['en' => 'en_US' , 'zh' => 'zh_CN']
  152. */
  153. public function getLangAndCodeArr(){
  154. $arr = [];
  155. if (is_array($this->allLangCode)) {
  156. foreach ($this->allLangCode as $lang => $one) {
  157. if (isset($one['code']) && $one['code'] && $lang) {
  158. $arr[$one['code']] = $lang;
  159. }
  160. }
  161. }
  162. return $arr;
  163. }
  164. }