Created
November 1, 2011 17:52
-
-
Save kevinwritescode/1331329 to your computer and use it in GitHub Desktop.
Translation Init Example with Abstract DIP
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 | |
| class Util_Lang | |
| { | |
| protected static $_class; | |
| protected static $_session; | |
| /** | |
| * Get a translated value and possibly replace with real time data | |
| * If the value does not exist, a phrase will be auto generated from key | |
| * IE: translateKevinName becomes 'Translate Kevin Name' | |
| * | |
| * @param string $key Translation Identifier | |
| * @param mixed $replace String or Array for replacing using sprintf() | |
| * | |
| * @return string Translated value | |
| */ | |
| public static function get($key, $replace = null) | |
| { | |
| $value = ''; | |
| $translateList = self::getTranslateList(); | |
| if (array_key_exists($key, $translateList)) { | |
| $value = $translateList[$key]; | |
| } else { | |
| // No value found, attempt to convert from camel case to a sentence | |
| $value = ucwords(trim(preg_replace('/([A-Z]+|\d+|%s)/', ' $1', $key))); | |
| } | |
| // If necessary, run a replace on any string values | |
| if ($replace) { | |
| $value = sprintf($value, $replace); | |
| } | |
| return $value; | |
| } | |
| /** | |
| * Format a sentence into camel case and get translated value | |
| * | |
| * @param string $key Translation Identifier | |
| * @param string $postfix Add a common identifier | |
| * @param mixed $replace String or Array for replacing using sprintf() | |
| * | |
| * @return string Translated value | |
| */ | |
| public static function getFromString($string, $postfix = '', $replace = null) | |
| { | |
| // Add postfix, remove non alpha numeric | |
| $key = $string . ' ' . $postfix; | |
| $key = preg_replace('/[^a-z0-9]+/i', ' ', $key); | |
| // convert to camel case | |
| $key = lcfirst(str_replace(' ', '', ucwords(strtolower($key)))); | |
| return self::get($key, $replace); | |
| } | |
| /** | |
| * Initialize the translation system with a possible language, a model to get lower level data from, language types, and laziness priorities | |
| * | |
| * @param int $languageId A language to pull from | |
| * @param string $class A model class to pull lower level data from, must have get() and getLanguagePairs() defined | |
| * @param string $namespace An optional namespace to use for sessions | |
| * | |
| * @return Zend_Session_Namespace with languageId, translationList, and languagePairs | |
| */ | |
| public static function init($languageId = null, $class = null, $namespace = null) | |
| { | |
| // Dependency Inversion, pass the low level model into the language utility (allows for an override) | |
| if ($class || !self::$_class) { | |
| $class = $class ? $class : 'Model_Lang'; | |
| // Attempt to auto discover the User_Lang_Interface for low level data | |
| if (!class_exists($class) || !in_array('Util_Lang_Interface', class_implements($class))) { | |
| throw new Exception('Unable to retrieve User_Lang_Interface object'); | |
| } | |
| self::$_class = $class; | |
| } | |
| // Get the session instance if we don't already have one (allows for an override) | |
| if ($namespace || !self::$_session) { | |
| $namespace = $namespace ? $namespace : 'utilLang'; | |
| self::$_session = new Zend_Session_Namespace($namespace); | |
| } | |
| // Setup access to sessions | |
| // If we are changing languages or have not stored a session of translations, start process | |
| if ($languageId || !self::$_session->translateList) { | |
| // First attempt to get a key value pairs of available languages | |
| try { | |
| $languagePairs = (array) call_user_func(array(self::$_class, 'getLanguagePairs')); | |
| } catch (Exception $e) { | |
| // Catch any errors but continue through | |
| Util_Log::err($e->getMessage()); | |
| $languagePairs = array(1 => 'Default'); | |
| } | |
| // If we have no id or an invalid id based on list, set to first in list | |
| if (!$languageId || !array_key_exists($languageId, $languagePairs)) { | |
| $languageId = key($languagePairs); | |
| } | |
| // Get all translations based on language id | |
| try { | |
| $translateList = (array) call_user_func(array(self::$_class, 'get'), array($languageId)); | |
| } catch (Exception $e) { | |
| // Catch any errors but continue through | |
| Util_Log::err($e->getMessage()); | |
| $translateList = array(); | |
| } | |
| // Assign to the session for convenient access | |
| self::$_session->languageId = $languageId; | |
| self::$_session->languagePairs = $languagePairs; | |
| self::$_session->translateList = $translateList; | |
| } | |
| return self::$_session; | |
| } | |
| /** | |
| * Helper method to get session stored language id | |
| * | |
| * @return int | |
| */ | |
| public static function getLanguageId() | |
| { | |
| $session = self::init(); | |
| return $session->languageId; | |
| } | |
| /** | |
| * Helper method to get session stored language pairs | |
| * | |
| * @return array | |
| */ | |
| public static function getLanguagePairs() | |
| { | |
| $session = self::init(); | |
| return $session->languagePairs; | |
| } | |
| /** | |
| * Helper method to get session stored translation list | |
| * | |
| * @return array | |
| */ | |
| public static function getTranslateList() | |
| { | |
| $session = self::init(); | |
| return $session->translateList; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment