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.

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