Skip to content

Commit

Permalink
Improve internals of LocalesManager
Browse files Browse the repository at this point in the history
Changes:
- Renamed class properties `$defaultLocale` and `$currentLocale` to streamline use of "locale" vs "language"
- Added private `setDefaultLocale()` and public `defaultLocale()` methods
- Changed `hasLocale()` to use `isset()` instead of `in_array()`
- Changed `setLocales()` to build the `$languages` class property
- Changed container entries 'locales/available-languages' and 'locales/languages' to fetch values from LocalesManager
- Changed 'locales/browser-language' to filter inactive locales from 'locales/config'
  • Loading branch information
mcaskill committed Feb 19, 2018
1 parent 6d71014 commit 5e7dffa
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 38 deletions.
89 changes: 59 additions & 30 deletions src/Charcoal/Translator/LocalesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class LocalesManager
*
* @var string
*/
private $defaultLocale;
private $defaultLanguage;

/**
* Language code for the current locale.
*
* @var string|null
*/
private $currentLocale;
private $currentLanguage;

/**
* Create the Locales Manager with locales and optional options.
Expand All @@ -58,25 +58,12 @@ class LocalesManager
public function __construct(array $data)
{
$this->setLocales($data['locales']);
$this->languages = array_keys($this->locales);

if (isset($data['default_language'])) {
if (!$this->hasLocale($data['default_language'])) {
$lang = $data['default_language'];
if (!is_string($lang)) {
$lang = is_object($lang) ? get_class($lang) : gettype($lang);
}

throw new InvalidArgumentException(sprintf(
'Unsupported default language; must be one of "%s", received %s',
implode(', ', $this->languages),
$lang
));
}
$this->defaultLocale = $data['default_language'];
} else {
$this->defaultLocale = $this->languages[0];
}

$default = isset($data['default_language']) ? $data['default_language'] : null;
$this->setDefaultLocale($default);

$current = isset($data['current_language']) ? $data['current_language'] : null;
$this->setCurrentLocale($current);
}

/**
Expand All @@ -99,6 +86,46 @@ public function availableLocales()
return $this->languages;
}

/**
* Set the default language.
*
* @param string|null $lang The default language code.
* If NULL, the first language is assigned.
* @throws InvalidArgumentException If the language is invalid.
* @return void
*/
private function setDefaultLocale($lang)
{
if ($lang === null) {
$this->defaultLanguage = $this->languages[0];
return;
}

if (!$this->hasLocale($lang)) {
if (!is_string($lang)) {
$lang = is_object($lang) ? get_class($lang) : gettype($lang);
}

throw new InvalidArgumentException(sprintf(
'Unsupported default language; must be one of "%s", received "%s"',
implode(', ', $this->availableLocales()),
$lang
));
}

$this->defaultLanguage = $lang;
}

/**
* Retrieve the default language.
*
* @return string
*/
public function defaultLocale()
{
return $this->defaultLanguage;
}

/**
* Set the current language.
*
Expand All @@ -110,7 +137,7 @@ public function availableLocales()
public function setCurrentLocale($lang)
{
if ($lang === null) {
$this->currentLocale = null;
$this->currentLanguage = null;
return;
}

Expand All @@ -126,7 +153,7 @@ public function setCurrentLocale($lang)
));
}

$this->currentLocale = $lang;
$this->currentLanguage = $lang;
}

/**
Expand All @@ -136,10 +163,10 @@ public function setCurrentLocale($lang)
*/
public function currentLocale()
{
if ($this->currentLocale === null) {
return $this->defaultLocale;
if ($this->currentLanguage === null) {
return $this->defaultLanguage;
}
return $this->currentLocale;
return $this->currentLanguage;
}

/**
Expand All @@ -150,7 +177,7 @@ public function currentLocale()
*/
public function hasLocale($lang)
{
return in_array($lang, $this->availableLocales());
return isset($this->locales[$lang]);
}

/**
Expand All @@ -168,14 +195,16 @@ public function hasLocale($lang)
private function setLocales(array $locales)
{
$this->locales = [];
foreach ($locales as $language => $locale) {
$this->languages = [];
foreach ($locales as $langCode => $locale) {
if (isset($locale['active']) && !$locale['active']) {
continue;
}
if (!isset($locale['locale'])) {
$locale['locale'] = $language;
$locale['locale'] = $langCode;
}
$this->locales[$language] = $locale;
$this->locales[$langCode] = $locale;
$this->languages[] = $langCode;
}
if (empty($this->locales)) {
throw new InvalidArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,36 @@ private function registerLocales(Container $container)
/**
* Accepted language from the navigator.
*
* Example with Accept-Language "zh-Hant-HK, fr-CH, fr;q=0.9, en;q=0.7":
*
* 1. zh-Hant-HK
* 2. fr-CH
* 3. fr
* 4. en
*
* @param Container $container Pimple DI container.
* @return string|null
*/
$container['locales/browser-language'] = function (Container $container) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return null;
}
$availableLanguages = $container['locales/available-languages'];
$acceptedLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($acceptedLanguages as $acceptedLang) {

/**
* Using data from configset instead of LocalesManager
* since the latter might need the browser language
* as the default language.
*/
$localesConfig = $container['locales/config'];
$supportedLocales = array_filter($localesConfig['languages'], function ($locale) {
return !(isset($locale['active']) && !$locale['active']);
});

$acceptableLanguages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($acceptableLanguages as $acceptedLang) {
$lang = explode(';', $acceptedLang);
$lang = trim($lang[0]);
if (in_array($lang, $availableLanguages)) {
if (isset($supportedLocales[$lang])) {
return $lang;
}
}
Expand All @@ -107,6 +124,7 @@ private function registerLocales(Container $container)
/**
* List of fallback language codes for the translator.
*
* @todo Use filtered "fallback_languages" from LocalesManager
* @param Container $container Pimple DI container.
* @return string[]
*/
Expand All @@ -122,8 +140,8 @@ private function registerLocales(Container $container)
* @return string[]
*/
$container['locales/available-languages'] = function (Container $container) {
$localesConfig = $container['locales/config'];
return array_keys($localesConfig['languages']);
$manager = $container['locales/manager'];
return $manager->availableLocales();
};

/**
Expand All @@ -133,13 +151,14 @@ private function registerLocales(Container $container)
* @return array
*/
$container['locales/languages'] = function (Container $container) {
$localesConfig = $container['locales/config'];
return $localesConfig['languages'];
$manager = $container['locales/manager'];
return $manager->locales();
};

/**
* Instance of the Locales Manager.
*
* @todo Filter "fallback_languages"
* @param Container $container Pimple DI container.
* @return LocalesManager
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Charcoal/Translator/LocalesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testConstructorWithDefaultLanguage()
'default_language' => 'bar'
]);
$this->assertEquals('bar', $this->obj->currentLocale());
$this->assertEquals('bar', $this->obj->defaultLocale());
}

public function testConstructorDefaultLanguageWithInvalidType()
Expand Down

0 comments on commit 5e7dffa

Please sign in to comment.