From af8f9f30488a83533dda3870fcc335b55cf964e0 Mon Sep 17 00:00:00 2001 From: Dmitry Bubyakin Date: Fri, 28 Jun 2019 19:04:56 +0300 Subject: [PATCH] Respect locales mapping (#631) * Respect locales mapping * PHPUnit 8 compatibility * Replace localeCode with corresponding value from the mapping * Update README.md and CHANGELOG.md --- CHANGELOG.md | 5 +- README.md | 7 +++ .../LaravelLocalization.php | 58 ++++++++++++++++++- tests/LocalizerTests.php | 18 +++++- 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3471e7d..66a8337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 2ad86db..6a4c7ab 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 9e643db..7d40177 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -84,6 +84,13 @@ class LaravelLocalization */ protected $supportedLocales; + /** + * Locales mapping. + * + * @var array + */ + protected $localesMapping; + /** * Current locale. * @@ -158,6 +165,8 @@ public function setLocale($locale = null) } } + $locale = $this->getInversedLocaleFromMapping($locale); + if (!empty($this->supportedLocales[$locale])) { $this->currentLocale = $locale; } else { @@ -190,7 +199,7 @@ public function setLocale($locale = null) setlocale(LC_MONETARY, $regional . $suffix); } - return $locale; + return $this->getLocaleFromMapping($locale); } /** @@ -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; @@ -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'], '/'); } @@ -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. * @@ -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; } diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 14d3776..d82cf2a 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -25,7 +25,7 @@ protected function getPackageAliases($app) ]; } - public function setUp() + public function setUp(): void { parent::setUp(); } @@ -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')); + } }