diff --git a/composer.json b/composer.json index e9ada52..7535945 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,9 @@ "suggest": { "ext-intl": "*" }, + "scripts": { + "test": "vendor/bin/phpunit" + }, "autoload": { "classmap": [ ], diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 704b3a7..17070b9 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -702,6 +702,7 @@ public function testCreateUrlFromUri() * @dataProvider accept_language_variations_data */ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = null) { + $full_config = include __DIR__ . '/full-config/config.php'; $request = $this->createMock(\Illuminate\Http\Request::class); @@ -717,9 +718,11 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = $language = $negotiator->negotiateLanguage(); + $this->assertEquals($must_resolve_to, $language); } + public function accept_language_variations_data() { $variations = [ ['en-GB', 'en-GB'], @@ -730,7 +733,7 @@ public function accept_language_variations_data() { ['fr-CA,fr;q=0.8', 'fr-CA'], ['fr-150', 'fr'], ['zh-Hant-cn', 'zh-Hant'], - ['zh-cn', 'zh'] + ['zh-cn', 'zh'], ]; $dataset = []; @@ -740,4 +743,37 @@ public function accept_language_variations_data() { return $dataset; } + + public function testLanguageNegotiationWithMapping() { + + $accept_string = 'en-GB'; + $must_resolve_to = 'uk'; + + $mapping = [ + $accept_string => $must_resolve_to + ]; + + $full_config = include __DIR__ . '/full-config/config.php'; + + $full_config['supportedLocales']['uk'] = $full_config['supportedLocales']['en-GB']; + unset($full_config['supportedLocales']['en-GB']); + + app('config')->set('laravellocalization.localesMapping', $mapping); + + $request = $this->createMock(\Illuminate\Http\Request::class); + $request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string); + + $negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class, + [ + 'defaultLocale' => 'wrong', + 'supportedLanguages' => $full_config['supportedLocales'], + 'request' => $request + ] + ); + + $language = $negotiator->negotiateLanguage(); + + $this->assertEquals($must_resolve_to, $language); + } + } diff --git a/tests/full-config/config.php b/tests/full-config/config.php index c60544e..e976aad 100644 --- a/tests/full-config/config.php +++ b/tests/full-config/config.php @@ -309,11 +309,14 @@ // 'hideDefaultLocaleInURL' => false, - // If you want to display the locales in particular order in the language selector you should write the order here. + // If you want to display the locales in particular order in the language selector you should write the order here. //CAUTION: Please consider using the appropriate locale code otherwise it will not work //Example: 'localesOrder' => ['es','en'], 'localesOrder' => [], + // If you want to use custom lang url segments like 'at' instead of 'de-AT', you can use the mapping to tallow the LanguageNegotiator to assign the descired locales based on HTTP Accept Language Header. For example you want ot use 'at', so map HTTP Accept Language Header 'de-AT' to 'at' (['de-AT' => 'at']). + 'localesMapping' => [], + // URLs which should not be processed, e.g. '/nova', '/nova/*', '/nova-api/*' or specific application URLs // Defaults to [] 'urlsIgnored' => [],