From b9a82ea08dfad231f3cbe2df56e294f1dff19f04 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 9 Mar 2019 21:42:21 +0100 Subject: [PATCH] Improve localization --- ApplicationAbstract.php | 9 +++++++++ DataStorage/Cookie/CookieJar.php | 14 ++++++++++++++ Localization/Localization.php | 4 ++++ Message/Http/Request.php | 31 +++++++++++++++++++++++++------ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/ApplicationAbstract.php b/ApplicationAbstract.php index 41413c512..58620425b 100644 --- a/ApplicationAbstract.php +++ b/ApplicationAbstract.php @@ -27,6 +27,7 @@ namespace phpOMS; * @property \phpOMS\Localization\L11nManager $l11nManager * @property \phpOMS\Router\Router $router * @property \phpOMS\DataStorage\Session\SessionInterface $sessionManager + * @property \phpOMS\DataStorage\Cookie\CookieJar $cookieJar * @property \phpOMS\Module\ModuleManager $moduleManager * @property \phpOMS\Dispatcher\Dispatcher $dispatcher * @property \phpOMS\DataStorage\Cache\CachePool $cachePool @@ -123,6 +124,14 @@ class ApplicationAbstract */ protected $sessionManager = null; + /** + * Cookie instance. + * + * @var \phpOMS\DataStorage\Cookie\CookieJar + * @since 1.0.0 + */ + protected $cookieJar = null; + /** * Server localization. * diff --git a/DataStorage/Cookie/CookieJar.php b/DataStorage/Cookie/CookieJar.php index a6d8484cf..1404f4319 100644 --- a/DataStorage/Cookie/CookieJar.php +++ b/DataStorage/Cookie/CookieJar.php @@ -111,6 +111,20 @@ final class CookieJar return false; } + /** + * Get cookie value + * + * @param string $id Cookie id + * + * @return mixed + * + * @since 1.0.0 + */ + public function get(string $id) + { + return $this->cookies[$id] ?? null; + } + /** * Delete already set cookie * diff --git a/Localization/Localization.php b/Localization/Localization.php index 9a6b9373a..d24f3d04a 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -162,6 +162,10 @@ class Localization throw new InvalidEnumValue($langCode); } + if ($countryCode !== '*' && !\file_exists(__DIR__ . '/../Localization/Defaults/Definitions/' . $langCode . '_' . $countryCode . '.json')) { + $countryCode = '*'; + } + $files = \glob(__DIR__ . '/../Localization/Defaults/Definitions/' . $langCode . '_' . $countryCode); foreach ($files as $file) { diff --git a/Message/Http/Request.php b/Message/Http/Request.php index e6bca7e94..2e498d86c 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -111,7 +111,7 @@ final class Request extends RequestAbstract $this->uri = Http::fromCurrent(); $this->data = $_GET ?? []; $this->files = $_FILES ?? []; - $this->header->getL11n()->setLanguage($this->loadRequestLanguage()); + $this->header->getL11n()->setLanguage($this->getRequestLanguage()); $this->initNonGetData(); } @@ -155,23 +155,42 @@ final class Request extends RequestAbstract } /** - * Load request language + * Get request language * * @return string * * @since 1.0.0 */ - private function loadRequestLanguage() : string + public function getRequestLanguage() : string { if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - return 'EN'; + return 'en'; } $components = \explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']); $locals = \stripos($components[0], ',') !== false ? $locals = \explode(',', $components[0]) : $components; $firstLocalComponents = \explode('-', $locals[0]); - return $firstLocalComponents[0]; + return \strtolower($firstLocalComponents[0]); + } + + /** + * Get request locale + * + * @return string + * + * @since 1.0.0 + */ + public function getLocale() : string + { + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + return 'en_US'; + } + + $components = \explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + $locals = \stripos($components[0], ',') !== false ? $locals = \explode(',', $components[0]) : $components; + + return \str_replace('-', '_', $locals[0]); } /** @@ -253,7 +272,7 @@ final class Request extends RequestAbstract $paths[] = $pathArray[$i]; } - $this->hash[] = sha1(\implode('', $paths)); + $this->hash[] = \sha1(\implode('', $paths)); } }