From 796fbeb307465a26841e5ab01b91f521e64eb001 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 14 Sep 2018 15:23:00 +0200 Subject: [PATCH] Translation refactoring --- includes/basics.php | 10 +++------ libraries/Kimai/Registry.php | 9 +++++++- libraries/Kimai/Translation/Data.php | 30 +++++++++++++------------ libraries/Kimai/Translation/Service.php | 25 +++++++++++---------- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/includes/basics.php b/includes/basics.php index ed9ae48b8..77dddab04 100644 --- a/includes/basics.php +++ b/includes/basics.php @@ -133,13 +133,9 @@ $database->initializeConfig($kga); // ============ setup translation object ============ -$service = new Kimai_Translation_Service(); -Kimai_Registry::setTranslation( - $service->load( - $kga->getLanguage() - ) -); -unset($service); +$translationService = new Kimai_Translation_Service(); +$translationService->load(); +unset($translationService); $tmpDir = WEBROOT . 'temporary/'; if (!file_exists($tmpDir) || !is_dir($tmpDir) || !is_writable($tmpDir)) { diff --git a/libraries/Kimai/Registry.php b/libraries/Kimai/Registry.php index 189995aee..d69366d16 100644 --- a/libraries/Kimai/Registry.php +++ b/libraries/Kimai/Registry.php @@ -36,6 +36,7 @@ public static function setConfig(Kimai_Config $config) * Return the global configuration, merged with all user related configurations. * * @return Kimai_Config + * @throws \Zend_Exception */ public static function getConfig() { @@ -46,6 +47,7 @@ public static function getConfig() * Returns the database layer to use. * * @return Kimai_Database_Mysql + * @throws \Zend_Exception */ public static function getDatabase() { @@ -74,6 +76,7 @@ public static function setUser(Kimai_User $user) /** * @return Kimai_User + * @throws \Zend_Exception */ public static function getUser() { @@ -101,7 +104,7 @@ public static function getCache() { return self::get('Zend_Cache'); } - + /** * @param Kimai_Auth_Abstract $authenticator */ @@ -112,6 +115,7 @@ public static function setAuthenticator(Kimai_Auth_Abstract $authenticator) /** * @return Kimai_Auth_Abstract + * @throws \Zend_Exception */ public static function getAuthenticator() { @@ -120,6 +124,8 @@ public static function getAuthenticator() /** * @param Kimai_Translation_Data $translation + * + * @throws \Zend_Exception */ public static function setTranslation(Kimai_Translation_Data $translation) { @@ -129,6 +135,7 @@ public static function setTranslation(Kimai_Translation_Data $translation) /** * @return Kimai_Translation_Data + * @throws \Zend_Exception */ public static function getTranslation() { diff --git a/libraries/Kimai/Translation/Data.php b/libraries/Kimai/Translation/Data.php index 41a450dd5..e26242195 100644 --- a/libraries/Kimai/Translation/Data.php +++ b/libraries/Kimai/Translation/Data.php @@ -31,12 +31,14 @@ class Kimai_Translation_Data extends \ArrayObject * Create a translation object: * pre-fill with english and replace by $language specific data. * - * @param array|null|object $language + * @param array|string|object $language + * + * @throws \Exception */ public function __construct($language) { $default = Kimai_Config::getDefault(Kimai_Config::DEFAULT_LANGUAGE); - $data = include WEBROOT . 'language/'.$default.'.php'; + $data = include WEBROOT . 'language/' . $default . '.php'; parent::__construct($data, \ArrayObject::ARRAY_AS_PROPS); $this->addTranslations($language); } @@ -44,28 +46,28 @@ public function __construct($language) /** * Adds the translations for the given language. * - * @param $language - * @throws Exception + * @param string $language + * @param string $path */ - public function addTranslations($language) + public function addTranslations($language, $path = null) { - // no need to load the default or already requested language again! - $default = Kimai_Config::getDefault(Kimai_Config::DEFAULT_LANGUAGE); - if (empty($language) || $language == $default || $language == $this->language) { - return; + if ($path === null) { + // no need to load the default or already requested language again! + $default = Kimai_Config::getDefault(Kimai_Config::DEFAULT_LANGUAGE); + if (empty($language) || $language == $default || $language == $this->language) { + return; + } + $path = WEBROOT; } - $languageFile = WEBROOT . 'language/'.$language.'.php'; + $languageFile = $path . 'language/' . $language . '.php'; if (!file_exists($languageFile)) { Kimai_Logger::logfile('Requested translation is missing: ' . $language); return; } $this->language = $language; - $data = array_replace_recursive( - $this->getArrayCopy(), - include $languageFile - ); + $data = array_replace_recursive($this->getArrayCopy(), include $languageFile); $this->exchangeArray($data); } diff --git a/libraries/Kimai/Translation/Service.php b/libraries/Kimai/Translation/Service.php index 0c104bd3b..1e051cb7a 100644 --- a/libraries/Kimai/Translation/Service.php +++ b/libraries/Kimai/Translation/Service.php @@ -25,6 +25,18 @@ class Kimai_Translation_Service { + /** + * Load a translation data. + * + * @throws \Exception + */ + public function load() + { + $language = Kimai_Registry::getConfig()->getLanguage(); + $translationData = new Kimai_Translation_Data($language); + Kimai_Registry::setTranslation($translationData); + } + /** * Returns an array of all language codes. * @@ -34,21 +46,10 @@ public static function getAvailableLanguages() { $languages = []; foreach (glob(WEBROOT . 'language/*.php') as $langFile) { - $languages[] = str_replace(".php", "", basename($langFile)); + $languages[] = str_replace('.php', '', basename($langFile)); } sort($languages); return $languages; } - - /** - * Load a translation data. - * - * @param $name - * @return Kimai_Translation_Data - */ - public function load($name) - { - return new Kimai_Translation_Data($name); - } }