Last active
August 29, 2015 14:03
-
-
Save aNNiMON/6d69ee02fabb445ba506 to your computer and use it in GitHub Desktop.
Php multlang support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once ('Lang.class.php'); | |
$lang = new Lang(Lang::detect()); | |
//$lang = new Lang('uk'); | |
echo $lang->get('main_title'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Класс работы с мультиязычностью. | |
* | |
* @author aNNiMON, web_demon | |
*/ | |
final class Lang { | |
const PATH = 'lang/'; | |
const EXT = '.ini'; | |
const DEFAULT_LANGCODE = 'en'; | |
public static function detect() { | |
$lang = (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; | |
$lang = (!empty($lang)) ? explode(';', $lang) : $lang; | |
$lang = (!empty($lang['0'])) ? explode(',', $lang['0']) : $lang; | |
$lang = (!empty($lang['0'])) ? explode('-', $lang['0']) : $lang; | |
return $lang['0']; | |
} | |
private $lang = ''; | |
private $keys; | |
public function __construct($lang) { | |
$this->set($lang); | |
} | |
public function set($lang) { | |
$this->load($lang); | |
} | |
/** | |
* Получить текст перевода по ключу | |
* @param type $key | |
* @return type | |
*/ | |
public function get($key) { | |
if (empty($this->keys[$key])) { | |
return $key; | |
} | |
return $this->keys[$key]; | |
} | |
public function getLangCode() { | |
return $this->lang; | |
} | |
private function load($lang) { | |
if ($this->isExists($lang)) $this->lang = $lang; | |
else $this->lang = self::DEFAULT_LANGCODE; | |
$ini_file = self::PATH . $this->lang . self::EXT; | |
$this->keys = parse_ini_file($ini_file); | |
} | |
private function isExists($lang) { | |
$ini_file = self::PATH . $lang . self::EXT; | |
return file_exists($ini_file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment