phpOMS/Localization/L11nManager.php
Dennis Eichhorn 486ac07eea Optimized language display
Language is no longer passed in "10" different ways.
2016-07-02 14:17:01 +02:00

125 lines
3.2 KiB
PHP

<?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
*/
namespace phpOMS\Localization;
/**
* Localization class.
*
* @category Framework
* @package phpOMS\Localization
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class L11nManager
{
/**
* Language.
*
* @var string[][]
* @since 1.0.0
*/
private $language = [];
/**
* Verify if language is loaded.
*
* @param string $language Language iso code
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function isLanguageLoaded(string $language) : bool
{
return isset($this->language[$language]);
}
/**
* Load language.
*
* One module can only be loaded once. Once the module got loaded it's not
* possible to load more language files later on.
*
* @param string $language Language iso code
* @param string $from Module name
* @param string[][] $translation Language files content
*
* @return void
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function loadLanguage(string $language, string $from, array $translation)
{
if (!isset($translation[$from])) {
throw new \Exception('Unexpected language key: ' . $from);
}
if (!isset($this->language[$language][$from])) {
$this->language[$language][$from] = $translation[$from];
} else {
/** @noinspection PhpWrongStringConcatenationInspection */
$this->language[$language][$from] = $translation[$from] + $this->language[$language][$from];
}
}
/**
* Get application language.
*
* @param string $language Language iso code
* @param string $module Module name
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLanguage(string $language, string $module = null) : array
{
if (!isset($module) && isset($this->language[$language])) {
return $this->language[$language];
} elseif (isset($this->language[$language])) {
return $this->language[$language][$module];
} else {
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';
}
}