├── .php-cs-fixer.dist.php ├── LICENSE ├── build └── report.junit.xml ├── composer.json ├── config └── domain-localization.php ├── docs ├── 10-getting-started.md ├── 20-usage.md └── 30-examples.md ├── phpstan.src.neon ├── phpstan.tests.neon └── src ├── Concerns └── HasLocaleConfigs.php ├── DomainLocalization.php ├── Exceptions ├── InvalidUrlException.php └── UnsupportedLocaleException.php ├── Facades └── Localization.php ├── Middleware └── SetupLocaleMiddleware.php └── ServiceProvider.php /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in(__DIR__.'/src') 5 | ->in(__DIR__.'/tests/src') 6 | ->ignoreVCSIgnored(true); 7 | 8 | return (new \DistortedFusion\PhpCsFixerConfig\Config)->setFinder($finder); 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kevin Dierkx 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /build/report.junit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kevindierkx/laravel-domain-localization", 3 | "description": "Laravel domain localization", 4 | "homepage": "https://distortedfusion.com/docs/kevindierkx/laravel-domain-localization", 5 | "support": { 6 | "issues": "https://github.com/kevindierkx/laravel-domain-localization/issues", 7 | "source": "https://github.com/kevindierkx/laravel-domain-localization" 8 | }, 9 | "license": "MIT", 10 | "keywords": [ 11 | "distortedfusion", 12 | "laravel", 13 | "domain", 14 | "localization" 15 | ], 16 | "authors": [ 17 | { 18 | "name": "Kevin Dierkx", 19 | "email": "kevin@distortedfusion.com", 20 | "homepage": "https://distortedfusion.com" 21 | }, 22 | { 23 | "name": "Contributors", 24 | "homepage": "https://github.com/kevindierkx/laravel-domain-localization/graphs/contributors" 25 | } 26 | ], 27 | "require": { 28 | "php": "^8.1", 29 | "illuminate/support": "^10.0" 30 | }, 31 | "require-dev": { 32 | "distortedfusion/php-cs-fixer-config": "^2.0", 33 | "nunomaduro/larastan": "^2.4", 34 | "orchestra/testbench-browser-kit": "^8.0", 35 | "phpstan/phpstan-phpunit": "^1.3", 36 | "phpstan/phpstan-strict-rules": "^1.4", 37 | "phpunit/phpunit": "^9.5.10" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Kevindierkx\\LaravelDomainLocalization\\": "src/" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "Kevindierkx\\LaravelDomainLocalization\\Tests\\": "tests/src/" 47 | } 48 | }, 49 | "scripts": { 50 | "phpcs-fix" : "php-cs-fixer fix --using-cache=no --allow-risky=yes --ansi", 51 | "phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes --ansi", 52 | "phpstan-src": "phpstan analyse -l max -c phpstan.src.neon src", 53 | "phpstan-tests": "phpstan analyse -l max -c phpstan.tests.neon tests", 54 | "phpstan": [ 55 | "@phpstan-src" 56 | ], 57 | "phpunit": "phpunit --coverage-text", 58 | "test": [ 59 | "@phpcs", 60 | "@phpstan", 61 | "@phpunit" 62 | ] 63 | }, 64 | "scripts-descriptions": { 65 | "phpcs": "Runs coding style test suite", 66 | "phpunit": "Runs unit and function tests", 67 | "test": "Runs all tests" 68 | }, 69 | "extra": { 70 | "laravel": { 71 | "providers": [ 72 | "Kevindierkx\\LaravelDomainLocalization\\ServiceProvider" 73 | ], 74 | "aliases": { 75 | "Localization": "Kevindierkx\\LaravelDomainLocalization\\Facades\\Localization" 76 | } 77 | } 78 | }, 79 | "config": { 80 | "sort-packages": true 81 | }, 82 | "minimum-stability": "dev", 83 | "prefer-stable": true 84 | } 85 | -------------------------------------------------------------------------------- /config/domain-localization.php: -------------------------------------------------------------------------------- 1 | [ 14 | 'en' => ['tld' => '.com', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'English', 'native' => 'English'], 15 | // 'en-AU' => ['tld' => '.au', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'Australian English', 'native' => 'Australian English'], 16 | // 'en-GB' => ['tld' => '.uk', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'British English', 'native' => 'British English'], 17 | // 'en-US' => ['tld' => '.us', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'U.S. English', 'native' => 'U.S. English'], 18 | // 'es' => ['tld' => '.es', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'Spanish', 'native' => 'español'], 19 | // 'nl' => ['tld' => '.nl', 'script' => 'Latn', 'dir' => 'ltr', 'name' => 'Dutch', 'native' => 'Nederlands'], 20 | ], 21 | ]; 22 | -------------------------------------------------------------------------------- /docs/10-getting-started.md: -------------------------------------------------------------------------------- 1 | # Getting Started 2 | 3 | This Laravel package offers easy i18n domain based localization in Laravel applications. 4 | 5 | For more advanced locale management take a look at [mcamara/laravel-localization](https://github.com/mcamara/laravel-localization). 6 | 7 | ## Installation 8 | 9 | The package can be installed via composer: 10 | 11 | ```bash 12 | composer require kevindierkx/laravel-domain-localization 13 | ``` 14 | 15 | *This package implements Laravel's Package Discovery, no further changes are needed to your application configurations. For more information [please refer to the Laravel documentation](https://laravel.com/docs/packages#package-discovery).* 16 | 17 | ## Version Compatibility 18 | 19 | | Laravel | PHP | Package | 20 | | ------- | -------------- | ------- | 21 | | 9.x | ^8.0 | >= 5.0 | 22 | | 10.x | ^8.1 | >= 5.1 | 23 | 24 | *Only the currently supported PHP versions are listed. Please [refer to previous releases of this package](https://github.com/kevindierkx/laravel-domain-localization/tags) for support for older PHP or Laravel versions.* 25 | 26 | ## Configuration 27 | 28 | Supported locales are defined in the package configuration file. Desired locales can be added or removed after publishing the package configuration file. 29 | 30 | In order to edit the default configuration you need to publish the package configuration to your application config directory: 31 | 32 | ```bash 33 | php artisan vendor:publish --provider="Kevindierkx\LaravelDomainLocalization\ServiceProvider" 34 | ``` 35 | 36 | The config file will be published to `config/domain-localization.php` in your application directory. Please refer to the [config file](https://github.com/kevindierkx/laravel-domain-localization/blob/master/config/domain-localization.php) for an overview of the available options. 37 | 38 | **Please note:** When a desired locale isn't present in the supported locales config an exception will be thrown. 39 | 40 | ## Setup Middleware 41 | 42 | The provided middleware enables dynamically setting the current application locale. To enable this behavior the middleware needs to be registered in the application's middleware array, found in the `app\Http\Kernel.php`: 43 | 44 | ```php 45 | protected $middleware = [ 46 | ... 47 | \Kevindierkx\LaravelDomainLocalization\Middleware\SetupLocaleMiddleware::class, 48 | ]; 49 | ``` 50 | 51 | For example, when you add the dutch locale `nl` the user could access two different locales, using the following addresses: 52 | 53 | ``` 54 | https://example.com 55 | https://example.nl 56 | ``` 57 | 58 | **Please note:** It is not required to use the middleware on all routes. The `Localization` service provides a variety of helper methods to resolve a matching locale from an URL. 59 | -------------------------------------------------------------------------------- /docs/20-usage.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | 3 | The package provides some useful helper methods. For a full list of methods and method descriptions please refer to the [`DomainLocalization::class`](https://github.com/kevindierkx/laravel-domain-localization/blob/master/src/DomainLocalization.php). 4 | 5 | **Please note:** By default the `Localization` facade will be registered during package discovery. In the following examples we will use this facade directly. 6 | 7 | ## Get the localized URL 8 | 9 | By providing a valid URL and the desired locale you can automatically create a localized URL: 10 | 11 | ```php 12 | Localization::getLocalizedUrl('https://example.com/page', 'nl'); 13 | ``` 14 | 15 | Would return: 16 | 17 | ```php 18 | https://example.nl/page 19 | ``` 20 | 21 | ## Listing supported locale configs 22 | 23 | You can either list all configured locales: 24 | 25 | ```php 26 | Localization::getSupportedLocales(); 27 | ``` 28 | 29 | Would return: 30 | 31 | ```php 32 | ['en' => [ 33 | 'tld' => '.com', 34 | 'script' => 'Latn', 35 | 'dir' => 'ltr', 36 | 'name' => 'English', 37 | 'native' => 'English' 38 | ]] 39 | ``` 40 | 41 | Or list a specific locale by its name: 42 | 43 | ```php 44 | Localization::getSupportedLocale('en'); 45 | ``` 46 | 47 | Would return: 48 | 49 | ```php 50 | [ 51 | 'tld' => '.com', 52 | 'script' => 'Latn', 53 | 'dir' => 'ltr', 54 | 'name' => 'English', 55 | 'native' => 'English' 56 | ] 57 | ``` 58 | 59 | Additionally you can simply check if a locale is configured: 60 | 61 | ```php 62 | Localization::hasSupportedLocale('en'); 63 | ``` 64 | 65 | Would return: 66 | 67 | ```php 68 | true 69 | ``` 70 | 71 | ## Resolving locale configs by TLD 72 | 73 | Instead of directly using the locale name to resolve a configuration you can also use the TLD: 74 | 75 | ```php 76 | Localization::getSupportedLocaleByTld('.com'); 77 | ``` 78 | 79 | Would return: 80 | 81 | ```php 82 | [ 83 | 'tld' => '.com', 84 | 'script' => 'Latn', 85 | 'dir' => 'ltr', 86 | 'name' => 'English', 87 | 'native' => 'English' 88 | ] 89 | ``` 90 | 91 | Or resolve the locale name by TLD: 92 | 93 | ```php 94 | Localization::getSupportedLocaleNameByTld('.com'); 95 | ``` 96 | 97 | Would return: 98 | 99 | ```php 100 | 'en' 101 | ``` 102 | 103 | And similar to the locale name you can check for the existence of a locale config by using its TLD: 104 | 105 | ```php 106 | Localization::hasSupportedLocaleByTld('en'); 107 | ``` 108 | 109 | Would return: 110 | 111 | ```php 112 | true 113 | ``` 114 | 115 | ## Resolving the TLD from an URL 116 | 117 | Preferably you wouldn't parse the URL without a lookup table to resolve the TLD. This due to the unusual format of some TLDs. A great package for parsing domains is [jeremykendall/php-domain-parser](https://github.com/jeremykendall/php-domain-parser). 118 | 119 | Luckily this package also supports parsing TLDs, matching them on the configured locales: 120 | 121 | ```php 122 | Localization::getTldFromUrl('https://example.com.local'); 123 | ``` 124 | 125 | Would return: 126 | 127 | ```php 128 | true 129 | ``` 130 | 131 | **Please note:** For this to work `.com.local` needs to be registered as TLD in the supported locales config. This is very effective during development where you can't point multiple domains to your local machine. 132 | 133 | ## Get attributes from locales 134 | 135 | For the current locale or a specific locale you can resolve various configuration attributes using the following helpers: 136 | 137 | ```php 138 | Localization::getTldForCurrentLocale(); 139 | Localization::getNameForCurrentLocale(); 140 | Localization::getDirectionForCurrentLocale(); 141 | Localization::getScriptForCurrentLocale(); 142 | Localization::getNativeForCurrentLocale(); 143 | ``` 144 | 145 | ```php 146 | Localization::getTldForLocale('en'); 147 | Localization::getNameForLocale('en'); 148 | Localization::getDirectionForLocale('en'); 149 | Localization::getScriptForLocale('en'); 150 | Localization::getNativeForLocale('en'); 151 | ``` 152 | 153 | Would return: 154 | 155 | ```php 156 | '.com' 157 | 'Latn' 158 | 'ltr' 159 | 'English' 160 | 'English' 161 | ``` 162 | 163 | ## Modifying the active locale 164 | 165 | When not using the middleware you might want to change the active locale manually: 166 | 167 | ```php 168 | Localization::setCurrentLocale('en'); 169 | ``` 170 | 171 | Or you might just want to check the currently set locale: 172 | 173 | ```php 174 | Localization::getCurrentLocale(); 175 | ``` 176 | 177 | Would return: 178 | 179 | ```php 180 | 'en' 181 | ``` 182 | 183 | During boot we also keep track of the default locale, this is the locale set in the `app.php` config file before any mutations have been made: 184 | 185 | ```php 186 | Localization::getDefaultLocale(); 187 | ``` 188 | 189 | Would return: 190 | 191 | ```php 192 | 'en' 193 | ``` 194 | -------------------------------------------------------------------------------- /docs/30-examples.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | ## Creating a language selector 4 | 5 | Using the helper methods we can create a simple but effective language switcher. The example below uses a [Bootstrap dropdown](https://getbootstrap.com/docs/3.4/components/#dropdowns). 6 | 7 | ```php 8 | 24 | ``` 25 | -------------------------------------------------------------------------------- /phpstan.src.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - vendor/nunomaduro/larastan/extension.neon 3 | - vendor/phpstan/phpstan-phpunit/extension.neon 4 | - vendor/phpstan/phpstan-phpunit/rules.neon 5 | parameters: 6 | reportUnmatchedIgnoredErrors: true 7 | checkMissingIterableValueType: false 8 | -------------------------------------------------------------------------------- /phpstan.tests.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - vendor/nunomaduro/larastan/extension.neon 3 | - vendor/phpstan/phpstan-strict-rules/rules.neon 4 | - vendor/phpstan/phpstan-phpunit/extension.neon 5 | - vendor/phpstan/phpstan-phpunit/rules.neon 6 | parameters: 7 | reportUnmatchedIgnoredErrors: true 8 | checkMissingIterableValueType: false 9 | -------------------------------------------------------------------------------- /src/Concerns/HasLocaleConfigs.php: -------------------------------------------------------------------------------- 1 | supportedLocales[$name] = $config; 35 | } 36 | 37 | /** 38 | * Get an array of all supported locales. 39 | * 40 | * @return array 41 | */ 42 | public function getSupportedLocales(): array 43 | { 44 | return $this->supportedLocales; 45 | } 46 | 47 | /** 48 | * Get a supported locale. 49 | * 50 | * @param string $key 51 | * 52 | * @throws \Kevindierkx\LaravelDomainLocalization\Exceptions\UnsupportedLocaleException 53 | * 54 | * @return array 55 | */ 56 | public function getSupportedLocale(string $key): array 57 | { 58 | if (! $this->hasSupportedLocale($key)) { 59 | throw new UnsupportedLocaleException(sprintf( 60 | 'The locale \'%s\' is not in the `supported_locales` array.', 61 | $key 62 | )); 63 | } 64 | 65 | return $this->supportedLocales[$key]; 66 | } 67 | 68 | /** 69 | * Determine a supported locale exists. 70 | * 71 | * @param string $key 72 | * 73 | * @return bool 74 | */ 75 | public function hasSupportedLocale(string $key): bool 76 | { 77 | return isset($this->supportedLocales[$key]); 78 | } 79 | 80 | /** 81 | * Get tld for current locale. 82 | * 83 | * @return string 84 | */ 85 | public function getTldForCurrentLocale(): string 86 | { 87 | return $this->getTldForLocale($this->getCurrentLocale()); 88 | } 89 | 90 | /** 91 | * Get name for current locale. 92 | * 93 | * @return string 94 | */ 95 | public function getNameForCurrentLocale(): string 96 | { 97 | return $this->getNameForLocale($this->getCurrentLocale()); 98 | } 99 | 100 | /** 101 | * Get direction for current locale. 102 | * 103 | * @return string 104 | */ 105 | public function getDirectionForCurrentLocale(): string 106 | { 107 | return $this->getDirectionForLocale($this->getCurrentLocale()); 108 | } 109 | 110 | /** 111 | * Get script for current locale. 112 | * 113 | * @return string 114 | */ 115 | public function getScriptForCurrentLocale(): string 116 | { 117 | return $this->getScriptForLocale($this->getCurrentLocale()); 118 | } 119 | 120 | /** 121 | * Get native for current locale. 122 | * 123 | * @return string 124 | */ 125 | public function getNativeForCurrentLocale(): string 126 | { 127 | return $this->getNativeForLocale($this->getCurrentLocale()); 128 | } 129 | 130 | /** 131 | * Get tld for locale. 132 | * 133 | * @param string $locale 134 | * 135 | * @return string 136 | */ 137 | public function getTldForLocale(string $locale): string 138 | { 139 | return $this->getSupportedLocale($locale)['tld'] ?? 'unknown'; 140 | } 141 | 142 | /** 143 | * Get name for locale. 144 | * 145 | * @param string $locale 146 | * 147 | * @return string 148 | */ 149 | public function getNameForLocale(string $locale): string 150 | { 151 | return $this->getSupportedLocale($locale)['name'] ?? 'unknown'; 152 | } 153 | 154 | /** 155 | * Get direction for locale. 156 | * 157 | * @param string $locale 158 | * 159 | * @return string 160 | */ 161 | public function getDirectionForLocale(string $locale): string 162 | { 163 | return $this->getSupportedLocale($locale)['dir'] ?? 'unknown'; 164 | } 165 | 166 | /** 167 | * Get script for locale. 168 | * 169 | * @param string $locale 170 | * 171 | * @return string 172 | */ 173 | public function getScriptForLocale(string $locale): string 174 | { 175 | return $this->getSupportedLocale($locale)['script'] ?? 'unknown'; 176 | } 177 | 178 | /** 179 | * Get native for locale. 180 | * 181 | * @param string $locale 182 | * 183 | * @return string 184 | */ 185 | public function getNativeForLocale(string $locale): string 186 | { 187 | return $this->getSupportedLocale($locale)['native'] ?? 'unknown'; 188 | } 189 | 190 | /** 191 | * Get a supported locale name by tld. 192 | * 193 | * @param string $tld 194 | * 195 | * @throws \Kevindierkx\LaravelDomainLocalization\Exceptions\UnsupportedLocaleException 196 | * 197 | * @return string 198 | */ 199 | public function getSupportedLocaleNameByTld(string $tld): string 200 | { 201 | /** @var string */ 202 | $key = Arr::first(array_keys($this->supportedLocales), function ($key) use ($tld) { 203 | return $this->getTldForLocale($key) === $tld; 204 | }); 205 | 206 | if (! $key) { 207 | throw new UnsupportedLocaleException(sprintf( 208 | 'The TLD \'%s\' is not in the `supported_locales` array.', 209 | $tld 210 | )); 211 | } 212 | 213 | return $key; 214 | } 215 | 216 | /** 217 | * Get a supported locale by tld. 218 | * 219 | * @param string $tld 220 | * 221 | * @throws \Kevindierkx\LaravelDomainLocalization\Exceptions\UnsupportedLocaleException 222 | * 223 | * @return array 224 | */ 225 | public function getSupportedLocaleByTld(string $tld): array 226 | { 227 | return $this->getSupportedLocale( 228 | $this->getSupportedLocaleNameByTld($tld) 229 | ); 230 | } 231 | 232 | /** 233 | * Determine a supported locale exists for the tld. 234 | * 235 | * @param string $tld 236 | * 237 | * @return bool 238 | */ 239 | public function hasSupportedLocaleByTld(string $tld): bool 240 | { 241 | try { 242 | $this->getSupportedLocaleNameByTld($tld); 243 | } catch (UnsupportedLocaleException $e) { 244 | return false; 245 | } 246 | 247 | return true; 248 | } 249 | } 250 | -------------------------------------------------------------------------------- /src/DomainLocalization.php: -------------------------------------------------------------------------------- 1 | defaultLocale = $defaultLocale; 51 | 52 | foreach ($locales as $name => $config) { 53 | $this->addLocale((string) $name, $config); 54 | } 55 | 56 | if (empty($this->supportedLocales[$defaultLocale])) { 57 | throw new UnsupportedLocaleException( 58 | 'The default locale is not configured in the `supported_locales` array.' 59 | ); 60 | } 61 | } 62 | 63 | /** 64 | * Get the default application locale. 65 | * 66 | * @return string 67 | */ 68 | public function getDefaultLocale(): string 69 | { 70 | return $this->defaultLocale; 71 | } 72 | 73 | /** 74 | * Get the active app locale. 75 | * 76 | * @return string 77 | */ 78 | public function getCurrentLocale(): string 79 | { 80 | /** @var string */ 81 | $configuredLocale = call_user_func(static::$localeGetter); 82 | 83 | return $configuredLocale; 84 | } 85 | 86 | /** 87 | * Set the active app locale. 88 | * 89 | * @param string $locale 90 | * 91 | * @return self 92 | */ 93 | public function setCurrentLocale($locale): self 94 | { 95 | call_user_func(static::$localeSetter, $locale); 96 | 97 | return $this; 98 | } 99 | 100 | /** 101 | * Get top level domain. 102 | * 103 | * @param string $url 104 | * 105 | * @throws \Kevindierkx\LaravelDomainLocalization\Exceptions\InvalidUrlException 106 | * 107 | * @return string 108 | */ 109 | public function getTldFromUrl(string $url): string 110 | { 111 | if (! ($host = parse_url($url, PHP_URL_HOST))) { 112 | throw new InvalidUrlException(sprintf( 113 | 'The url \'%s\' could not be parsed, make sure the provided URL contains a host.', 114 | $url 115 | )); 116 | } 117 | 118 | $matchingLocales = $this->resolveMatchingLocales($host); 119 | 120 | // When we don't match anything the locale might not be configured. 121 | // We will default to the last element after the final period. 122 | if (empty($matchingLocales)) { 123 | return sprintf('.%s', Str::afterLast($host, '.')); 124 | } 125 | 126 | return $matchingLocales[0]; 127 | } 128 | 129 | /** 130 | * Resolve and sort matching TLDs from the config. 131 | * The best matching/longest will be first in the results. 132 | * 133 | * @param string $host 134 | * 135 | * @return array 136 | */ 137 | protected function resolveMatchingLocales(string $host): array 138 | { 139 | $matches = []; 140 | 141 | foreach ($this->getSupportedLocales() as $config) { 142 | // We ensure the match is at the end of the string to prevent '.com' 143 | // being matched on '.com.dev'. 144 | if ( 145 | isset($config['tld']) 146 | && strpos($host, $config['tld']) !== false 147 | && strlen($host) - strlen($config['tld']) === strrpos($host, $config['tld']) 148 | ) { 149 | $matches[] = $config['tld']; 150 | } 151 | } 152 | 153 | // The best matching TLD will most likely be the longest, before we 154 | // return the matches we sort them on size. 155 | usort($matches, [$this, 'compareStrLength']); 156 | 157 | return $matches; 158 | } 159 | 160 | /** 161 | * Resolve the length difference of two strings, used in the getTld method 162 | * for comparing the best matching TLD. Negative results would push the 163 | * item to the start since the TLD would be longer. 164 | * 165 | * @param string $a 166 | * @param string $b 167 | * 168 | * @return int 169 | */ 170 | protected function compareStrLength(string $a, string $b): int 171 | { 172 | return strlen($b) - strlen($a); 173 | } 174 | 175 | /** 176 | * Localize the URL to the provided locale key or to the default locale when 177 | * no locale is provided. 178 | * 179 | * @param string $url 180 | * @param string|null $key 181 | * 182 | * @throws \Kevindierkx\LaravelDomainLocalization\Exceptions\UnsupportedLocaleException 183 | * 184 | * @return string 185 | */ 186 | public function getLocalizedUrl(string $url, string $key = null): string 187 | { 188 | $key = $key ?: $this->getDefaultLocale(); 189 | 190 | // We validate the supplied locale before we mutate the current URL 191 | // to make sure the locale exists and we don't return an invalid URL. 192 | if (! $this->hasSupportedLocale($key)) { 193 | throw new UnsupportedLocaleException(sprintf( 194 | 'The locale \'%s\' is not in the `supported_locales` array.', 195 | $key 196 | )); 197 | } 198 | 199 | return str_replace( 200 | $this->getTldFromUrl($url), 201 | $this->getTldForLocale($key), 202 | $url 203 | ); 204 | } 205 | 206 | /** 207 | * Set the locale getter closure. 208 | * 209 | * @param Closure $closure 210 | * 211 | * @return void 212 | */ 213 | public static function setLocaleGetter(Closure $closure): void 214 | { 215 | static::$localeGetter = $closure; 216 | } 217 | 218 | /** 219 | * Set the locale setter closure. 220 | * 221 | * @param Closure $closure 222 | * 223 | * @return void 224 | */ 225 | public static function setLocaleSetter(Closure $closure): void 226 | { 227 | static::$localeSetter = $closure; 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidUrlException.php: -------------------------------------------------------------------------------- 1 | getUri()); 22 | 23 | if ($locale = Localization::getSupportedLocaleNameByTld($tld)) { 24 | Localization::setCurrentLocale($locale); 25 | } 26 | 27 | return $next($request); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app; 17 | 18 | $this->defineConfigPublishing(); 19 | 20 | DomainLocalization::setLocaleGetter(function () use ($app) { 21 | return $app->getLocale(); 22 | }); 23 | DomainLocalization::setLocaleSetter(function ($locale) use ($app) { 24 | $app->setLocale($locale); 25 | }); 26 | } 27 | 28 | /** 29 | * Register any application services. 30 | * 31 | * @return void 32 | */ 33 | public function register(): void 34 | { 35 | if (! defined('LARAVEL_DL_PATH')) { 36 | define('LARAVEL_DL_PATH', realpath(__DIR__.'/../')); 37 | } 38 | 39 | $this->mergeConfigFrom(LARAVEL_DL_PATH.'/config/domain-localization.php', 'domain-localization'); 40 | 41 | $this->app->singleton('domain.localization', function ($app) { 42 | return new DomainLocalization( 43 | $app->getLocale(), 44 | $app->make('config')->get('domain-localization.supported_locales', []) 45 | ); 46 | }); 47 | } 48 | 49 | /** 50 | * Define the configuration publishing. 51 | * 52 | * @return void 53 | */ 54 | protected function defineConfigPublishing(): void 55 | { 56 | $this->publishes([ 57 | LARAVEL_DL_PATH.'/config/domain-localization.php' => config_path('domain-localization.php'), 58 | ], 'config'); 59 | } 60 | } 61 | --------------------------------------------------------------------------------