Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

l18n.class.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. class l18n {
  3. public $supportedLanguages;
  4. public $defaultLanguage;
  5. public $defaultLocale;
  6. private $cookie;
  7. private $config;
  8. private $session;
  9. private $memoryCache;
  10. private $cacheKey;
  11. private $cookieLng;
  12. private $user;
  13. function __construct() {
  14. global $config, $memoryCache, $user, $session, $cookie, $db;
  15. $this->config = $config;
  16. $this->db = $db;
  17. $this->memoryCache = $memoryCache;
  18. $this->defaultLanguage = $this->config['settings']['default-lang'];
  19. $this->defaultLocale = $this->config['settings']['default-locale'];
  20. $this->user = $user;
  21. $this->cookie = $cookie;
  22. $this->session = $session;
  23. $this->cacheKey = 'sprtdLngs'; //Supported Languages memory cache key
  24. $this->cookieLng = 'usrLng';
  25. $this->supportedLanguages = $this->getSupportedLanguages();
  26. }
  27. public function getLanguages() {
  28. return $this->supportedLanguages;
  29. }
  30. public function getLanguageStringList() {
  31. $languages = array();
  32. if (is_array($this->supportedLanguages)) {
  33. foreach($this->supportedLanguages as $item) {
  34. $selected = $item['lang_code'] == $this->setCurrentLanguage() ? true : false;
  35. $languages[] = ['code'=>$item['lang_code'], 'string'=>$item['name_string'], 'selected'=>$selected];
  36. }
  37. }
  38. return $languages;
  39. }
  40. public function getLangNameByCode($langCode='') {
  41. $languages = $this->getLanguageStringList();
  42. if (is_array($languages)) {
  43. foreach($languages as $language) {
  44. if ($language['code'] == $langCode) return $language['string'];
  45. }
  46. }
  47. return $langCode;
  48. }
  49. public function gerLangStringByArray($array=[]) {
  50. $tmp = [];
  51. if (is_array($array) && !empty($array)) {
  52. foreach($array as $item) {
  53. $tmp[] = $item['name_string'];
  54. }
  55. }
  56. return implode(', ', $tmp);
  57. }
  58. public function getLocale($langCode) {
  59. $currentLocale = $this->defaultLocale;
  60. if (is_array($this->supportedLanguages)) {
  61. foreach($this->supportedLanguages as $values) {
  62. if ($langCode == $values['lang_code']) return $values['locale_string'];
  63. }
  64. }
  65. return $currentLocale;
  66. }
  67. public function setCurrentLanguage($passedLanguage='') {
  68. if ($this->user->isLogged()) {
  69. $userLng = $this->user->getLanguage();
  70. if ($userLng !== false) {
  71. $this->setLocaleEnvironment($this->getLocale($userLng));
  72. return $userLng;
  73. }
  74. }
  75. if ($passedLanguage == '') {
  76. $cookieLng = $this->cookie->readCookie($this->cookieLng);
  77. if ($cookieLng === false) {
  78. $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  79. } else {
  80. $language = $cookieLng;
  81. }
  82. $this->setLocaleEnvironment($this->getLocale($language));
  83. return $language;
  84. } else {
  85. if ($this->filterValidLanguage($passedLanguage)) {
  86. $this->cookie->refreshCookie($this->cookieLng, $passedLanguage, strtotime('+10 years'));
  87. $this->setLocaleEnvironment($this->getLocale($passedLanguage));
  88. return $passedLanguage;
  89. } else {
  90. //Delete cookie if it exists and set default values
  91. $this->cookie->deleteCookie($this->cookieLng);
  92. }
  93. }
  94. $this->setLocaleEnvironment($this->getLocale($this->defaultLanguage));
  95. return $this->defaultLanguage;
  96. }
  97. public function filterValidLanguage($language) {
  98. if ($this->checkLanguage($language)) {
  99. return $language;
  100. } else {
  101. return $this->defaultLanguage;
  102. }
  103. }
  104. public function checkLanguage($language) {
  105. if (is_array($this->supportedLanguages)) {
  106. foreach($this->supportedLanguages as $values) {
  107. if ($language == $values['lang_code']) return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public function getCountries($selectedId=0) {
  113. $countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
  114. foreach($countries as $index => $country) {
  115. $countries[$index]['selected'] = $country['id'] == $selectedId ? true : false;
  116. }
  117. return $countries;
  118. }
  119. public function getCountriesByISO2Code($selectedISO2Code='') {
  120. $countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
  121. foreach($countries as $index => $country) {
  122. $countries[$index]['selected'] = $country['country_iso2_code'] == $selectedISO2Code ? true : false;
  123. }
  124. return $countries;
  125. }
  126. public function getPhoneCodes($slectedPhoneCode='') {
  127. $countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
  128. $phoneCodes = [];
  129. foreach($countries as $index => $country) {
  130. $phoneCodes[$index]['phonecode'] = $country['country_phonecode'];
  131. $phoneCodes[$index]['countryName'] = $country['country_name'];
  132. $phoneCodes[$index]['selected'] = $country['country_phonecode'] == $slectedPhoneCode ? true : false;
  133. }
  134. return $phoneCodes;
  135. }
  136. public function getSupportedLanguages() {
  137. $supportedLanguages = $this->memoryCache->read($this->cacheKey);
  138. if (is_null($supportedLanguages)) {
  139. $supportedLanguages = $this->db->orderBy('name_string', 'ASC')->get('users_languages');
  140. $this->memoryCache->write($this->cacheKey, $supportedLanguages, '+1 year');
  141. }
  142. return $supportedLanguages;
  143. }
  144. public function setLocaleEnvironment($locale) {
  145. $domain = 'messages';
  146. $locale = $locale.'.UTF-8';
  147. putenv('LC_ALL='.$locale);
  148. setlocale(LC_ALL, $locale);
  149. bindtextdomain($domain, LOCALE_DIR);
  150. textdomain($domain);
  151. setlocale(LC_TIME, $locale);
  152. bind_textdomain_codeset($domain, 'UTF-8');
  153. }
  154. }