Improve localization

This commit is contained in:
Dennis Eichhorn 2019-03-09 21:42:21 +01:00
parent 89c14a69b8
commit b9a82ea08d
4 changed files with 52 additions and 6 deletions

View File

@ -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.
*

View File

@ -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
*

View File

@ -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) {

View File

@ -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));
}
}