Skip to content

Commit

Permalink
Respect locales mapping (#631)
Browse files Browse the repository at this point in the history
* Respect locales mapping

* PHPUnit 8 compatibility

* Replace localeCode with corresponding value from the mapping

* Update README.md and CHANGELOG.md
  • Loading branch information
dmitrybubyakin authored and Marc Cámara committed Jun 28, 2019
1 parent 7b45211 commit af8f9f3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### 1.3.20
- Respect locales mapping ([#631](https://github.com/mcamara/laravel-localization/pull/631))

### 1.3.11
- Merged in solution for caching translated and localized routes (originally in separate package [czim/laravel-localization-route-cache](https://github.com/czim/laravel-localization-route-cache)) by [CZim](https://github.com/czim).
- Merged in solution for caching translated and localized routes (originally in separate package [czim/laravel-localization-route-cache](https://github.com/czim/laravel-localization-route-cache)) by [CZim](https://github.com/czim).
If you used this package, be sure to remove it when upgrading to this version.
- Added `'utf8suffix' => env('LARAVELLOCALIZATION_UTF8SUFFIX', '.UTF-8')` to config file.

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ localesMapping``` is needed to enable the LanguageNegotiator to correctly assign
],
```

After that ```http://url-to-laravel/en-GB/a/b/c``` becomes ```http://url-to-laravel/uk/a/b/c```.

```php
LaravelLocalization::getLocalizedURL('en-GB', 'a/b/c'); // http://url-to-laravel/uk/a/b/c
LaravelLocalization::getLocalizedURL('uk', 'a/b/c'); // http://url-to-laravel/uk/a/b/c
```

## Helpers

This package comes with some useful functions, like:
Expand Down
58 changes: 55 additions & 3 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class LaravelLocalization
*/
protected $supportedLocales;

/**
* Locales mapping.
*
* @var array
*/
protected $localesMapping;

/**
* Current locale.
*
Expand Down Expand Up @@ -158,6 +165,8 @@ public function setLocale($locale = null)
}
}

$locale = $this->getInversedLocaleFromMapping($locale);

if (!empty($this->supportedLocales[$locale])) {
$this->currentLocale = $locale;
} else {
Expand Down Expand Up @@ -190,7 +199,7 @@ public function setLocale($locale = null)
setlocale(LC_MONETARY, $regional . $suffix);
}

return $locale;
return $this->getLocaleFromMapping($locale);
}

/**
Expand Down Expand Up @@ -286,6 +295,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $
$parsed_url['path'] = str_replace($base_path, '', '/'.ltrim($parsed_url['path'], '/'));
$path = $parsed_url['path'];
foreach ($this->getSupportedLocales() as $localeCode => $lang) {
$localeCode = $this->getLocaleFromMapping($localeCode);

$parsed_url['path'] = preg_replace('%^/?'.$localeCode.'/%', '$1', $parsed_url['path']);
if ($parsed_url['path'] !== $path) {
$url_locale = $localeCode;
Expand All @@ -306,7 +317,9 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation).$urlQuery;
}

if (!empty($locale)) {
$locale = $this->getLocaleFromMapping($locale);

if (!empty($locale)) {
if ($forceDefaultLocation || $locale != $this->getDefaultLocale() || !$this->hideDefaultLocaleInURL()) {
$parsed_url['path'] = $locale.'/'.ltrim($parsed_url['path'], '/');
}
Expand Down Expand Up @@ -395,6 +408,44 @@ public function getDefaultLocale()
return $this->defaultLocale;
}

/**
* Return locales mapping.
*
* @return array
*/
public function getLocalesMapping()
{
if (empty($this->localesMapping)) {
$this->localesMapping = $this->configRepository->get('laravellocalization.localesMapping');
}

return $this->localesMapping;
}

/**
* Returns a locale from the mapping.
*
* @param string|null $locale
*
* @return string|null
*/
public function getLocaleFromMapping($locale)
{
return $this->getLocalesMapping()[$locale] ?? $locale;
}

/**
* Returns inversed locale from the mapping.
*
* @param string|null $locale
*
* @return string|null
*/
public function getInversedLocaleFromMapping($locale)
{
return \array_flip($this->getLocalesMapping())[$locale] ?? $locale;
}

/**
* Return an array of all supported Locales.
*
Expand Down Expand Up @@ -562,8 +613,9 @@ public function getSupportedLanguagesKeys()
*/
public function checkLocaleInSupportedLocales($locale)
{
$inversedLocale = $this->getInversedLocaleFromMapping($locale);
$locales = $this->getSupportedLocales();
if ($locale !== false && empty($locales[$locale])) {
if ($locale !== false && empty($locales[$locale]) && empty($locales[$inversedLocale])) {
return false;
}

Expand Down
18 changes: 17 additions & 1 deletion tests/LocalizerTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function getPackageAliases($app)
];
}

public function setUp()
public function setUp(): void
{
parent::setUp();
}
Expand Down Expand Up @@ -786,4 +786,20 @@ public function testLanguageNegotiationWithMapping() {
$this->assertEquals($must_resolve_to, $language);
}

public function testSetLocaleWithMapping()
{
app('config')->set('laravellocalization.localesMapping', [
'en' => 'custom',
]);

$this->assertEquals('custom', app('laravellocalization')->setLocale('custom'));
$this->assertEquals('en', app('laravellocalization')->getCurrentLocale());

$this->assertTrue(app('laravellocalization')->checkLocaleInSupportedLocales('en'));
$this->assertTrue(app('laravellocalization')->checkLocaleInSupportedLocales('custom'));

$this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'en'));
$this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom'));
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
}
}

0 comments on commit af8f9f3

Please sign in to comment.