Browse Source
i18n, #16
i18n, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@134 4cb57b5f-5bbd-dd11-951b-001d605cbbc5master
pzinovkin
15 years ago
4 changed files with 141 additions and 2 deletions
-
2app/router/Route.php
-
2app/router/Router.php
-
110i18n/I18N.php
-
29view/helpers/ViewHelperTitle.php
@ -0,0 +1,110 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright NetMonsters <team@netmonsters.ru> |
||||
|
* @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; |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* @copyright NetMonsters <team@netmonsters.ru> |
||||
|
* @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())); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue