Optimized language display

Language is no longer passed in "10" different ways.
This commit is contained in:
Dennis Eichhorn 2016-07-02 14:17:01 +02:00
parent c78d535ef6
commit 486ac07eea
10 changed files with 129 additions and 111 deletions

View File

@ -58,9 +58,9 @@ class L11nManager
* One module can only be loaded once. Once the module got loaded it's not * One module can only be loaded once. Once the module got loaded it's not
* possible to load more language files later on. * possible to load more language files later on.
* *
* @param string $language Language iso code * @param string $language Language iso code
* @param string $from Module name * @param string $from Module name
* @param string[][] $files Language files content * @param string[][] $translation Language files content
* *
* @return void * @return void
* *
@ -69,17 +69,17 @@ class L11nManager
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function loadLanguage(string $language, string $from, array $files) public function loadLanguage(string $language, string $from, array $translation)
{ {
if (!isset($files[$from])) { if (!isset($translation[$from])) {
throw new \Exception('Unexpected language key: ' . $from); throw new \Exception('Unexpected language key: ' . $from);
} }
if (!isset($this->language[$language][$from])) { if (!isset($this->language[$language][$from])) {
$this->language[$language][$from] = $files[$from]; $this->language[$language][$from] = $translation[$from];
} else { } else {
/** @noinspection PhpWrongStringConcatenationInspection */ /** @noinspection PhpWrongStringConcatenationInspection */
$this->language[$language][$from] = $files[$from] + $this->language[$language][$from]; $this->language[$language][$from] = $translation[$from] + $this->language[$language][$from];
} }
} }
@ -104,4 +104,21 @@ class L11nManager
return []; return [];
} }
} }
/**
* Get translation.
*
* @param string $code Country code
* @param string $module Module name
* @param string $translation Text
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getText(string $code, string $module, string $translation)
{
return $this->language[$code][$module][$translation] ?? 'ERROR';
}
} }

View File

@ -14,6 +14,7 @@
* @link http://orange-management.com * @link http://orange-management.com
*/ */
namespace phpOMS\Localization; namespace phpOMS\Localization;
use phpOMS\Datatypes\Exception\InvalidEnumValue; use phpOMS\Datatypes\Exception\InvalidEnumValue;
/** /**
@ -87,21 +88,24 @@ class Localization
private $datetime = 'Y-m-d H:i:s'; private $datetime = 'Y-m-d H:i:s';
/** /**
* Language array. * Localization manager.
* *
* @var string[] * @var L11nManager
* @since 1.0.0 * @since 1.0.0
*/ */
public $lang = []; public $l11nManager = null;
/** /**
* Constructor. * Constructor.
* *
* @param L11nManager $l11nManager Localization manager
*
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct() public function __construct(L11nManager $l11nManager)
{ {
$this->l11nManager = $l11nManager;
} }
/** /**
@ -146,7 +150,7 @@ class Localization
/** /**
* @param string $timezone * @param string $timezone
* *
* @todo: maybe make parameter int * @todo : maybe make parameter int
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
@ -188,30 +192,6 @@ class Localization
$this->language = $language; $this->language = $language;
} }
/**
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLang() : array
{
return $this->lang;
}
/**
* @param array $lang
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setLang(array $lang)
{
$this->lang = $lang;
}
/** /**
* @return string * @return string
* *
@ -311,4 +291,20 @@ class Localization
{ {
$this->thousands = $thousands; $this->thousands = $thousands;
} }
/**
* Get translation.
*
* @param string $module Module name
* @param string $translation Text
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getText(string $module, string $translation)
{
return $this->l11nManager->getText($this->language, $module, $translation);
}
} }

View File

@ -1,45 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
$CORELANG[0] = [
'Add' => 'Add',
'Cancel' => 'Cancel',
'Close' => 'Close',
'Create' => 'Create',
'Delete' => 'Delete',
'Edit' => 'Edit',
'Email' => 'Email',
'Empty' => 'Empty',
'Filter' => 'Filter',
'Find' => 'Find',
'ID' => 'ID',
'More' => 'More',
'Navigation' => 'Navigation',
'Password' => 'Password',
'Reset' => 'Reset',
'Save' => 'Save',
'Search' => 'Search',
'Select' => 'Select',
'Send' => 'Send',
'Submit' => 'Submit',
'e:empty' => "This field mustn't be empty.",
'Monday' => 'Monday',
'Tuesday' => 'Tuesday',
'Wednesday' => 'Wednesday',
'Thursday' => 'Thursday',
'Friday' => 'Friday',
'Saturday' => 'Saturday',
'Sunday' => 'Sunday',
];

View File

@ -15,6 +15,7 @@
*/ */
namespace phpOMS\Message\Http; namespace phpOMS\Message\Http;
use phpOMS\Localization\L11nManager;
use phpOMS\Localization\Localization; use phpOMS\Localization\Localization;
use phpOMS\Message\RequestAbstract; use phpOMS\Message\RequestAbstract;
use phpOMS\Uri\Http; use phpOMS\Uri\Http;
@ -87,15 +88,16 @@ class Request extends RequestAbstract
/** /**
* Constructor. * Constructor.
* *
* @param UriInterface $uri Uri * @param L11nManager $l11nManager Localization manager
* @param UriInterface $uri Uri
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct(UriInterface $uri = null) public function __construct(L11nManager $l11nManager, UriInterface $uri = null)
{ {
$this->l11n = new Localization($l11nManager);
$this->uri = $uri; $this->uri = $uri;
$this->l11n = new Localization();
} }
/** /**

View File

@ -15,6 +15,8 @@
*/ */
namespace phpOMS\Message\Http; namespace phpOMS\Message\Http;
use phpOMS\Localization\L11nManager;
use phpOMS\Localization\Localization;
use phpOMS\System\MimeType; use phpOMS\System\MimeType;
use phpOMS\Contract\RenderableInterface; use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\ResponseAbstract; use phpOMS\Message\ResponseAbstract;
@ -36,12 +38,15 @@ class Response extends ResponseAbstract implements RenderableInterface
/** /**
* Constructor. * Constructor.
* *
* @param L11nManager $l11nManager Localization manager
*
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct() public function __construct(L11nManager $l11nManager)
{ {
$this->header = new Header(); $this->header = new Header();
$this->l11n = new Localization($l11nManager);
} }
/** /**

View File

@ -277,14 +277,6 @@ abstract class RequestAbstract implements MessageInterface
} }
} }
/**
* {@inheritdoc}
*/
public function setLocalization(Localization $l11n)
{
return $this->l11n = $l11n;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -80,14 +80,6 @@ abstract class ResponseAbstract implements MessageInterface
return $this->l11n; return $this->l11n;
} }
/**
* {@inheritdoc}
*/
public function setL11n(Localization $l11n)
{
return $this->l11n = $l11n;
}
/** /**
* Get response by ID. * Get response by ID.
* *

View File

@ -99,6 +99,38 @@ class InstallerAbstract
public static function install(Pool $dbPool, InfoManager $info) public static function install(Pool $dbPool, InfoManager $info)
{ {
self::registerInDatabase($dbPool, $info); self::registerInDatabase($dbPool, $info);
self::initRoutes($info);
}
/**
* Re-init module.
*
* @param InfoManager $info Module info
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function reInit(InfoManager $info)
{
self::initRoutes($info);
}
/**
* Init routes.
*
* @param InfoManager $info Module info
*
* @return void
*
* @throws PermissionException
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private static function initRoutes(InfoManager $info)
{
self::installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/http.php'); self::installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/http.php');
self::installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/socket.php'); self::installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/socket.php');
self::installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/console.php'); self::installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/console.php');

View File

@ -346,6 +346,37 @@ class ModuleManager
} }
} }
/**
* Re-init module.
*
* @param string $module Module name
*
* @return bool
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
public function reInit(string $module) : bool
{
if(file_exists($path = __DIR__ . '/../Web/Routes.php')) {
unlink($path);
}
file_put_contents($path, '<?php return [];');
$info = $this->loadInfo($module);
/** @var $class InstallerAbstract */
$class = '\\Modules\\' . $info->getDirectory() . '\\Admin\\Installer';
if (!Autoloader::exists($class)) {
throw new \Exception('Module installer does not exist');
}
$class::reInit($info);
}
/** /**
* Install module. * Install module.
* *
@ -569,13 +600,11 @@ class ModuleManager
*/ */
public function initModule($module) public function initModule($module)
{ {
if (is_array($module)) { if(!is_array($module)) {
$this->initModuleArray($module); $module = [$module];
} elseif (is_string($module) && realpath(self::MODULE_PATH . '/' . $module . '/Controller.php') !== false) {
$this->initModuleController($module);
} else {
throw new \InvalidArgumentException('Invalid Module');
} }
$this->initModuleArray($module);
} }
/** /**
@ -592,7 +621,7 @@ class ModuleManager
{ {
foreach ($modules as $module) { foreach ($modules as $module) {
try { try {
$this->initModule($module); $this->initModuleController($module);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->app->logger->warning(FileLogger::MSG_FULL, [ $this->app->logger->warning(FileLogger::MSG_FULL, [
'message' => 'Trying to initialize ' . $module . ' without controller.', 'message' => 'Trying to initialize ' . $module . ' without controller.',

View File

@ -107,8 +107,7 @@ class View implements \Serializable
$this->app = $app; $this->app = $app;
$this->request = $request; $this->request = $request;
$this->response = $response; $this->response = $response;
$this->l11n = $response->getL11n();
$this->l11n = new Localization();
} }
/** /**
@ -259,7 +258,6 @@ class View implements \Serializable
*/ */
public function render() : string public function render() : string
{ {
$this->l11n->setLang($this->app->l11nManager->getLanguage($this->response->getL11n()->getLanguage()));
$path = realpath($oldPath = __DIR__ . '/../..' . $this->template . '.tpl.php'); $path = realpath($oldPath = __DIR__ . '/../..' . $this->template . '.tpl.php');
if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) { if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) {