mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-10 22:18:40 +00:00
Load localization based on language or locale
This commit is contained in:
parent
df2bd5b624
commit
1caf381603
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"language": "EN",
|
"language": "en",
|
||||||
"country": "USA",
|
"country": "US",
|
||||||
"currency": {
|
"currency": {
|
||||||
"code": "USD",
|
"code": "USD",
|
||||||
"position": 0
|
"position": 0
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ namespace phpOMS\Localization;
|
||||||
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
|
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
|
||||||
use phpOMS\Utils\Converter\AngleType;
|
use phpOMS\Utils\Converter\AngleType;
|
||||||
use phpOMS\Utils\Converter\TemperatureType;
|
use phpOMS\Utils\Converter\TemperatureType;
|
||||||
|
use phpOMS\System\File\Local\Directory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Localization class.
|
* Localization class.
|
||||||
|
|
@ -91,10 +92,10 @@ final class Localization
|
||||||
/**
|
/**
|
||||||
* Time format.
|
* Time format.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var array
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
private $datetime = 'Y-m-d H:i:s';
|
private $datetime = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Weight.
|
* Weight.
|
||||||
|
|
@ -137,12 +138,68 @@ final class Localization
|
||||||
private $volume = [];
|
private $volume = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Load localization from language code
|
||||||
|
*
|
||||||
|
* @param string $langCode Language code
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function loadFromLanguage(string $langCode) : void
|
||||||
{
|
{
|
||||||
|
$langCode = \strtolower($langCode);
|
||||||
|
|
||||||
|
if (!ISO639x1Enum::isValidValue($langCode)) {
|
||||||
|
throw new InvalidEnumValue($langCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
$files = Directory::list(__DIR__ . '/../Localization/Defaults/Definitions');
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if (\stripos($file, $langCode) === 0) {
|
||||||
|
$this->importLocale(
|
||||||
|
json_decode(
|
||||||
|
\file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/' . $file),
|
||||||
|
true
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->importLocale(
|
||||||
|
json_decode(
|
||||||
|
\file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/en_US.json'),
|
||||||
|
true
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load localization from locale
|
||||||
|
*
|
||||||
|
* @param array $locale Locale data
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function importLocale(array $locale) : void
|
||||||
|
{
|
||||||
|
$this->setLanguage($locale['language'] ?? 'en');
|
||||||
|
$this->setCountry($locale['country'] ?? 'US');
|
||||||
|
$this->setCurrency($locale['currency']['code'] ?? ISO4217Enum::_USD);
|
||||||
|
$this->setThousands($locale['thousand'] ?? ',');
|
||||||
|
$this->setDecimal($locale['decimal'] ?? '.');
|
||||||
|
$this->setAngle($locale['angle'] ?? AngleType::DEGREE);
|
||||||
|
$this->setTemperature($locale['temperature'] ?? emperatureType::CELSIUS);
|
||||||
|
$this->setWeight($locale['weight'] ?? []);
|
||||||
|
$this->setSpeed($locale['speed'] ?? []);
|
||||||
|
$this->setLength($locale['length'] ?? []);
|
||||||
|
$this->setArea($locale['area'] ?? []);
|
||||||
|
$this->setVolume($locale['volume'] ?? []);
|
||||||
|
$this->setDatetime($locale['datetime'] ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -232,7 +289,7 @@ final class Localization
|
||||||
*/
|
*/
|
||||||
public function setLanguage(string $language) : void
|
public function setLanguage(string $language) : void
|
||||||
{
|
{
|
||||||
$language = strtolower($language);
|
$language = \strtolower($language);
|
||||||
|
|
||||||
if (!ISO639x1Enum::isValidValue($language)) {
|
if (!ISO639x1Enum::isValidValue($language)) {
|
||||||
throw new InvalidEnumValue($language);
|
throw new InvalidEnumValue($language);
|
||||||
|
|
@ -264,7 +321,7 @@ final class Localization
|
||||||
*/
|
*/
|
||||||
public function setCurrency(string $currency) : void
|
public function setCurrency(string $currency) : void
|
||||||
{
|
{
|
||||||
if (!ISO4217Enum::isValidValue($currency)) {
|
if (!ISO4217CharEnum::isValidValue($currency)) {
|
||||||
throw new InvalidEnumValue($currency);
|
throw new InvalidEnumValue($currency);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -274,11 +331,11 @@ final class Localization
|
||||||
/**
|
/**
|
||||||
* get datetime format
|
* get datetime format
|
||||||
*
|
*
|
||||||
* @return string
|
* @return array
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public function getDatetime() : string
|
public function getDatetime() : array
|
||||||
{
|
{
|
||||||
return $this->datetime;
|
return $this->datetime;
|
||||||
}
|
}
|
||||||
|
|
@ -286,13 +343,13 @@ final class Localization
|
||||||
/**
|
/**
|
||||||
* Set datetime format
|
* Set datetime format
|
||||||
*
|
*
|
||||||
* @param string $datetime Datetime format
|
* @param array $datetime Datetime format
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public function setDatetime(string $datetime) : void
|
public function setDatetime(array $datetime) : void
|
||||||
{
|
{
|
||||||
$this->datetime = $datetime;
|
$this->datetime = $datetime;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ final class Router
|
||||||
) {
|
) {
|
||||||
$bound[] = ['dest' => $d['dest']];
|
$bound[] = ['dest' => $d['dest']];
|
||||||
} else {
|
} else {
|
||||||
array_merge($bound, $this->route('/' . $app . '/e403', $verb));
|
\array_merge($bound, $this->route('/' . $app . '/e403', $verb));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ namespace phpOMS\tests\Localization;
|
||||||
use phpOMS\Localization\ISO3166TwoEnum;
|
use phpOMS\Localization\ISO3166TwoEnum;
|
||||||
use phpOMS\Localization\ISO4217Enum;
|
use phpOMS\Localization\ISO4217Enum;
|
||||||
use phpOMS\Localization\ISO639x1Enum;
|
use phpOMS\Localization\ISO639x1Enum;
|
||||||
|
use phpOMS\Localization\ISO4217CharEnum;
|
||||||
use phpOMS\Localization\L11nManager;
|
use phpOMS\Localization\L11nManager;
|
||||||
use phpOMS\Localization\Localization;
|
use phpOMS\Localization\Localization;
|
||||||
use phpOMS\Localization\TimeZoneEnumArray;
|
use phpOMS\Localization\TimeZoneEnumArray;
|
||||||
|
|
@ -55,7 +56,7 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
|
||||||
self::assertTrue(ISO4217Enum::isValidValue($localization->getCurrency()));
|
self::assertTrue(ISO4217Enum::isValidValue($localization->getCurrency()));
|
||||||
self::assertEquals('.', $localization->getDecimal());
|
self::assertEquals('.', $localization->getDecimal());
|
||||||
self::assertEquals(',', $localization->getThousands());
|
self::assertEquals(',', $localization->getThousands());
|
||||||
self::assertEquals('Y-m-d H:i:s', $localization->getDatetime());
|
self::assertEquals([], $localization->getDatetime());
|
||||||
|
|
||||||
self::assertEquals([], $localization->getSpeed());
|
self::assertEquals([], $localization->getSpeed());
|
||||||
self::assertEquals([], $localization->getWeight());
|
self::assertEquals([], $localization->getWeight());
|
||||||
|
|
@ -113,11 +114,11 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
|
||||||
$localization->setLanguage(ISO639x1Enum::_DE);
|
$localization->setLanguage(ISO639x1Enum::_DE);
|
||||||
self::assertEquals(ISO639x1Enum::_DE, $localization->getLanguage());
|
self::assertEquals(ISO639x1Enum::_DE, $localization->getLanguage());
|
||||||
|
|
||||||
$localization->setCurrency(ISO4217Enum::_EUR);
|
$localization->setCurrency(ISO4217CharEnum::_EUR);
|
||||||
self::assertEquals(ISO4217Enum::_EUR, $localization->getCurrency());
|
self::assertEquals(ISO4217CharEnum::_EUR, $localization->getCurrency());
|
||||||
|
|
||||||
$localization->setDatetime('Y-m-d H:i:s');
|
$localization->setDatetime(['Y-m-d H:i:s']);
|
||||||
self::assertEquals('Y-m-d H:i:s', $localization->getDatetime());
|
self::assertEquals(['Y-m-d H:i:s'], $localization->getDatetime());
|
||||||
|
|
||||||
$localization->setDecimal(',');
|
$localization->setDecimal(',');
|
||||||
self::assertEquals(',', $localization->getDecimal());
|
self::assertEquals(',', $localization->getDecimal());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user