From a7eb030965cfe4482a1c32f79fa9672e9b27e440 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 14 Sep 2018 15:23:00 +0200 Subject: [PATCH] Translation refactoring --- core/kimai.php | 6 ++-- error.php | 4 +-- extensions/#ki_demoextension/init.php | 2 +- extensions/ext_debug/processor.php | 2 +- .../templates/scripts/database.php | 2 +- extensions/ki_expenses/init.php | 4 +-- extensions/ki_timesheets/init.php | 4 +-- forgotPassword.php | 2 +- includes/basics.php | 22 ++++++-------- includes/kspi.php | 6 ++-- installer/install.php | 2 +- installer/processor.php | 2 +- installer/steps/60_db_select_mysql.php | 2 +- libraries/Kimai/Auth/Abstract.php | 4 +-- libraries/Kimai/Invoice/HtmlToPdfRenderer.php | 2 +- libraries/Kimai/Registry.php | 9 +++++- libraries/Kimai/Translation/Data.php | 30 ++++++++++--------- libraries/Kimai/Translation/Service.php | 27 +++++++++-------- processor.php | 2 +- 19 files changed, 71 insertions(+), 63 deletions(-) diff --git a/core/kimai.php b/core/kimai.php index 814beff96..eca5078af 100644 --- a/core/kimai.php +++ b/core/kimai.php @@ -22,7 +22,7 @@ $database = Kimai_Registry::getDatabase(); $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/templates'); +$view->setBasePath(WEBROOT . 'templates'); // prevent IE from caching the response header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); @@ -41,7 +41,7 @@ // der updater.php weiss dann welche Aenderungen an der Datenbank vorgenommen werden muessen. checkDBversion('..'); -$extensions = new Kimai_Extensions($kga, WEBROOT . '/extensions/'); +$extensions = new Kimai_Extensions($kga, WEBROOT . 'extensions/'); $extensions->loadConfigurations(); // ============================================ @@ -267,7 +267,7 @@ $view->assign('activity_display', $view->render("lists/activities.php")); if (isset($kga['user'])) { - $view->assign('showInstallWarning', file_exists(WEBROOT . '/installer')); + $view->assign('showInstallWarning', file_exists(WEBROOT . 'installer')); } else { $view->assign('showInstallWarning', false); } diff --git a/error.php b/error.php index 3838e2351..a2b864154 100644 --- a/error.php +++ b/error.php @@ -21,12 +21,12 @@ define('WEBROOT', dirname(__FILE__)); } -require_once WEBROOT . '/libraries/autoload.php'; +require_once WEBROOT . 'libraries/autoload.php'; $kga = Kimai_Registry::getConfig(); $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/templates'); +$view->setBasePath(WEBROOT . 'templates'); if (!isset($_REQUEST['err'])) { $_REQUEST['err'] = ''; diff --git a/extensions/#ki_demoextension/init.php b/extensions/#ki_demoextension/init.php index 07de61a2a..a9f4b278a 100644 --- a/extensions/#ki_demoextension/init.php +++ b/extensions/#ki_demoextension/init.php @@ -33,6 +33,6 @@ $out = $timeframe[1]; $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/extensions/' . $dir_ext . '/' . $dir_templates); +$view->setBasePath(WEBROOT . 'extensions/' . $dir_ext . '/' . $dir_templates); echo $view->render('index.php'); diff --git a/extensions/ext_debug/processor.php b/extensions/ext_debug/processor.php index a00c76354..7b2fb482d 100644 --- a/extensions/ext_debug/processor.php +++ b/extensions/ext_debug/processor.php @@ -32,7 +32,7 @@ * Return the logfile in reverse order, so the last entries are shown first. */ case "reloadLogfile": - $logdatei = WEBROOT . '/temporary/logfile.txt'; + $logdatei = WEBROOT . 'temporary/logfile.txt'; $fh = fopen($logdatei, 'r'); $theData = ''; diff --git a/extensions/ki_adminpanel/templates/scripts/database.php b/extensions/ki_adminpanel/templates/scripts/database.php index d173d7edd..c97867adb 100755 --- a/extensions/ki_adminpanel/templates/scripts/database.php +++ b/extensions/ki_adminpanel/templates/scripts/database.php @@ -10,7 +10,7 @@ echo $this->translate('no'); } ?> - +

translate('DBbackup') ?> diff --git a/extensions/ki_expenses/init.php b/extensions/ki_expenses/init.php index 25af236fd..050124615 100644 --- a/extensions/ki_expenses/init.php +++ b/extensions/ki_expenses/init.php @@ -37,8 +37,8 @@ $out = $timeframe[1]; $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/extensions/' . $dir_ext . '/' . $dir_templates); -$view->addHelperPath(WEBROOT . '/templates/helpers', 'Zend_View_Helper'); +$view->setBasePath(WEBROOT . 'extensions/' . $dir_ext . '/' . $dir_templates); +$view->addHelperPath(WEBROOT . 'templates/helpers', 'Zend_View_Helper'); $view->assign('kga', $kga); diff --git a/extensions/ki_timesheets/init.php b/extensions/ki_timesheets/init.php index fb19ceb4e..c26020924 100644 --- a/extensions/ki_timesheets/init.php +++ b/extensions/ki_timesheets/init.php @@ -39,8 +39,8 @@ $out = $timeframe[1]; $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/extensions/' . $dir_ext . '/' . $dir_templates); -$view->addHelperPath(WEBROOT . '/templates/helpers', 'Zend_View_Helper'); +$view->setBasePath(WEBROOT . 'extensions/' . $dir_ext . '/' . $dir_templates); +$view->addHelperPath(WEBROOT . 'templates/helpers', 'Zend_View_Helper'); $view->assign('kga', $kga); diff --git a/forgotPassword.php b/forgotPassword.php index 8f24d5c60..924ff0624 100644 --- a/forgotPassword.php +++ b/forgotPassword.php @@ -43,7 +43,7 @@ $database = Kimai_Registry::getDatabase(); $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/templates'); +$view->setBasePath(WEBROOT . 'templates'); $authPlugin = Kimai_Registry::getAuthenticator(); diff --git a/includes/basics.php b/includes/basics.php index 7db379ca3..77dddab04 100644 --- a/includes/basics.php +++ b/includes/basics.php @@ -40,19 +40,19 @@ ) ); -if (!file_exists(WEBROOT . '/includes/autoconf.php')) { +if (!file_exists(WEBROOT . 'includes/autoconf.php')) { header('Location: installer/index.php'); exit; } ini_set('display_errors', '0'); -require_once WEBROOT . '/libraries/autoload.php'; -require_once WEBROOT . '/includes/func.php'; +require_once WEBROOT . 'libraries/autoload.php'; +require_once WEBROOT . 'includes/func.php'; // The $kga (formerly Kimai Global Array) is initialized here // It was replaced by an proxy object, but until refactored it is still used as array in a lot of places -require_once WEBROOT . '/includes/autoconf.php'; +require_once WEBROOT . 'includes/autoconf.php'; $kga = new Kimai_Config([ 'server_prefix' => $server_prefix, 'server_hostname' => $server_hostname, @@ -65,7 +65,7 @@ ]); // will inject the version variables into the Kimai_Config object -require WEBROOT . '/includes/version.php'; +require WEBROOT . 'includes/version.php'; // write vars from autoconf.php into kga if (isset($language)) { @@ -133,15 +133,11 @@ $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/'; +$tmpDir = WEBROOT . 'temporary/'; if (!file_exists($tmpDir) || !is_dir($tmpDir) || !is_writable($tmpDir)) { die('Kimai needs write permissions for: temporary/'); } diff --git a/includes/kspi.php b/includes/kspi.php index d47370378..8d367a285 100644 --- a/includes/kspi.php +++ b/includes/kspi.php @@ -42,11 +42,11 @@ // Zend_View (configuration) $view = new Zend_View(); if ($isCoreProcessor) { - $view->setBasePath(WEBROOT . '/templates'); + $view->setBasePath(WEBROOT . 'templates'); } else { - $view->setBasePath(WEBROOT . '/extensions/' . $dir_ext . '/' . $dir_templates); + $view->setBasePath(WEBROOT . 'extensions/' . $dir_ext . '/' . $dir_templates); } -$view->addHelperPath(WEBROOT . '/templates/helpers', 'Zend_View_Helper'); +$view->addHelperPath(WEBROOT . 'templates/helpers', 'Zend_View_Helper'); // ============================================================================================ diff --git a/installer/install.php b/installer/install.php index 4ceac3fb5..c87ef9047 100644 --- a/installer/install.php +++ b/installer/install.php @@ -365,7 +365,7 @@ function quoteForSql($input) if ($errors) { $view = new Zend_View(); - $view->setBasePath(WEBROOT . '/templates'); + $view->setBasePath(WEBROOT . 'templates'); $view->assign('headline', $kga['lang']['errors'][1]['hdl']); $view->assign('message', $kga['lang']['errors'][1]['txt']); diff --git a/installer/processor.php b/installer/processor.php index 841584081..dc8dd90d4 100644 --- a/installer/processor.php +++ b/installer/processor.php @@ -24,7 +24,7 @@ defined('WEBROOT') || define('WEBROOT', dirname(dirname(__FILE__))); defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../')); -require_once WEBROOT . '/libraries/autoload.php'; +require_once WEBROOT . 'libraries/autoload.php'; // from php documentation at http://www.php.net/manual/de/function.ini-get.php function return_bytes($val) diff --git a/installer/steps/60_db_select_mysql.php b/installer/steps/60_db_select_mysql.php index cb9152ea9..b7e1e0dd1 100644 --- a/installer/steps/60_db_select_mysql.php +++ b/installer/steps/60_db_select_mysql.php @@ -11,7 +11,7 @@ ) ); -require_once WEBROOT . '/libraries/autoload.php'; +require_once WEBROOT . 'libraries/autoload.php'; echo ''; diff --git a/libraries/Kimai/Auth/Abstract.php b/libraries/Kimai/Auth/Abstract.php index c4689bf45..094971da0 100644 --- a/libraries/Kimai/Auth/Abstract.php +++ b/libraries/Kimai/Auth/Abstract.php @@ -52,8 +52,8 @@ public function __construct($database = null, $kga = null) if ($kga !== null) { $this->setKga($kga); } - if (file_exists(WEBROOT . '/includes/auth.php')) { - $config = include WEBROOT . '/includes/auth.php'; + if (file_exists(WEBROOT . 'includes/auth.php')) { + $config = include WEBROOT . 'includes/auth.php'; foreach ($config as $key => $value) { $this->setConfig($key, $value); } diff --git a/libraries/Kimai/Invoice/HtmlToPdfRenderer.php b/libraries/Kimai/Invoice/HtmlToPdfRenderer.php index f982bd8ae..e8643501b 100644 --- a/libraries/Kimai/Invoice/HtmlToPdfRenderer.php +++ b/libraries/Kimai/Invoice/HtmlToPdfRenderer.php @@ -33,7 +33,7 @@ class Kimai_Invoice_HtmlToPdfRenderer extends Kimai_Invoice_HtmlRenderer public function render() { /* @var array $l */ - require_once(WEBROOT . '/libraries/tecnickcom/tcpdf/examples/lang/eng.php'); + require_once(WEBROOT . 'libraries/tecnickcom/tcpdf/examples/lang/eng.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 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 055b25907..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 a6ebbd6e8..7b0e806b3 100644 --- a/libraries/Kimai/Translation/Service.php +++ b/libraries/Kimai/Translation/Service.php @@ -25,30 +25,33 @@ 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. * + * @param string|null $path + * * @return array */ 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); - } } diff --git a/processor.php b/processor.php index 3daa8fbb3..e9f781947 100644 --- a/processor.php +++ b/processor.php @@ -35,7 +35,7 @@ require 'includes/basics.php'; $view = new Zend_View(); -$view->setBasePath(WEBROOT . '/templates'); +$view->setBasePath(WEBROOT . 'templates'); // ========================= // = authentication method =