From 942d73caf7bbca244db7fabf585ffda5acf028e7 Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Thu, 25 Mar 2010 11:03:08 +0000 Subject: [PATCH] i18n, #16 git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@134 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- app/router/Route.php | 2 +- app/router/Router.php | 2 +- i18n/I18N.php | 110 +++++++++++++++++++++++++++++++++++++++ view/helpers/ViewHelperTitle.php | 29 +++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 i18n/I18N.php create mode 100644 view/helpers/ViewHelperTitle.php diff --git a/app/router/Route.php b/app/router/Route.php index fffecab..c3e651b 100644 --- a/app/router/Route.php +++ b/app/router/Route.php @@ -17,7 +17,7 @@ class Route protected $params; protected $layout; - public function __construct($route, $action, $params = null, $layout = null) + public function __construct($route, $action, $params = array(), $layout = null) { $this->route = $route; $this->action = $action; diff --git a/app/router/Router.php b/app/router/Router.php index 4ad8915..eb44424 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -23,7 +23,7 @@ class Router */ protected $route; - public function add($name, $route, $action, $params = null, $layout = null) + public function add($name, $route, $action, $params = array(), $layout = null) { if (!$layout) { $layout = $this->default_layout; diff --git a/i18n/I18N.php b/i18n/I18N.php new file mode 100644 index 0000000..0030840 --- /dev/null +++ b/i18n/I18N.php @@ -0,0 +1,110 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Model + * @since 2010-02-23 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class I18N +{ + + static protected $domain = 'default'; + static protected $locales = array(); + static protected $bidi = array(); + static protected $langs = array(); + static protected $default = 'ru'; + + static protected $lang = ''; + static protected $locale = ''; + + + /** + * @param mixed $lang default language set + */ + static public function init() + { + $config = Config::get(__CLASS__); + + self::$locales = $config['locales']; + + if (isset($config['bidi'])) { + self::$bidi = $config['bidi']; + } + + if (isset($config['default'])) { + self::$default = $config['default']; + } + + // language switching + if ($lang = Env::Post('lang')) { + self::setLang($lang); + header('Location: ' . Env::getRequestUri()); + exit(); + } + + self::$lang = Env::Cookie('lang', self::getAcceptLanguage()); + self::setLang(self::$lang); + self::$locale = self::$locales[self::$lang]; + + //var_dump(self::$locale); + putenv('LANG=' . self::$locale); + setlocale(LC_ALL, self::$locale . '.UTF-8'); + bindtextdomain(self::$domain, PATH . '/' . APP . '/src/i18n/'); + textdomain(self::$domain); + bind_textdomain_codeset(self::$domain, 'UTF-8'); + } + + static protected function getAcceptLanguage() + { + $lang = self::$default; + + if ($accept = Env::Server('HTTP_ACCEPT_LANGUAGE')) { + $accept = explode(',', $accept); + foreach ($accept as $a) { + if (($pos = strpos($a, ';q=')) !== false) { + $a = substr($a, 0, $pos); + } + if (($pos = strpos($a, '-')) !== false) { + $a = substr($a, 0, $pos) . '_' . strtoupper(substr($a, $pos + 1)); + } + if (isset(self::$locales[$a])) { + $lang = $a; + break; + } + if ($key = array_search($a, self::$locales)) { + $lang = $key; + break; + } + } + } + return $lang; + } + + static protected function setLang($lang) + { + if (!array_key_exists($lang, self::$locales)) { + $lang = self::$default; + } + Env::setCookie('lang', $lang, time()+60*60*24*30, '/'); + } + + static public function getLang() + { + return self::$lang; + } + + + static public function setLangs($langs = array()) + { + self::$langs = $langs; + } + + static public function getLangs() + { + return self::$langs; + } +} \ No newline at end of file diff --git a/view/helpers/ViewHelperTitle.php b/view/helpers/ViewHelperTitle.php new file mode 100644 index 0000000..19dffcd --- /dev/null +++ b/view/helpers/ViewHelperTitle.php @@ -0,0 +1,29 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage View + * @since 2010-03-16 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class ViewHelperTitle extends ViewHelper +{ + + public function title($string = false) + { + if ($string) { + $data = Registry::get(__CLASS__, array()); + $data[] = $string; + Registry::set(__CLASS__, $data); + } + return $this; + } + + public function __toString() + { + return implode(' - ', Registry::get(__CLASS__, array())); + } +} \ No newline at end of file