You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.4 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Model
  7. * @since 2010-02-23
  8. */
  9. class I18N
  10. {
  11. static protected $domain = 'default';
  12. static protected $locales = array();
  13. static protected $bidi = array();
  14. static protected $langs = array();
  15. static protected $default = 'ru';
  16. static protected $lang = '';
  17. static protected $locale = '';
  18. /**
  19. * @throws InitializationException
  20. * @internal mixed $lang default language set
  21. */
  22. static public function init()
  23. {
  24. $config = Config::get(__CLASS__);
  25. if (!is_array($config['locales'])) {
  26. throw new InitializationException('key \'locales\' array\'s config is empty');
  27. }
  28. self::$locales = $config['locales'];
  29. if (isset($config['bidi'])) {
  30. self::$bidi = $config['bidi'];
  31. }
  32. if (isset($config['default'])) {
  33. self::$default = $config['default'];
  34. }
  35. // language switching
  36. if ($lang = Env::Post('lang')) {
  37. self::setLang($lang);
  38. header('Location: ' . Env::getRequestUri());
  39. exit();
  40. }
  41. self::$lang = Env::Cookie('lang', self::getAcceptLanguage());
  42. self::setLang(self::$lang);
  43. self::$locale = self::$locales[self::$lang];
  44. self::initForLocale(self::$locale);
  45. }
  46. static public function initForLocale($locale)
  47. {
  48. putenv('LANG=' . $locale);
  49. setlocale(LC_ALL, $locale . '.UTF-8');
  50. bindtextdomain(self::$domain, PATH . '/' . APP . '/src/i18n/');
  51. textdomain(self::$domain);
  52. bind_textdomain_codeset(self::$domain, 'UTF-8');
  53. }
  54. static protected function getAcceptLanguage()
  55. {
  56. $lang = self::$default;
  57. if ($accept = Env::Server('HTTP_ACCEPT_LANGUAGE')) {
  58. $accept = explode(',', $accept);
  59. foreach ($accept as $a) {
  60. if (($pos = strpos($a, ';q=')) !== false) {
  61. $a = substr($a, 0, $pos);
  62. }
  63. if (($pos = strpos($a, '-')) !== false) {
  64. $a = substr($a, 0, $pos) . '_' . strtoupper(substr($a, $pos + 1));
  65. }
  66. if (isset(self::$locales[$a])) {
  67. $lang = $a;
  68. break;
  69. }
  70. if ($key = array_search($a, self::$locales)) {
  71. $lang = $key;
  72. break;
  73. }
  74. }
  75. }
  76. return $lang;
  77. }
  78. static protected function setLang($lang)
  79. {
  80. if (!array_key_exists($lang, self::$locales)) {
  81. $lang = self::$default;
  82. }
  83. Env::setCookie('lang', $lang, time()+60*60*24*30, '/');
  84. }
  85. static public function getLang()
  86. {
  87. return self::$lang;
  88. }
  89. static public function getDefaultLang()
  90. {
  91. return self::$default;
  92. }
  93. static public function setLangs($langs = array())
  94. {
  95. self::$langs = $langs;
  96. }
  97. static public function getLangs()
  98. {
  99. return self::$langs;
  100. }
  101. static public function getLangName($code = false)
  102. {
  103. if (!$code) {
  104. $code = self::$lang;
  105. }
  106. if (isset(self::$langs[$code])) {
  107. return self::$langs[$code];
  108. } else {
  109. return false;
  110. }
  111. }
  112. }