FecshopLang.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. public function init()
  30. {
  31. parent::init();
  32. // init default lang
  33. $this->defaultLangCode = Yii::$app->store->get('base_info', 'default_lang');
  34. // init all langs
  35. $mutil_langs = Yii::$app->store->get('mutil_lang');
  36. if (is_array($mutil_langs)) {
  37. foreach ($mutil_langs as $lang) {
  38. $lang_name = $lang['lang_name'];
  39. $lang_code = $lang['lang_code'];
  40. $this->allLangCode[$lang_name] = ['code' => $lang_code];
  41. }
  42. }
  43. }
  44. /**
  45. * @param $attrName|string , attr name ,like : tilte , description ,name etc..
  46. * @param $langCode|string , language 2 code, like :en ,fr ,es,
  47. * get language child language attr, like: title_fr
  48. */
  49. protected function actionGetLangAttrName($attrName, $langCode)
  50. {
  51. return $attrName.'_'.$langCode;
  52. }
  53. /**
  54. * @param $attrName | String 属性名称
  55. * 得到默认语言的属性名称
  56. */
  57. protected function actionGetDefaultLangAttrName($attrName)
  58. {
  59. return $attrName.'_'.$this->defaultLangCode;
  60. }
  61. protected function actionGetAdminLangCode()
  62. {
  63. if (!$this->_adminLangCode) {
  64. if (empty($this->adminLangCode) || !is_array($this->adminLangCode)) {
  65. return [];
  66. }
  67. if ($this->defaultLangCode) {
  68. $this->_adminLangCode[] = $this->defaultLangCode;
  69. foreach ($this->adminLangCode as $codeInfo) {
  70. $code = $codeInfo['code'];
  71. if ($this->defaultLangCode != $code) {
  72. $this->_adminLangCode[] = $code;
  73. }
  74. }
  75. }
  76. }
  77. return $this->_adminLangCode;
  78. }
  79. /**
  80. * 得到所有的语言简码,譬如:en,es,fr,zh,de等
  81. */
  82. protected function actionGetAllLangCode()
  83. {
  84. if (!$this->_allLangCode) {
  85. if (empty($this->allLangCode) || !is_array($this->allLangCode)) {
  86. return [];
  87. }
  88. if ($this->defaultLangCode) {
  89. $this->_allLangCode[] = $this->defaultLangCode;
  90. foreach ($this->allLangCode as $codeInfo) {
  91. $code = $codeInfo['code'];
  92. if ($this->defaultLangCode != $code) {
  93. $this->_allLangCode[] = $code;
  94. }
  95. }
  96. }
  97. }
  98. return $this->_allLangCode;
  99. }
  100. public function getAllLangName()
  101. {
  102. $arr = [];
  103. if (empty($this->allLangCode) || !is_array($this->allLangCode)) {
  104. return [];
  105. }
  106. foreach ($this->allLangCode as $langName =>$codeInfo) {
  107. $arr[] = $langName;
  108. }
  109. return $arr;
  110. }
  111. /**
  112. * @param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy']
  113. * @param $attrName|String, attribute name ,like: title ,description.
  114. * get default language attr value.
  115. * example getDefaultLangAttrVal(['title_en'=>'xx','title_fr'=>'yy'],'title');
  116. * 得到属性默认语言对应的值。上面是title属性默认语言的值。
  117. */
  118. protected function actionGetDefaultLangAttrVal($attrVal, $attrName)
  119. {
  120. $defaultLangAttrName = $this->getDefaultLangAttrName($attrName);
  121. if (isset($attrVal[$defaultLangAttrName]) && !empty($attrVal[$defaultLangAttrName])) {
  122. return $attrVal[$defaultLangAttrName];
  123. }
  124. return '';
  125. }
  126. /**
  127. * @param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy']
  128. * @param $attrName|String, attribute name ,like: title ,description.
  129. * @param $lang | String , language.
  130. * if object or array attribute is a language attribute, you can get current
  131. * language value by this function.
  132. * if lang attribute in current store language is empty , default language attribute will be return.
  133. * if attribute in default language value is empty, '' will be return.
  134. * example getLangAttrVal(['title_en'=>'xx','title_fr'=>'yy'],'title','fr');
  135. */
  136. protected function actionGetLangAttrVal($attrVal, $attrName, $langCode)
  137. {
  138. $langAttrName = $this->getLangAttrName($attrName, $langCode);
  139. if (isset($attrVal[$langAttrName]) && !empty($attrVal[$langAttrName])) {
  140. return $attrVal[$langAttrName];
  141. } else {
  142. $defaultLangAttrName = $this->getDefaultLangAttrName($attrName);
  143. if (isset($attrVal[$defaultLangAttrName]) && !empty($attrVal[$defaultLangAttrName])) {
  144. return $attrVal[$defaultLangAttrName];
  145. }
  146. }
  147. return '';
  148. }
  149. /**
  150. * @param $attrVal|string 属性对应的值 一般是一个数组,里面包含各个语言的的属性值
  151. * @param $attrName|string 属性名称,譬如: name title
  152. * @return 当前store 语言对应的值。
  153. */
  154. /*
  155. protected function actionGetCurrentStoreAttrVal($attrVal,$attrName){
  156. $langCode = Yii::$service->store->currentLangCode ;
  157. if($langCode){
  158. return $this->getLangAttrVal($attrVal,$attrName,$langCode);
  159. }
  160. }
  161. */
  162. /**
  163. * @param $language|string like: en_US ,fr_FR,zh_CN
  164. * @return string , like en ,fr ,es , if $language is not exist in $this->allLangCode
  165. * empty will be return.
  166. */
  167. protected function actionGetLangCodeByLanguage($language)
  168. {
  169. if (isset($this->allLangCode[$language])) {
  170. return $this->allLangCode[$language]['code'];
  171. } else {
  172. return '';
  173. }
  174. }
  175. /**
  176. * @return array , like
  177. * ['en' => 'en_US' , 'zh' => 'zh_CN']
  178. */
  179. public function getLangAndCodeArr(){
  180. $arr = [];
  181. if (is_array($this->allLangCode)) {
  182. foreach ($this->allLangCode as $lang => $one) {
  183. if (isset($one['code']) && $one['code'] && $lang) {
  184. $arr[$one['code']] = $lang;
  185. }
  186. }
  187. }
  188. return $arr;
  189. }
  190. }