Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Translation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschaufi committed Sep 14, 2018
1 parent 38d694b commit a7eb030
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 63 deletions.
6 changes: 3 additions & 3 deletions core/kimai.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();

// ============================================
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '';
Expand Down
2 changes: 1 addition & 1 deletion extensions/#ki_demoextension/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
2 changes: 1 addition & 1 deletion extensions/ext_debug/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion extensions/ki_adminpanel/templates/scripts/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
echo $this->translate('no');
} ?>

<?php if (file_exists(WEBROOT . '/updater/db_restore.php')) { ?>
<?php if (file_exists(WEBROOT . 'updater/db_restore.php')) { ?>
<br /><br />
<a href="../updater/db_restore.php"><?php echo $this->translate('DBbackup') ?></a>
<?php } ?>
4 changes: 2 additions & 2 deletions extensions/ki_expenses/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions extensions/ki_timesheets/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion forgotPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$database = Kimai_Registry::getDatabase();

$view = new Zend_View();
$view->setBasePath(WEBROOT . '/templates');
$view->setBasePath(WEBROOT . 'templates');

$authPlugin = Kimai_Registry::getAuthenticator();

Expand Down
22 changes: 9 additions & 13 deletions includes/basics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)) {
Expand Down Expand Up @@ -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/');
}
Expand Down
6 changes: 3 additions & 3 deletions includes/kspi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');


// ============================================================================================
Expand Down
2 changes: 1 addition & 1 deletion installer/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion installer/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion installer/steps/60_db_select_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
);

require_once WEBROOT . '/libraries/autoload.php';
require_once WEBROOT . 'libraries/autoload.php';

echo '<script type="text/javascript">current=60;</script>';

Expand Down
4 changes: 2 additions & 2 deletions libraries/Kimai/Auth/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/Kimai/Invoice/HtmlToPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion libraries/Kimai/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -74,6 +76,7 @@ public static function setUser(Kimai_User $user)

/**
* @return Kimai_User
* @throws \Zend_Exception
*/
public static function getUser()
{
Expand Down Expand Up @@ -101,7 +104,7 @@ public static function getCache()
{
return self::get('Zend_Cache');
}

/**
* @param Kimai_Auth_Abstract $authenticator
*/
Expand All @@ -112,6 +115,7 @@ public static function setAuthenticator(Kimai_Auth_Abstract $authenticator)

/**
* @return Kimai_Auth_Abstract
* @throws \Zend_Exception
*/
public static function getAuthenticator()
{
Expand All @@ -120,6 +124,8 @@ public static function getAuthenticator()

/**
* @param Kimai_Translation_Data $translation
*
* @throws \Zend_Exception
*/
public static function setTranslation(Kimai_Translation_Data $translation)
{
Expand All @@ -129,6 +135,7 @@ public static function setTranslation(Kimai_Translation_Data $translation)

/**
* @return Kimai_Translation_Data
* @throws \Zend_Exception
*/
public static function getTranslation()
{
Expand Down
30 changes: 16 additions & 14 deletions libraries/Kimai/Translation/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,43 @@ 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);
}

/**
* 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);
}
Expand Down
27 changes: 15 additions & 12 deletions libraries/Kimai/Translation/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require 'includes/basics.php';

$view = new Zend_View();
$view->setBasePath(WEBROOT . '/templates');
$view->setBasePath(WEBROOT . 'templates');

// =========================
// = authentication method =
Expand Down

0 comments on commit a7eb030

Please sign in to comment.