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.

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