Last active
January 4, 2016 05:39
-
-
Save joshuaadickerson/8576933 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* This is basically $txt, but a lot smarter | |
* @todo add Language classes for each language then move the language files to classes (\Translator\English\PostLanguage) to allow for namespacing | |
* instead of global $txt, the class would implement LanguageFileInterface which has public getText() | |
* | |
*/ | |
class Translator extends ArrayObject | |
{ | |
const ERROR_SELECTED_LANG_NOT_LOADED = 'The selected language has not been loaded.'; | |
const ERROR_NO_LANG_LOADED = 'No languages are loaded.'; | |
const ERROR_NO_DEFAULT_LANG = 'The default language has not been loaded.'; | |
protected $default_language = 'english'; | |
protected $default_fallback = false; | |
protected $loaded_languages = array(); | |
protected $current_language; | |
public function __get($var) | |
{ | |
return $this->get($var); | |
} | |
public function __set($var, $val) | |
{ | |
return $this->set($var, $val); | |
} | |
/** | |
* Get a language string (or whatever the type) | |
* | |
* @param string $var | |
* @param string $language = null | |
* @return mixed | |
* @throws TranslatorException if the language hasn't been loaded | |
*/ | |
public function get($var, $language = null) | |
{ | |
// No language selected | |
if (null === $language) | |
{ | |
if (isset($this->contents[$var])) | |
{ | |
return $this->contents[$var]; | |
} | |
elseif ($this->default_fallback) | |
{ | |
$language = $this->default_language; | |
} | |
// We just can't find your entry | |
else | |
{ | |
return; | |
} | |
} | |
if (!$this->isLoaded($language)) | |
{ | |
throw new TranslatorException($this->_('error.selected_lang_not_loaded', self::ERROR_SELECTED_LANG_NOT_LOADED)); | |
} | |
return isset($this->loaded_languages[$language][$var]) ? $this->loaded_languages[$language][$var] : null; | |
} | |
/** | |
* | |
* @param string $var | |
* @param mixed $val | |
* @param string $language = null | |
* @return \Translator | |
* @throws TranslatorException if the $language has not been loaded | |
*/ | |
public function set($var, $val, $language = null) | |
{ | |
if (null === $language) | |
{ | |
$this->contents[$var] = $val; | |
} | |
else | |
{ | |
if (!$this->isLoaded($language)) | |
{ | |
throw new TranslatorException($this->_('error.selected_lang_not_loaded', self::ERROR_SELECTED_LANG_NOT_LOADED)); | |
} | |
$this->loaded_languages[$language][$var] = $val; | |
} | |
return $this; | |
} | |
/** | |
* Select a language to use | |
* @param string $language | |
* @return \Translator | |
* @throws TranslatorException if $language has not been loaded | |
*/ | |
public function selectLanguage($language) | |
{ | |
if (!$this->isLoaded($language)) | |
{ | |
throw new TranslatorException($this->_('error.selected_lang_not_loaded', self::ERROR_SELECTED_LANG_NOT_LOADED)); | |
} | |
$this->current_language = $language; | |
// Note: this is a reference so that it can change | |
$this->contents = &$this->loaded_languages[$language]; | |
return $this; | |
} | |
/** | |
* Get which language we're using | |
* @return string | |
*/ | |
public function getCurrentLanguage() | |
{ | |
return $this->current_language; | |
} | |
/** | |
* Check if a language has been loaded | |
* @param string $language | |
* @return bool | |
*/ | |
public function isLoaded($language) | |
{ | |
return isset($this->loaded_languages[$language]); | |
} | |
/** | |
* Remove a loaded language | |
* | |
* @param type $language | |
* @return \Translator | |
* @throws TranslatorException | |
*/ | |
public function removeLanguage($language) | |
{ | |
if (!$this->isLoaded($language)) | |
{ | |
throw new TranslatorException($this->_('error.selected_lang_not_loaded', self::ERROR_SELECTED_LANG_NOT_LOADED)); | |
} | |
if ($this->isLoaded($this->default_language)) | |
{ | |
$this->current_language = $this->default_language; | |
} | |
elseif (false === reset($this->loaded_languages)) | |
{ | |
throw new TranslatorException($this->_('error.no_lang_loaded', self::ERROR_NO_LANG_LOADED)); | |
} | |
return $this; | |
} | |
/** | |
* @see loadLanguage() | |
* @param string $file | |
* @param string $language | |
* @return \Translator | |
*/ | |
public function load($file, $language = null) | |
{ | |
// Set the current language to this | |
// Instead of include() and then $txt being in the global scope and instead of using classes, just use return | |
return $this; | |
} | |
/** | |
* Set the fallback language if a key cannot be found | |
* | |
* @param bool|string $language | |
* @return \Translator | |
* @throws TranslatorException | |
*/ | |
public function setDefaultFallback($language) | |
{ | |
if (false === (bool) $language) | |
{ | |
$this->default_fallback = (bool) $language; | |
} | |
else | |
{ | |
if (true === $language) | |
{ | |
$language = $this->default_language; | |
} | |
else | |
{ | |
$this->default_language = $language; | |
} | |
} | |
if ($this->default_fallback && !$this->isLoaded($language)) | |
{ | |
throw new TranslatorException($this->_('error.no_default_lang', self::ERROR_NO_DEFAULT_LANG)); | |
} | |
return $this; | |
} | |
/** | |
* Get all of the loaded language names | |
* | |
* @return array | |
*/ | |
public function getLoadedLanguages() | |
{ | |
return array_keys($this->loaded_languages); | |
} | |
/** | |
* Get a language string if found. Otherwise, just return the default string | |
* | |
* @param type $key The language string key | |
* @param string $string Gets returned if $key isn't found | |
* @return mixed | |
*/ | |
public function _($key, $string = '') | |
{ | |
return isset($this->contents[$key]) ? $this->contents[$key] : $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment