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.

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