From ec5188eeea0f4aa1f681f4fa1561230b506a4d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw?= Date: Sat, 12 Sep 2020 16:17:13 +0200 Subject: [PATCH 01/40] Laravel 8 (#742) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c643de4..45a2352 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0" + "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0" }, "require-dev": { "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0", From fdb68d2a1f09c852e557420a1688dec038311938 Mon Sep 17 00:00:00 2001 From: Oytun Tez Date: Sat, 12 Sep 2020 17:18:37 +0300 Subject: [PATCH 02/40] Use Env::get instead of env (#745) It conflicts with other `env()` global functions (such as CakePHP), and most of them don't handle closures. --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index e4000b5..fcbe356 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -5,6 +5,7 @@ use Illuminate\Config\Repository; use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Support\Str; +use Illuminate\Support\Env; use Mcamara\LaravelLocalization\Exceptions\SupportedLocalesNotDefined; use Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException; @@ -1036,7 +1037,7 @@ protected function normalizeAttributes($attributes) */ protected function getForcedLocale() { - return env(static::ENV_ROUTE_KEY, function () { + return Env::get(static::ENV_ROUTE_KEY, function () { $value = getenv(static::ENV_ROUTE_KEY); if ($value !== false) { From 9191396524c0047eac0e37d336b53f30d8b701a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Sat, 12 Sep 2020 16:23:20 +0200 Subject: [PATCH 03/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 000d16f..6b90642 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2.x 5.2.x-5.8.x (PHP 7 required) | 1.3.x 5.2.0-6.x (PHP 7 required) | 1.4.x - 5.2.0-7.x (PHP 7 required) | 1.5.x + 5.2.0-8.x (PHP 7 required) | 1.6.x ## Installation From 4f0bfd89e5ee8100cb8cff8ca2cc3b985ed46694 Mon Sep 17 00:00:00 2001 From: Oytun Tez Date: Thu, 1 Oct 2020 10:45:06 +0300 Subject: [PATCH 04/40] Support Laravel < 6 for Illuminate\Support\Env usage (#750) * Use Env::get instead of env It conflicts with other `env()` global functions (such as CakePHP), and most of them don't handle closures. * Support Laravel < 6 for Illuminate\Support\Env usage Fixed `env()` and `Illuminate\Support\Env` usage to support Laravel < 6 --- .../LaravelLocalization.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index fcbe356..0bc40fc 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -1037,12 +1037,22 @@ protected function normalizeAttributes($attributes) */ protected function getForcedLocale() { - return Env::get(static::ENV_ROUTE_KEY, function () { - $value = getenv(static::ENV_ROUTE_KEY); + if (version_compare($this->app->version(), '6') >= 0) { + return Env::get(static::ENV_ROUTE_KEY, function () { + $value = getenv(static::ENV_ROUTE_KEY); - if ($value !== false) { - return $value; - } - }); + if ($value !== false) { + return $value; + } + }); + } else { + return env(static::ENV_ROUTE_KEY, function () { + $value = getenv(static::ENV_ROUTE_KEY); + + if ($value !== false) { + return $value; + } + }); + } } } From 0ec9e75c551ce9e5fa094e4c8034bdad13c59b3b Mon Sep 17 00:00:00 2001 From: Adam Nielsen <1765602+iwasherefirst2@users.noreply.github.com> Date: Sun, 25 Oct 2020 18:40:34 +0100 Subject: [PATCH 05/40] Fix route caching documentation In the caching routes documnetation was a small link to `more details`. However, it contained essential instructions. Moved essential instructions to core readme. Fixes: #749, #712, #736 --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b90642..86151f7 100644 --- a/README.md +++ b/README.md @@ -461,7 +461,18 @@ To cache your routes, use: php artisan route:trans:cache ``` -... instead of the normal `route:cache` command. +... instead of the normal `route:cache` command. Using `artisan route:cache` will **not** work correctly! + +For the route caching solution to work, it is required to make a minor adjustment to your application route provision. + +In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait: + +```php + Date: Mon, 4 Jan 2021 21:09:04 +0300 Subject: [PATCH 06/40] fix LocaleCookieRedirect section (#767) It says localeCookieRedirect middleware stores locale in session. I replace session with cookie. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 86151f7..0f27c0a 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ For example, if a user navigates to http://url-to-laravel/test and `en` is the Similar to LocaleSessionRedirect, but it stores value in a cookie instead of a session. -Whenever a locale is present in the url, it will be stored in the session by this middleware. +Whenever a locale is present in the url, it will be stored in the cookie by this middleware. In there is no locale present in the url, then this middleware will check the following From 2aebc553378fae968b1d5420096ea3fcd1d38b43 Mon Sep 17 00:00:00 2001 From: Key Shang Date: Thu, 3 Jun 2021 00:40:42 +0800 Subject: [PATCH 07/40] In `localizeUrl('(/test)')`, the `()` is redundant (#786) In `localizeUrl('(/test)')`, the `()` is redundant. It will generate url like `https://host/en/(/test)`. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f27c0a..a33703c 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ aswell as the `hideDefaultLocaleInURL` and [Translated Routes](#translated-route ```php // If current locale is Spanish, it returns `/es/test` - @lang('Follow this link') + @lang('Follow this link') ``` #### Get localized URL for an specific locale From 1a16ee7d2f06895650866bf00d44e94baad53de3 Mon Sep 17 00:00:00 2001 From: Mike Wink <85063626+mikewink@users.noreply.github.com> Date: Thu, 8 Jul 2021 11:23:45 +0200 Subject: [PATCH 08/40] Add a section for getCurrentLocaleNative() (#789) I think this method is really important and should not be missing in the helpers section. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index a33703c..95f206c 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,13 @@ Return current locale's name as string (English/Spanish/Arabic/ ..etc). {{ LaravelLocalization::getCurrentLocaleName() }} ``` +### Get Current Locale Native Name +Return current locale's native name as string (English/Español/عربى/ ..etc). + +```php +{{ LaravelLocalization::getCurrentLocaleNative() }} +``` + ### Get Current Locale Direction Return current locale's direction as string (ltr/rtl). From 204c3ae564855af4bc87d478e43ab0f1f7f45fb0 Mon Sep 17 00:00:00 2001 From: jargoud Date: Sun, 24 Oct 2021 13:47:09 +0200 Subject: [PATCH 09/40] Handle locales mapping when loading caches routes (#763) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérémy ARGOUD --- .../Traits/LoadsTranslatedCachedRoutes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php b/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php index 1d0e58b..c7353d7 100644 --- a/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php +++ b/src/Mcamara/LaravelLocalization/Traits/LoadsTranslatedCachedRoutes.php @@ -24,7 +24,9 @@ protected function loadCachedRoutes() // compute $locale from url. // It is null if url does not contain locale. - $locale = $localization->setLocale(); + $locale = $localization->getInversedLocaleFromMapping( + $localization->setLocale() + ); $localeKeys = $localization->getSupportedLanguagesKeys(); From c71bb119752a28b9d73fb3c1c342594b761bbc93 Mon Sep 17 00:00:00 2001 From: baltun Date: Sun, 24 Oct 2021 14:47:26 +0300 Subject: [PATCH 10/40] #760 - exclude http methods from processing to solve 'POST not working' issue (#762) Co-authored-by: Ilya Kolesnikov --- README.md | 4 ++++ .../Middleware/LaravelLocalizationMiddlewareBase.php | 3 +++ src/config/config.php | 1 + 3 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 95f206c..d560107 100644 --- a/README.md +++ b/README.md @@ -508,6 +508,10 @@ will not work. Instead, one has to use ``` + +Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored' +to prevent of processing this type of requests + ### MethodNotAllowedHttpException If you do not localize your post url and use a redirect middleware, diff --git a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php index 2f2a10e..e12f908 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php @@ -20,6 +20,9 @@ class LaravelLocalizationMiddlewareBase */ protected function shouldIgnore($request) { + if (in_array($request->method(), config('laravellocalization.httpMethodsIgnored'))) { + return true; + } $this->except = $this->except ?? config('laravellocalization.urlsIgnored', []); foreach ($this->except as $except) { if ($except !== '/') { diff --git a/src/config/config.php b/src/config/config.php index c04a302..32d382f 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -344,4 +344,5 @@ // Defaults to [] 'urlsIgnored' => ['/skipped'], + 'httpMethodsIgnored' => ['POST', 'PUT', 'PATCH', 'DELETE'], ]; From e511c76d553de64357ba8ae61b6c233bd0c1c7e9 Mon Sep 17 00:00:00 2001 From: Daan Raatjes Date: Sun, 24 Oct 2021 13:48:02 +0200 Subject: [PATCH 11/40] Add PHPDoc to facade (#768) --- .../Facades/LaravelLocalization.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php index 0bc0b9f..675266d 100644 --- a/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php @@ -4,6 +4,42 @@ use Illuminate\Support\Facades\Facade; +/** + * @method static string setLocale(string $locale = null) + * @method static bool isHiddenDefault(string $locale) + * @method static void setSupportedLocales(array $locales) + * @method static string localizeUrl(string $url = null, string|bool $locale = null) + * @method static string|false getLocalizedUrl(string|bool $locale = null, string|false $url = null, array $attributes = [], bool $forceDefaultLocation = false) + * @method static string|false getURLFromRouteNameTranslated(string|bool $locale, string $transKeyName, array $attributes = [], bool $forceDefaultLocation = false) + * @method static string getNonLocalizedURL(string|false $url = null) + * @method static string getDefaultLocale() + * @method static array getLocalesMapping() + * @method static string|null getLocalesFromMapping(string|null $locale) + * @method static string|null getInversedLocaleFromMapping(string|null $locale) + * @method static array getSupportedLocales() + * @method static array getLocalesOrder() + * @method static string getCurrentLocaleName() + * @method static string getCurrentLocaleNative() + * @method static string getCurrentLocaleDirection() + * @method static string getCurrentLocaleScript() + * @method static string getCurrentLocaleNativeReading() + * @method static string getCurrentLocale() + * @method static string getCurrentLocaleRegional() + * @method static array getSupportedLanguagesKeys() + * @method static bool checkLocaleInSupportedLocales(string|bool $locale) + * @method static void setRouteName(string $routeName) + * @method static string transRoute(string $routeName) + * @method static string|false getRouteNameFromAPath(string $path) + * @method static \Illuminate\Config\Repository getConfigRepository() + * @method static bool hideUrlAndAcceptHeader() + * @method static bool hideDefaultLocaleInURL() + * @method static string createUrlFromUri(string $uri) + * @method static void setBaseUrl(string $url) + * @method static string getSerializedTranslatedRoutes() + * @method static void setSerializedTranslatedRoutes(string $serializedRoutes) + * + * @see \Mcamara\LaravelLocalization\LaravelLocalization + */ class LaravelLocalization extends Facade { /** From 645819da9ef29f3ba7588d9b4598799caf0b2463 Mon Sep 17 00:00:00 2001 From: DJEMMAL-nour-el-islam <52753848+djemmal-nour-el-islam@users.noreply.github.com> Date: Sun, 24 Oct 2021 12:55:35 +0100 Subject: [PATCH 12/40] fix bug in switching arabic to another language (#722) to handle the case of switching from arabic to another lang Co-authored-by: DJEMMAL-nour-el-islam <52753848+djemml-nour-el-islam@users.noreply.github.com> --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 0bc40fc..5c22b10 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -761,7 +761,7 @@ protected function findTranslatedRouteByUrl($url, $attributes, $locale) $routeName = $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes); // We can ignore extra url parts and compare only their url_path (ignore arguments that are not attributes) - if (parse_url($this->getNonLocalizedURL($routeName), PHP_URL_PATH) == parse_url($this->getNonLocalizedURL($url), PHP_URL_PATH)) { + if (parse_url($this->getNonLocalizedURL($routeName), PHP_URL_PATH) == parse_url($this->getNonLocalizedURL(urldecode($url)), PHP_URL_PATH)) { $this->cachedTranslatedRoutesByUrl[$locale][$url] = $translatedRoute; return $translatedRoute; From dabb0d45ca43bcd078cde866a3dd454694e68cf2 Mon Sep 17 00:00:00 2001 From: John F <15612814+usernotnull@users.noreply.github.com> Date: Wed, 26 Jan 2022 12:25:03 +0200 Subject: [PATCH 13/40] Update LaravelLocalization.php (#806) --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 5c22b10..1f9cbe8 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -270,9 +270,6 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $ $attributes = $this->extractAttributes($url, $locale); } - $urlQuery = parse_url($url, PHP_URL_QUERY); - $urlQuery = $urlQuery ? '?'.$urlQuery : ''; - if (empty($url)) { $url = $this->request->fullUrl(); $urlQuery = parse_url($url, PHP_URL_QUERY); @@ -282,6 +279,9 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $ return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes, $forceDefaultLocation) . $urlQuery; } } else { + $urlQuery = parse_url($url, PHP_URL_QUERY); + $urlQuery = $urlQuery ? '?'.$urlQuery : ''; + $url = $this->url->to($url); } From 4548e6c85a295df15653ab5c46e9121b5624cf71 Mon Sep 17 00:00:00 2001 From: sietzekeuning Date: Wed, 26 Jan 2022 11:27:03 +0100 Subject: [PATCH 14/40] Update composer.json (#812) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 45a2352..8f281fb 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0" + "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0" }, "require-dev": { "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0", From bf6107d7e3c5f3c5ea1f5c4d58d91d0d56550917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Wed, 26 Jan 2022 11:27:28 +0100 Subject: [PATCH 15/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d560107..6a43e22 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2.x 5.2.x-5.8.x (PHP 7 required) | 1.3.x 5.2.0-6.x (PHP 7 required) | 1.4.x - 5.2.0-8.x (PHP 7 required) | 1.6.x + 5.2.0-9.x (PHP 7 required) | 1.6.x ## Installation From 27565315c585e90d8d93aa06afd49d6a5992fe5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Wed, 26 Jan 2022 11:27:45 +0100 Subject: [PATCH 16/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a43e22..d24623b 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2.x 5.2.x-5.8.x (PHP 7 required) | 1.3.x 5.2.0-6.x (PHP 7 required) | 1.4.x - 5.2.0-9.x (PHP 7 required) | 1.6.x + 5.2.0-9.x (PHP 7 required) | 1.7.x ## Installation From 077d160a23af37b9df59ff8464c69b647ed10ad5 Mon Sep 17 00:00:00 2001 From: Muath Alsowadi <34031333+muath-ye@users.noreply.github.com> Date: Wed, 17 Aug 2022 19:47:12 +0300 Subject: [PATCH 17/40] Update misspell at README.md (#824) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d24623b..9aba08e 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ and `useAcceptLanguageHeader` in `config/laravellocalization.php`: Whenever a locale is present in the url, it will be stored in the session by this middleware. -In there is no locale present in the url, then this middleware will check the following +If there is no locale present in the url, then this middleware will check the following - If no locale is saved in session and `useAcceptLanguageHeader` is set to true, compute locale from browser and redirect to url with locale. - If a locale is saved in session redirect to url with locale, unless its the default locale and `hideDefaultLocaleInURL` is set to true. From dc27b7f5ce9b1af122c3dbd3812b6f9e6715bab0 Mon Sep 17 00:00:00 2001 From: Ronan-Lenor Date: Mon, 24 Oct 2022 19:00:50 +0200 Subject: [PATCH 18/40] Add documentation on Helper getCurrentLocaleRegional (#843) --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 9aba08e..8906657 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,13 @@ Return current locale's native name as string (English/Español/عربى/ ..etc) {{ LaravelLocalization::getCurrentLocaleNative() }} ``` +### Get Current Locale Regional Name +Return current locale's regional name as string (en_GB/en_US/fr_FR/ ..etc). + +```php +{{ LaravelLocalization::getCurrentLocaleRegional() }} +``` + ### Get Current Locale Direction Return current locale's direction as string (ltr/rtl). From 7e718b70811a584f943887313bfd4d38ae8d3277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Garc=C3=A9s=20Rodr=C3=ADguez?= <41131304+juliangarcess@users.noreply.github.com> Date: Wed, 4 Jan 2023 12:24:30 +0100 Subject: [PATCH 19/40] Added void to tearDown() in docs (#845) * Update README.md * Added juliangarcess to collaborators --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8906657..517af44 100644 --- a/README.md +++ b/README.md @@ -550,7 +550,7 @@ protected function refreshApplicationWithLocale($locale) self::setUp(); } -protected function tearDown() +protected function tearDown(): void { putenv(LaravelLocalization::ENV_ROUTE_KEY); parent::tearDown(); @@ -566,6 +566,7 @@ public function testBasicTest() ## Collaborators - [Adam Nielsen (iwasherefirst2)](https://github.com/iwasherefirst2) +- [Julián Garcés (juliangarcess)](https://github.com/juliangarcess) Ask [mcamara](https://github.com/mcamara) if you want to be one of them! From 2fd4b872e46a65cfe53b2bdee5505d725718ab72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Wed, 4 Jan 2023 12:29:23 +0100 Subject: [PATCH 20/40] Updated readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 517af44..0623de8 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ The package offers the following: 4.2.x | 0.15.x 5.0.x/5.1.x | 1.0.x 5.2.x-5.4.x (PHP 7 not required) | 1.2.x - 5.2.x-5.8.x (PHP 7 required) | 1.3.x - 5.2.0-6.x (PHP 7 required) | 1.4.x - 5.2.0-9.x (PHP 7 required) | 1.7.x + 5.2.x-5.8.x (PHP version >= 7 required) | 1.3.x + 5.2.0-6.x (PHP version >= 7 required) | 1.4.x + 5.2.0-9.x (PHP version >= 7 required) | 1.7.x ## Installation @@ -516,7 +516,7 @@ will not work. Instead, one has to use ``` -Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored' +Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored' to prevent of processing this type of requests ### MethodNotAllowedHttpException @@ -566,7 +566,6 @@ public function testBasicTest() ## Collaborators - [Adam Nielsen (iwasherefirst2)](https://github.com/iwasherefirst2) -- [Julián Garcés (juliangarcess)](https://github.com/juliangarcess) Ask [mcamara](https://github.com/mcamara) if you want to be one of them! From 9b1779a963c1779d1977d8ad0558a681608e3ec1 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Sat, 18 Feb 2023 10:43:23 -0500 Subject: [PATCH 21/40] Bump dependencies for Laravel 10 (#857) --- composer.json | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 8f281fb..2711412 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,11 @@ { "name": "mcamara/laravel-localization", "description": "Easy localization for Laravel", - "keywords": ["localization", "laravel", "php"], + "keywords": [ + "localization", + "laravel", + "php" + ], "homepage": "https://github.com/mcamara/laravel-localization", "license": "MIT", "authors": [ @@ -13,11 +17,11 @@ ], "require": { "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0" + "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0" }, "require-dev": { - "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0", - "phpunit/phpunit": "6.0.*|^8.0" + "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12", + "phpunit/phpunit": "6.0.*|^8.0|^9.5.10" }, "suggest": { "ext-intl": "*" @@ -26,8 +30,7 @@ "test": "vendor/bin/phpunit" }, "autoload": { - "classmap": [ - ], + "classmap": [], "psr-0": { "Mcamara\\LaravelLocalization": "src/" } From 198e039c143cdc79f37dddf2af48544e87431212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Thu, 9 Mar 2023 10:35:03 +0100 Subject: [PATCH 22/40] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0623de8..0a4fbe6 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2.x 5.2.x-5.8.x (PHP version >= 7 required) | 1.3.x 5.2.0-6.x (PHP version >= 7 required) | 1.4.x - 5.2.0-9.x (PHP version >= 7 required) | 1.7.x + 5.2.0-10.x (PHP version >= 7 required) | 1.8.x ## Installation From 24af7ee06e48f45c884018f8ab33332bbb210c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Mon, 10 Apr 2023 10:29:36 +0900 Subject: [PATCH 23/40] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0a4fbe6..214fb2b 100644 --- a/README.md +++ b/README.md @@ -43,10 +43,9 @@ The package offers the following: 4.1.x | 0.13.x 4.2.x | 0.15.x 5.0.x/5.1.x | 1.0.x - 5.2.x-5.4.x (PHP 7 not required) | 1.2.x - 5.2.x-5.8.x (PHP version >= 7 required) | 1.3.x + 5.2.x-5.4.x (PHP 7 not required) | 1.2. 5.2.0-6.x (PHP version >= 7 required) | 1.4.x - 5.2.0-10.x (PHP version >= 7 required) | 1.8.x + 6.x-10.x (PHP version >= 7 required) | 1.8.x ## Installation From 63331fd434ca8d3c63a1a34ee324a5a62f5782a5 Mon Sep 17 00:00:00 2001 From: Ahmed Sayed Date: Fri, 11 Aug 2023 13:15:33 +0300 Subject: [PATCH 24/40] Fix typo (#876) Thanks for your help, I'll merge it --- src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php index 675266d..e8b633c 100644 --- a/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php @@ -14,7 +14,7 @@ * @method static string getNonLocalizedURL(string|false $url = null) * @method static string getDefaultLocale() * @method static array getLocalesMapping() - * @method static string|null getLocalesFromMapping(string|null $locale) + * @method static string|null getLocaleFromMapping(string|null $locale) * @method static string|null getInversedLocaleFromMapping(string|null $locale) * @method static array getSupportedLocales() * @method static array getLocalesOrder() From 00dc0c99be1633e5bf91235221238e87d6c47834 Mon Sep 17 00:00:00 2001 From: Paul Prins Date: Fri, 11 Aug 2023 12:18:47 +0200 Subject: [PATCH 25/40] Add an example with attributes for getURLFromRouteNameTranslated (#874) This appears to be the most direct way to make links within a website that leverage the `routes.php` files. The lack of documentation for the use of the attributes took me way to long to look up. So here I am providing a simple addition to the documentation to help others (and myself) in the future. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 214fb2b..97ecee5 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,13 @@ Returns a route, [localized](#translated-routes) to the desired locale. If the t // Returns /es/acerca {{ LaravelLocalization::getURLFromRouteNameTranslated('es', 'routes.about') }} ``` +**Example of a localized link using routes with attributes** + +```php +// An array of attributes can be provided. +// Returns /en/archive/ghosts, /fr/archive/fantômes, /pt/arquivo/fantasmas, etc. +Ghost Stories +``` ### Get Supported Locales From afe2e677f280dfdb19ceebb5f8cfe894fb036c17 Mon Sep 17 00:00:00 2001 From: Carlos Alognon Date: Fri, 11 Aug 2023 11:19:39 +0100 Subject: [PATCH 26/40] Change routeMiddleware to middlewareAliases (#864) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97ecee5..b02e6e4 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ class Kernel extends HttpKernel { * * @var array */ - protected $routeMiddleware = [ + protected $middlewareAliases = [ /**** OTHER MIDDLEWARE ****/ 'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class, 'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class, From 05827a68c3b168c5995c6ca6f2bf0ff88818a243 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Wed, 13 Mar 2024 09:29:44 +0100 Subject: [PATCH 27/40] Support contextual binding (#879) * Update: support contextual binding * Update: document breaking v2 changes --- CHANGELOG.md | 5 ++ UPGRADING.md | 41 +++++++++++++++ .../LaravelLocalization.php | 50 ++++++++++--------- .../LaravelLocalizationServiceProvider.php | 4 +- tests/LocalizerTests.php | 10 ++-- 5 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 UPGRADING.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a296ff..a25a5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 2.0.0 +- Support [contextual binding](https://laravel.com/docs/container#contextual-binding) ([#879](https://github.com/mcamara/laravel-localization/pull/879)) + +For guidance on the upgrade process, please refer to the [UPGRADING.md](/UPGRADING.md) file. + ### 1.4.0 - Added compatibility with Laravel 6 diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000..046b13e --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,41 @@ +## From v1 to v2 +This package now uses [dependency injection](https://laravel.com/docs/container#introduction) to retrieve dependencies from the container. + +This modification is a breaking change, especially if you had made extensions to the `__construct` method within the `Mcamara\LaravelLocalization\LaravelLocalization` class. +You may now use depdency injection in your own implementation and forward the dependencies to the parent constructor. +```php +use Mcamara\LaravelLocalization\LaravelLocalization; +use Illuminate\Contracts\Config\Repository as ConfigRepository; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Contracts\Routing\UrlGenerator; +use Illuminate\Contracts\Translation\Translator; +use Illuminate\Http\Request; +use Illuminate\Routing\Router; + +class MyLaravelLocalization extends LaravelLocalization +{ + public function __construct( + mixed $myCustomVariable, + Application $app, + ConfigRepository $configRepository, + Translator $translator, + Router $router, + Request $request, + UrlGenerator $url + ) { + parent::__construct($app, $configRepository, $translator, $router, $request, $url); + } +} +``` + +If your previous approach involved overriding the `LaravelLocalization` singleton in the container and generating a new instance of your custom implementation, there's now a more straightforward method for binding. This will automatically inject the correct dependencies for you. +```diff +use Mcamara\LaravelLocalization\LaravelLocalization; + +-$this->app->singleton(LaravelLocalization::class, function () { +- return new MyLaravelLocalization(); +-}); ++$this->app->singleton(LaravelLocalization::class, MyLaravelLocalization::class); +``` + +For more information, please see the following PR [#879](https://github.com/mcamara/laravel-localization/pull/879/files) \ No newline at end of file diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 1f9cbe8..228ece5 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -2,8 +2,13 @@ namespace Mcamara\LaravelLocalization; -use Illuminate\Config\Repository; +use Illuminate\Contracts\Config\Repository as ConfigRepository; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Contracts\Routing\UrlRoutable; +use Illuminate\Contracts\Translation\Translator; +use Illuminate\Http\Request; +use Illuminate\Routing\Router; use Illuminate\Support\Str; use Illuminate\Support\Env; use Mcamara\LaravelLocalization\Exceptions\SupportedLocalesNotDefined; @@ -19,21 +24,14 @@ class LaravelLocalization /** * Config repository. * - * @var \Illuminate\Config\Repository + * @var \Illuminate\Contracts\Config\Repository */ protected $configRepository; - /** - * Illuminate view Factory. - * - * @var \Illuminate\View\Factory - */ - protected $view; - /** * Illuminate translator class. * - * @var \Illuminate\Translation\Translator + * @var \Illuminate\Contracts\Translation\Translator */ protected $translator; @@ -47,21 +45,21 @@ class LaravelLocalization /** * Illuminate request class. * - * @var \Illuminate\Routing\Request + * @var \Illuminate\Http\Request */ protected $request; /** * Illuminate url class. * - * @var \Illuminate\Routing\UrlGenerator + * @var \Illuminate\Contracts\Routing\UrlGenerator */ protected $url; /** * Illuminate request class. * - * @var Illuminate\Foundation\Application + * @var \Illuminate\Contracts\Foundation\Application */ protected $app; @@ -126,16 +124,20 @@ class LaravelLocalization * * @throws UnsupportedLocaleException */ - public function __construct() - { - $this->app = app(); - - $this->configRepository = $this->app['config']; - $this->view = $this->app['view']; - $this->translator = $this->app['translator']; - $this->router = $this->app['router']; - $this->request = $this->app['request']; - $this->url = $this->app['url']; + public function __construct( + Application $app, + ConfigRepository $configRepository, + Translator $translator, + Router $router, + Request $request, + UrlGenerator $url + ) { + $this->app = $app; + $this->configRepository = $configRepository; + $this->translator = $translator; + $this->router = $router; + $this->request = $request; + $this->url = $url; // set default locale $this->defaultLocale = $this->configRepository->get('app.locale'); @@ -786,7 +788,7 @@ protected function checkUrl($url) /** * Returns the config repository for this instance. * - * @return Repository Configuration repository + * @return \Illuminate\Contracts\Config\Repository Configuration repository */ public function getConfigRepository() { diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php b/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php index 788d8b3..b9b231f 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php @@ -51,9 +51,7 @@ public function register() */ protected function registerBindings() { - $this->app->singleton(LaravelLocalization::class, function () { - return new LaravelLocalization(); - }); + $this->app->singleton(LaravelLocalization::class); $this->app->alias(LaravelLocalization::class, 'laravellocalization'); } diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 80f83f6..54eee63 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -1,6 +1,6 @@ set('laravellocalization.hideDefaultLocaleInURL', true); - app()['request'] = $this->createRequest( + $request = $this->createRequest( $uri = 'en/about?q=2' ); - $laravelLocalization = new \Mcamara\LaravelLocalization\LaravelLocalization(); + $laravelLocalization = app(LaravelLocalization::class, ['request' => $request]); $laravelLocalization->transRoute('LaravelLocalization::routes.about'); $this->assertEquals( @@ -342,10 +342,10 @@ public function testGetLocalizedURLWithQueryStringAndhideDefaultLocaleInURL() public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute() { - app()['request'] = $this->createRequest( + $request = $this->createRequest( $uri = 'en/about?q=2' ); - $laravelLocalization = new \Mcamara\LaravelLocalization\LaravelLocalization(); + $laravelLocalization = app(LaravelLocalization::class, ['request' => $request]); $this->assertEquals( $this->test_url . 'en/about?q=2', From 6248c636251e8b880cfeb05f2d8f30a4c3c7c2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Wed, 13 Mar 2024 09:31:09 +0100 Subject: [PATCH 28/40] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b02e6e4..193de23 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2. 5.2.0-6.x (PHP version >= 7 required) | 1.4.x 6.x-10.x (PHP version >= 7 required) | 1.8.x + 8.x-11.x (PHP version >= 7 required) | 2.0.x ## Installation From 35fbaae553a65bc619c74df9ee48dce02537d846 Mon Sep 17 00:00:00 2001 From: Lars Christian Schou Date: Wed, 13 Mar 2024 09:31:55 +0100 Subject: [PATCH 29/40] laravel-11 Add support for Laravel 11. Propose bumping version to 2.x - since it would require php version >= 8.2 (#897) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cámara --- README.md | 2 +- composer.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 193de23..fdee690 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The package offers the following: 5.2.x-5.4.x (PHP 7 not required) | 1.2. 5.2.0-6.x (PHP version >= 7 required) | 1.4.x 6.x-10.x (PHP version >= 7 required) | 1.8.x - 8.x-11.x (PHP version >= 7 required) | 2.0.x + 10.x-11.x (PHP version >= 8.2 required) | 2.0.x ## Installation diff --git a/composer.json b/composer.json index 2711412..935c5ad 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,11 @@ ], "require": { "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0" + "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0||^11.0" }, "require-dev": { - "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12", - "phpunit/phpunit": "6.0.*|^8.0|^9.5.10" + "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12|^8.5", + "phpunit/phpunit": "6.0.*|^8.0|^9.5.10´|^10.0" }, "suggest": { "ext-intl": "*" From 15959cc6c46e09588c0088c8e6a9f2a175fd42f5 Mon Sep 17 00:00:00 2001 From: Johnny Walker Date: Wed, 13 Mar 2024 10:35:05 +0200 Subject: [PATCH 30/40] Fix typos and layout in config.php (#884) * Update config.php Fix typos and missing carriage returns where describing 'localesMapping' feature. * Update config.php --- src/config/config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/config/config.php b/src/config/config.php index 32d382f..88ccdbc 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -333,7 +333,9 @@ //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']). + // If you want to use custom language URL segments like 'at' instead of 'de-AT', you can map them to allow the + // LanguageNegotiator to assign the desired locales based on HTTP Accept Language Header. For example, if you want + // to use 'at' instead of 'de-AT', you would map 'de-AT' to 'at' (ie. ['de-AT' => 'at']). 'localesMapping' => [], // Locale suffix for LC_TIME and LC_MONETARY From 9b47bb7db4f6712564ceca448b4df8bb7c7e415a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20C=C3=A1mara?= Date: Wed, 13 Mar 2024 09:44:33 +0100 Subject: [PATCH 31/40] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 935c5ad..1c78358 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12|^8.5", - "phpunit/phpunit": "6.0.*|^8.0|^9.5.10´|^10.0" + "phpunit/phpunit": "6.0.*|^8.0|^9.5.10|^10.0" }, "suggest": { "ext-intl": "*" From 0164afa66770a0eb601d7e7d905997838118c9ff Mon Sep 17 00:00:00 2001 From: Oleksandr Moik <50796878+Oleksandr-Moik@users.noreply.github.com> Date: Wed, 24 Jul 2024 12:27:09 +0300 Subject: [PATCH 32/40] Update README.md for Laravel 11 (#900) --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index fdee690..41748fb 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,23 @@ class Kernel extends HttpKernel { } ``` +If you are using Laravel 11, you may register in `bootstrap/app.php` file in closure `withMiddleware`: + +```php +return Application::configure(basePath: dirname(__DIR__)) + // Other application configurations + ->withMiddleware(function (Middleware $middleware) { + $middleware->alias([ + /**** OTHER MIDDLEWARE ALIASES ****/ + 'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class, + 'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class, + 'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class, + 'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class, + 'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class, + ]); + }) +``` + ## Usage Add the following to your routes file: @@ -386,6 +403,9 @@ It is necessary that at least the `localize` middleware in loaded in your `Route For each language, add a `routes.php` into `resources/lang/**/routes.php` folder. The file contains an array with all translatable routes. For example, like this: +> Keep in mind: starting from [Laravel 9](https://laravel.com/docs/9.x/upgrade#the-lang-directory), the `resources/lang` folder is now located in the root project folder (`lang`). +> If your project has `lang` folder in the root, you must add a `routes.php` into `lang/**/routes.php` folder. + ```php Date: Wed, 24 Jul 2024 12:27:40 +0300 Subject: [PATCH 33/40] add docs to loadCacheRoutes in RouteServiceProvider (#902) * adding support for laravel 11 * adding docs to support laravel 11 * adding docs to support laravel 11 * remove class --- CACHING.md | 18 ++++++++++++++++++ README.md | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/CACHING.md b/CACHING.md index c1fa11b..021a247 100644 --- a/CACHING.md +++ b/CACHING.md @@ -6,6 +6,8 @@ If you want to cache the routes in all languages, you will need to use special A For the route caching solution to work, it is required to make a minor adjustment to your application route provision. + +**before laravel 11** In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait: ```php @@ -14,7 +16,23 @@ class RouteServiceProvider extends ServiceProvider { use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes; ``` +**after laravel 11** +In your App's `AppServiceProvider`, use the `CachedTranslatedRouteLoader` class in register method: +```php + $this->loadCachedRoutes()); + ... + } +``` ## Usage diff --git a/README.md b/README.md index 41748fb..4babdbd 100644 --- a/README.md +++ b/README.md @@ -506,6 +506,8 @@ php artisan route:trans:cache For the route caching solution to work, it is required to make a minor adjustment to your application route provision. +**before laravel 11** + In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait: ```php @@ -515,6 +517,26 @@ class RouteServiceProvider extends ServiceProvider use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes; ``` +**after laravel 11** + +In your App's `AppServiceProvider`, use the `CachedTranslatedRouteLoader` class in register method: + +```php + $this->loadCachedRoutes()); + ... + } +``` + + For more details see [here](CACHING.md). From a9a81ab3965dd205c9497268f1c796b05830a008 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Tue, 26 Nov 2024 19:53:00 +0100 Subject: [PATCH 34/40] test: improve implementation using Github Actions * tests: add PSR-4 autoloading for test namespace - Introduced PSR-4 autoloading for the test namespace. - Updated test files to include the new namespace. * test: simplify test case structure - Updated LocalizerTests to extend a new base TestCase. - Moved package provider and alias methods to the new TestCase class. - Cleaned up unnecessary setup code in LocalizerTests. * test: add GitHub Actions workflow for running tests - Introduced a new workflow to automate testing on push and pull requests. - Configured matrix strategy for different PHP and Laravel versions. * chore: remove Travis and CircleCI config files - Deleted `.travis.yml` and `circle.yml` as CI configurations. - Updated README to reflect the switch to GitHub Actions for CI. * test: make PHPUnit config compatible with v10 * test: use testbench package defined in composer.json --- .github/workflows/run-tests.yml | 49 +++++++++++++++++++ .gitignore | 1 + .travis.yml | 20 -------- README.md | 4 +- circle.yml | 3 -- composer.json | 7 ++- phpunit.xml.dist | 30 ++++++------ tests/.gitkeep | 0 ...rTests.php => LaravelLocalizationTest.php} | 24 ++------- tests/ModelWithTranslatableRoutes.php | 2 + tests/TestCase.php | 22 +++++++++ 11 files changed, 102 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/run-tests.yml delete mode 100755 .travis.yml delete mode 100644 circle.yml delete mode 100644 tests/.gitkeep rename tests/{LocalizerTests.php => LaravelLocalizationTest.php} (98%) create mode 100644 tests/TestCase.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..3d0bf86 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,49 @@ +name: run-tests + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + php: [8.4, 8.3, 8.2] + laravel: [11.*, 10.*] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 11.* + testbench: 9.* + - laravel: 10.* + testbench: 8.* + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.64" "orchestra/testbench-browser-kit:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit \ No newline at end of file diff --git a/.gitignore b/.gitignore index 30c2b7b..c909102 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock composer.phar Thumbs.db phpunit.xml +.phpunit.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100755 index 0249f86..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: php - -sudo: false - -dist: trusty - -php: - - 7.1 - - 7.2 - - 7.3 - - 7.4 - -before_script: - - composer self-update - - composer install --prefer-dist --no-interaction - -script: ./vendor/bin/phpunit -d memory_limit=1024M - -matrix: - fast_finish: true diff --git a/README.md b/README.md index 4babdbd..15b08b1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ [![Join the chat at https://gitter.im/mcamara/laravel-localization](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mcamara/laravel-localization?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![Latest Stable Version](https://poser.pugx.org/mcamara/laravel-localization/version.png)](https://packagist.org/packages/mcamara/laravel-localization) [![Total Downloads](https://poser.pugx.org/mcamara/laravel-localization/d/total.png)](https://packagist.org/packages/mcamara/laravel-localization) [![Build Status](https://travis-ci.org/mcamara/laravel-localization.png)](https://travis-ci.org/mcamara/laravel-localization) +[![Latest Stable Version](https://poser.pugx.org/mcamara/laravel-localization/version.png)](https://packagist.org/packages/mcamara/laravel-localization) +[![Total Downloads](https://poser.pugx.org/mcamara/laravel-localization/d/total.png)](https://packagist.org/packages/mcamara/laravel-localization) +![GitHub Actions](https://github.com/mcamara/laravel-localization/actions/workflows/run-tests.yml/badge.svg) [![Open Source Helpers](https://www.codetriage.com/mcamara/laravel-localization/badges/users.svg)](https://www.codetriage.com/mcamara/laravel-localization) [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com) diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 27c19b6..0000000 --- a/circle.yml +++ /dev/null @@ -1,3 +0,0 @@ -machine: - php: - version: 7.0.4 diff --git a/composer.json b/composer.json index 1c78358..aed23a8 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0||^11.0" }, "require-dev": { - "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12|^8.5", + "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12|^8.5|^9.0", "phpunit/phpunit": "6.0.*|^8.0|^9.5.10|^10.0" }, "suggest": { @@ -35,6 +35,11 @@ "Mcamara\\LaravelLocalization": "src/" } }, + "autoload-dev": { + "psr-4": { + "Mcamara\\LaravelLocalization\\Tests\\": "tests" + } + }, "extra": { "laravel": { "providers": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 931dca3..fe86ad2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,23 @@ - - ./tests/ + tests - - - + + ./src/Mcamara - - + + diff --git a/tests/.gitkeep b/tests/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/LocalizerTests.php b/tests/LaravelLocalizationTest.php similarity index 98% rename from tests/LocalizerTests.php rename to tests/LaravelLocalizationTest.php index 54eee63..a3f8e3d 100644 --- a/tests/LocalizerTests.php +++ b/tests/LaravelLocalizationTest.php @@ -1,8 +1,11 @@ 'Mcamara\LaravelLocalization\Facades\LaravelLocalization', - ]; - } - - public function setUp(): void - { - parent::setUp(); - } - /** * Set routes for testing. * diff --git a/tests/ModelWithTranslatableRoutes.php b/tests/ModelWithTranslatableRoutes.php index fc3ddca..4089dfc 100644 --- a/tests/ModelWithTranslatableRoutes.php +++ b/tests/ModelWithTranslatableRoutes.php @@ -1,5 +1,7 @@ LaravelLocalization::class, + ]; + } +} From ce301eb05448a724c8719534b75a1f8ae1e7491d Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Tue, 26 Nov 2024 20:00:11 +0100 Subject: [PATCH 35/40] chore: replace unavailable badges (#915) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 15b08b1..32518da 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ [![Join the chat at https://gitter.im/mcamara/laravel-localization](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mcamara/laravel-localization?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![Latest Stable Version](https://poser.pugx.org/mcamara/laravel-localization/version.png)](https://packagist.org/packages/mcamara/laravel-localization) -[![Total Downloads](https://poser.pugx.org/mcamara/laravel-localization/d/total.png)](https://packagist.org/packages/mcamara/laravel-localization) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization) +[![Total Downloads](https://img.shields.io/packagist/dt/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization) ![GitHub Actions](https://github.com/mcamara/laravel-localization/actions/workflows/run-tests.yml/badge.svg) [![Open Source Helpers](https://www.codetriage.com/mcamara/laravel-localization/badges/users.svg)](https://www.codetriage.com/mcamara/laravel-localization) [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com) From 461982e04f889c0a94dc96fa6fc28f4ab81f4fd8 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Tue, 26 Nov 2024 20:04:14 +0100 Subject: [PATCH 36/40] docs: remove chat badge (#916) Removed the Gitter chat badge from the README, GitHub issues should be used instead. --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 32518da..cca0472 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # Laravel Localization -[![Join the chat at https://gitter.im/mcamara/laravel-localization](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mcamara/laravel-localization?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) - [![Latest Version on Packagist](https://img.shields.io/packagist/v/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization) [![Total Downloads](https://img.shields.io/packagist/dt/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization) ![GitHub Actions](https://github.com/mcamara/laravel-localization/actions/workflows/run-tests.yml/badge.svg) From 75d2a3c6de2d0ca2678e12cea62be36932442c4a Mon Sep 17 00:00:00 2001 From: Bruno Francisco <17223411+CharlieBrownCharacter@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:18:27 -0300 Subject: [PATCH 37/40] docs: updated link to appropriate installation section (#909) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cca0472..33f3514 100644 --- a/README.md +++ b/README.md @@ -398,7 +398,7 @@ Note that Route Model Binding is supported. You may translate your routes. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish) or http://url/en/article/important-article and http://url/es/articulo/important-article (article is articulo in spanish) would be redirected to the same controller/view as follows: -It is necessary that at least the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#LaravelLocalizationRoutes)). +It is necessary that at least the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#installation)). For each language, add a `routes.php` into `resources/lang/**/routes.php` folder. The file contains an array with all translatable routes. For example, like this: From ff6bcf8077131792a7ff2be59d44d18ae0452fb5 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Tue, 26 Nov 2024 22:38:16 +0100 Subject: [PATCH 38/40] chore: update PHP and Laravel version requirements (#917) * chore: update PHP and Laravel version requirements Previous (unsupported) versions were kept in the composer.json file. These version constraints are now aligned with what is mentioned in the README and the run-tests action. * chore: prevent prefer-lowest tests from failing due to PHPUnit 10 deprecations --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index aed23a8..5af85fb 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,12 @@ } ], "require": { - "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0||^11.0" + "php": "^8.2", + "laravel/framework": "^10.0|^11.0" }, "require-dev": { - "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0|^7.12|^8.5|^9.0", - "phpunit/phpunit": "6.0.*|^8.0|^9.5.10|^10.0" + "orchestra/testbench-browser-kit": "^8.5|^9.0", + "phpunit/phpunit": "^9.5.10|^10.0" }, "suggest": { "ext-intl": "*" From fea226dfe4c4352839bb08ee1d6d80302b1ddcf3 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Wed, 27 Nov 2024 20:22:55 +0100 Subject: [PATCH 39/40] chore: solve PHPUnit 10 deprecations (#918) * Bump PHPUnit dependencies * Adopt PHP attributes in test classes * Declare data providers as `static` * Add return types to test methods * Define test classes as `final` * chore: make static dataproviders work * chore: attempt to make tests pass on prefer-lowest * chore: camel case variables --------- Co-authored-by: Shift --- composer.json | 2 +- tests/LaravelLocalizationTest.php | 397 +++++++++++++++--------------- 2 files changed, 198 insertions(+), 201 deletions(-) diff --git a/composer.json b/composer.json index 5af85fb..aa54c99 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "orchestra/testbench-browser-kit": "^8.5|^9.0", - "phpunit/phpunit": "^9.5.10|^10.0" + "phpunit/phpunit": "^10.1" }, "suggest": { "ext-intl": "*" diff --git a/tests/LaravelLocalizationTest.php b/tests/LaravelLocalizationTest.php index a3f8e3d..30304e3 100644 --- a/tests/LaravelLocalizationTest.php +++ b/tests/LaravelLocalizationTest.php @@ -2,13 +2,14 @@ namespace Mcamara\LaravelLocalization\Tests; +use PHPUnit\Framework\Attributes\DataProvider; use Illuminate\Support\Facades\Request; use Mcamara\LaravelLocalization\LaravelLocalization; -class LaravelLocalizationTest extends TestCase +final class LaravelLocalizationTest extends TestCase { - protected $test_url = 'http://localhost/'; - protected $test_url2 = 'http://localhost'; + protected static string $testUrl = 'http://localhost/'; + protected static string $testUrl2 = 'http://localhost'; protected $supportedLocales = []; @@ -117,7 +118,7 @@ protected function createRequest( */ protected function getEnvironmentSetUp($app) { - app('config')->set('app.url', $this->test_url); + app('config')->set('app.url', self::$testUrl); app('config')->set('app.locale', $this->defaultLocale); @@ -133,12 +134,12 @@ protected function getEnvironmentSetUp($app) app('translator')->load('LaravelLocalization', 'routes', 'es'); app('translator')->load('LaravelLocalization', 'routes', 'en'); - app('laravellocalization')->setBaseUrl($this->test_url); + app('laravellocalization')->setBaseUrl(self::$testUrl); $this->setRoutes(); } - public function testSetLocale() + public function testSetLocale(): void { $this->assertEquals(route('about'), 'http://localhost/about'); @@ -157,16 +158,16 @@ public function testSetLocale() $this->assertEquals('en', app('laravellocalization')->getCurrentLocale()); } - public function testLocalizeURL() + public function testLocalizeURL(): void { $this->assertEquals( - $this->test_url.app('laravellocalization')->getCurrentLocale(), + self::$testUrl.app('laravellocalization')->getCurrentLocale(), app('laravellocalization')->localizeURL() ); // Missing trailing slash in a URL $this->assertEquals( - $this->test_url2.'/'.app('laravellocalization')->getCurrentLocale(), + self::$testUrl2.'/'.app('laravellocalization')->getCurrentLocale(), app('laravellocalization')->localizeURL() ); @@ -174,49 +175,49 @@ public function testLocalizeURL() // testing hide default locale option $this->assertNotEquals( - $this->test_url.app('laravellocalization')->getDefaultLocale(), + self::$testUrl.app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->localizeURL() ); $this->assertEquals( - $this->test_url, + self::$testUrl, app('laravellocalization')->localizeURL() ); app('laravellocalization')->setLocale('es'); $this->assertEquals( - $this->test_url.'es', + self::$testUrl.'es', app('laravellocalization')->localizeURL() ); $this->assertEquals( - $this->test_url.'about', - app('laravellocalization')->localizeURL($this->test_url.'about', 'en') + self::$testUrl.'about', + app('laravellocalization')->localizeURL(self::$testUrl.'about', 'en') ); $this->assertNotEquals( - $this->test_url.'en/about', - app('laravellocalization')->localizeURL($this->test_url.'about', 'en') + self::$testUrl.'en/about', + app('laravellocalization')->localizeURL(self::$testUrl.'about', 'en') ); app('config')->set('laravellocalization.hideDefaultLocaleInURL', false); $this->assertEquals( - $this->test_url.'en/about', - app('laravellocalization')->localizeURL($this->test_url.'about', 'en') + self::$testUrl.'en/about', + app('laravellocalization')->localizeURL(self::$testUrl.'about', 'en') ); $this->assertNotEquals( - $this->test_url.'about', - app('laravellocalization')->localizeURL($this->test_url.'about', 'en') + self::$testUrl.'about', + app('laravellocalization')->localizeURL(self::$testUrl.'about', 'en') ); } - public function testGetLocalizedURL() + public function testGetLocalizedURL(): void { $this->assertEquals( - $this->test_url.app('laravellocalization')->getCurrentLocale(), + self::$testUrl.app('laravellocalization')->getCurrentLocale(), app('laravellocalization')->getLocalizedURL() ); @@ -224,37 +225,37 @@ public function testGetLocalizedURL() // testing default language hidden $this->assertNotEquals( - $this->test_url.app('laravellocalization')->getDefaultLocale(), + self::$testUrl.app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getLocalizedURL() ); app('laravellocalization')->setLocale('es'); $this->assertNotEquals( - $this->test_url, + self::$testUrl, app('laravellocalization')->getLocalizedURL() ); $this->assertNotEquals( - $this->test_url.app('laravellocalization')->getDefaultLocale(), + self::$testUrl.app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getLocalizedURL() ); $this->assertEquals( - $this->test_url.app('laravellocalization')->getCurrentLocale(), + self::$testUrl.app('laravellocalization')->getCurrentLocale(), app('laravellocalization')->getLocalizedURL() ); $this->assertEquals( - $this->test_url.'es/acerca', - app('laravellocalization')->getLocalizedURL('es', $this->test_url.'about') + self::$testUrl.'es/acerca', + app('laravellocalization')->getLocalizedURL('es', self::$testUrl.'about') ); app('laravellocalization')->setLocale('en'); $crawler = $this->call( 'GET', - $this->test_url.'about', + self::$testUrl.'about', [], [], [], @@ -263,7 +264,7 @@ public function testGetLocalizedURL() $this->assertResponseOk(); $this->assertEquals( - $this->test_url.'es/acerca', + self::$testUrl.'es/acerca', $crawler->getContent() ); @@ -272,18 +273,18 @@ public function testGetLocalizedURL() app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); $this->assertEquals( - $this->test_url.'test', - app('laravellocalization')->getLocalizedURL('en', $this->test_url.'test') + self::$testUrl.'test', + app('laravellocalization')->getLocalizedURL('en', self::$testUrl.'test') ); $this->assertEquals( - $this->test_url.'test?a=1', - app('laravellocalization')->getLocalizedURL('en', $this->test_url.'test?a=1') + self::$testUrl.'test?a=1', + app('laravellocalization')->getLocalizedURL('en', self::$testUrl.'test?a=1') ); $crawler = $this->call( 'GET', - app('laravellocalization')->getLocalizedURL('en', $this->test_url.'test'), + app('laravellocalization')->getLocalizedURL('en', self::$testUrl.'test'), [], [], [], @@ -299,17 +300,17 @@ public function testGetLocalizedURL() $this->refreshApplication('es'); $this->assertEquals( - $this->test_url.'es/test', - app('laravellocalization')->getLocalizedURL('es', $this->test_url.'test') + self::$testUrl.'es/test', + app('laravellocalization')->getLocalizedURL('es', self::$testUrl.'test') ); $this->assertEquals( - $this->test_url.'es/test?a=1', - app('laravellocalization')->getLocalizedURL('es', $this->test_url.'test?a=1') + self::$testUrl.'es/test?a=1', + app('laravellocalization')->getLocalizedURL('es', self::$testUrl.'test?a=1') ); } - public function testGetLocalizedURLWithQueryStringAndhideDefaultLocaleInURL() + public function testGetLocalizedURLWithQueryStringAndhideDefaultLocaleInURL(): void { app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); $request = $this->createRequest( @@ -319,12 +320,12 @@ public function testGetLocalizedURLWithQueryStringAndhideDefaultLocaleInURL() $laravelLocalization->transRoute('LaravelLocalization::routes.about'); $this->assertEquals( - $this->test_url . 'about?q=2', + self::$testUrl . 'about?q=2', $laravelLocalization->getLocalizedURL() ); } - public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute() + public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute(): void { $request = $this->createRequest( $uri = 'en/about?q=2' @@ -332,7 +333,7 @@ public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute() $laravelLocalization = app(LaravelLocalization::class, ['request' => $request]); $this->assertEquals( - $this->test_url . 'en/about?q=2', + self::$testUrl . 'en/about?q=2', $laravelLocalization->getLocalizedURL() ); } @@ -340,10 +341,9 @@ public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute() /** * @param string $path * @param string|bool $expectedRouteName - * - * @dataProvider getRouteNameFromAPathDataProvider */ - public function testGetRouteNameFromAPath($path, $expectedRouteName) + #[DataProvider('getRouteNameFromAPathDataProvider')] + public function testGetRouteNameFromAPath($path, $expectedRouteName): void { $this->assertEquals( $expectedRouteName, @@ -351,29 +351,29 @@ public function testGetRouteNameFromAPath($path, $expectedRouteName) ); } - public function getRouteNameFromAPathDataProvider() + public static function getRouteNameFromAPathDataProvider(): array { return [ - [$this->test_url, false], - [$this->test_url.'es', false], - [$this->test_url.'en/about', 'LaravelLocalization::routes.about'], - [$this->test_url.'ver/1', false], - [$this->test_url.'view/1', 'LaravelLocalization::routes.view'], - [$this->test_url.'view/1/project', 'LaravelLocalization::routes.view_project'], - [$this->test_url.'view/1/project/1', 'LaravelLocalization::routes.view_project'], - [$this->test_url.'en/view/1/project/1', 'LaravelLocalization::routes.view_project'], - [$this->test_url.'manage/1', 'LaravelLocalization::routes.manage'], - [$this->test_url.'manage', 'LaravelLocalization::routes.manage'], - [$this->test_url.'manage/', 'LaravelLocalization::routes.manage'], - [$this->test_url.'manage/0', 'LaravelLocalization::routes.manage'], - [$this->test_url.'manage/0?ex=2&ex2=a', 'LaravelLocalization::routes.manage'], + [self::$testUrl, false], + [self::$testUrl.'es', false], + [self::$testUrl.'en/about', 'LaravelLocalization::routes.about'], + [self::$testUrl.'ver/1', false], + [self::$testUrl.'view/1', 'LaravelLocalization::routes.view'], + [self::$testUrl.'view/1/project', 'LaravelLocalization::routes.view_project'], + [self::$testUrl.'view/1/project/1', 'LaravelLocalization::routes.view_project'], + [self::$testUrl.'en/view/1/project/1', 'LaravelLocalization::routes.view_project'], + [self::$testUrl.'manage/1', 'LaravelLocalization::routes.manage'], + [self::$testUrl.'manage', 'LaravelLocalization::routes.manage'], + [self::$testUrl.'manage/', 'LaravelLocalization::routes.manage'], + [self::$testUrl.'manage/0', 'LaravelLocalization::routes.manage'], + [self::$testUrl.'manage/0?ex=2&ex2=a', 'LaravelLocalization::routes.manage'], ]; } - public function testGetLocalizedUrlForIgnoredUrls() { + public function testGetLocalizedUrlForIgnoredUrls(): void { $crawler = $this->call( 'GET', - $this->test_url2.'/skipped', + self::$testUrl2.'/skipped', [], [], [], @@ -382,7 +382,7 @@ public function testGetLocalizedUrlForIgnoredUrls() { $this->assertResponseOk(); $this->assertEquals( - $this->test_url2.'/skipped', + self::$testUrl2.'/skipped', $crawler->getContent() ); } @@ -393,10 +393,9 @@ public function testGetLocalizedUrlForIgnoredUrls() { * @param string $locale * @param string $path * @param string $expectedURL - * - * @dataProvider getLocalizedURLDataProvider */ - public function testGetLocalizedURLFormat($hideDefaultLocaleInURL, $forceDefault, $locale, $path, $expectedURL) + #[DataProvider('getLocalizedURLDataProvider')] + public function testGetLocalizedURLFormat($hideDefaultLocaleInURL, $forceDefault, $locale, $path, $expectedURL): void { app('config')->set('laravellocalization.hideDefaultLocaleInURL', $hideDefaultLocaleInURL); $this->assertEquals( @@ -406,212 +405,212 @@ public function testGetLocalizedURLFormat($hideDefaultLocaleInURL, $forceDefault } - public function getLocalizedURLDataProvider() + public static function getLocalizedURLDataProvider(): array { return [ // Do not hide default - [false, false, 'es', $this->test_url, $this->test_url.'es'], - [false, false, 'es', $this->test_url.'es', $this->test_url.'es'], - [false, false, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'], - [false, false, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'], - [false, false, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'], - [false, false, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [false, false, 'es', $this->test_url.'en/view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [false, false, 'es', $this->test_url.'manage/1', $this->test_url.'es/administrar/1'], - [false, false, 'es', $this->test_url.'manage', $this->test_url.'es/administrar'], - [false, false, 'es', $this->test_url.'manage/', $this->test_url.'es/administrar'], - [false, false, 'es', $this->test_url.'manage/0', $this->test_url.'es/administrar/0'], - [false, false, 'es', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'es/administrar/0?ex=2&ex2=a'], + [false, false, 'es', self::$testUrl, self::$testUrl.'es'], + [false, false, 'es', self::$testUrl.'es', self::$testUrl.'es'], + [false, false, 'es', self::$testUrl.'en/about', self::$testUrl.'es/acerca'], + [false, false, 'es', self::$testUrl.'ver/1', self::$testUrl.'es/ver/1'], + [false, false, 'es', self::$testUrl.'view/1/project', self::$testUrl.'es/ver/1/proyecto'], + [false, false, 'es', self::$testUrl.'view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [false, false, 'es', self::$testUrl.'en/view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [false, false, 'es', self::$testUrl.'manage/1', self::$testUrl.'es/administrar/1'], + [false, false, 'es', self::$testUrl.'manage', self::$testUrl.'es/administrar'], + [false, false, 'es', self::$testUrl.'manage/', self::$testUrl.'es/administrar'], + [false, false, 'es', self::$testUrl.'manage/0', self::$testUrl.'es/administrar/0'], + [false, false, 'es', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'es/administrar/0?ex=2&ex2=a'], // Do not hide default - [false, false, 'en', $this->test_url.'en', $this->test_url.'en'], - [false, false, 'en', $this->test_url.'about', $this->test_url.'en/about'], - [false, false, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'], - [false, false, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'], - [false, false, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'], - [false, false, 'en', $this->test_url.'en/view/1/project/1', $this->test_url.'en/view/1/project/1'], - [false, false, 'en', $this->test_url.'manage/1', $this->test_url.'en/manage/1'], - [false, false, 'en', $this->test_url.'manage', $this->test_url.'en/manage'], - [false, false, 'en', $this->test_url.'manage/', $this->test_url.'en/manage'], - [false, false, 'en', $this->test_url.'manage/0', $this->test_url.'en/manage/0'], - [false, false, 'en', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'en/manage/0?ex=2&ex2=a'], + [false, false, 'en', self::$testUrl.'en', self::$testUrl.'en'], + [false, false, 'en', self::$testUrl.'about', self::$testUrl.'en/about'], + [false, false, 'en', self::$testUrl.'ver/1', self::$testUrl.'en/ver/1'], + [false, false, 'en', self::$testUrl.'view/1/project', self::$testUrl.'en/view/1/project'], + [false, false, 'en', self::$testUrl.'view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [false, false, 'en', self::$testUrl.'en/view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [false, false, 'en', self::$testUrl.'manage/1', self::$testUrl.'en/manage/1'], + [false, false, 'en', self::$testUrl.'manage', self::$testUrl.'en/manage'], + [false, false, 'en', self::$testUrl.'manage/', self::$testUrl.'en/manage'], + [false, false, 'en', self::$testUrl.'manage/0', self::$testUrl.'en/manage/0'], + [false, false, 'en', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'en/manage/0?ex=2&ex2=a'], // Hide default - [true, false, 'es', $this->test_url, $this->test_url.'es'], - [true, false, 'es', $this->test_url.'es', $this->test_url.'es'], - [true, false, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'], - [true, false, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'], - [true, false, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'], - [true, false, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [true, false, 'es', $this->test_url.'en/view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [true, false, 'es', $this->test_url.'manage/1', $this->test_url.'es/administrar/1'], - [true, false, 'es', $this->test_url.'manage', $this->test_url.'es/administrar'], - [true, false, 'es', $this->test_url.'manage/', $this->test_url.'es/administrar'], - [true, false, 'es', $this->test_url.'manage/0', $this->test_url.'es/administrar/0'], - [true, false, 'es', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'es/administrar/0?ex=2&ex2=a'], + [true, false, 'es', self::$testUrl, self::$testUrl.'es'], + [true, false, 'es', self::$testUrl.'es', self::$testUrl.'es'], + [true, false, 'es', self::$testUrl.'en/about', self::$testUrl.'es/acerca'], + [true, false, 'es', self::$testUrl.'ver/1', self::$testUrl.'es/ver/1'], + [true, false, 'es', self::$testUrl.'view/1/project', self::$testUrl.'es/ver/1/proyecto'], + [true, false, 'es', self::$testUrl.'view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [true, false, 'es', self::$testUrl.'en/view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [true, false, 'es', self::$testUrl.'manage/1', self::$testUrl.'es/administrar/1'], + [true, false, 'es', self::$testUrl.'manage', self::$testUrl.'es/administrar'], + [true, false, 'es', self::$testUrl.'manage/', self::$testUrl.'es/administrar'], + [true, false, 'es', self::$testUrl.'manage/0', self::$testUrl.'es/administrar/0'], + [true, false, 'es', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'es/administrar/0?ex=2&ex2=a'], // Hide default - [true, false, 'en', $this->test_url.'en', $this->test_url.''], - [true, false, 'en', $this->test_url.'about', $this->test_url.'about'], - [true, false, 'en', $this->test_url.'ver/1', $this->test_url.'ver/1'], - [true, false, 'en', $this->test_url.'view/1/project', $this->test_url.'view/1/project'], - [true, false, 'en', $this->test_url.'view/1/project/1', $this->test_url.'view/1/project/1'], - [true, false, 'en', $this->test_url.'en/view/1/project/1', $this->test_url.'view/1/project/1'], - [true, false, 'en', $this->test_url.'manage/1', $this->test_url.'manage/1'], - [true, false, 'en', $this->test_url.'manage', $this->test_url.'manage'], - [true, false, 'en', $this->test_url.'manage/', $this->test_url.'manage'], - [true, false, 'en', $this->test_url.'manage/0', $this->test_url.'manage/0'], - [true, false, 'en', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'manage/0?ex=2&ex2=a'], + [true, false, 'en', self::$testUrl.'en', self::$testUrl.''], + [true, false, 'en', self::$testUrl.'about', self::$testUrl.'about'], + [true, false, 'en', self::$testUrl.'ver/1', self::$testUrl.'ver/1'], + [true, false, 'en', self::$testUrl.'view/1/project', self::$testUrl.'view/1/project'], + [true, false, 'en', self::$testUrl.'view/1/project/1', self::$testUrl.'view/1/project/1'], + [true, false, 'en', self::$testUrl.'en/view/1/project/1', self::$testUrl.'view/1/project/1'], + [true, false, 'en', self::$testUrl.'manage/1', self::$testUrl.'manage/1'], + [true, false, 'en', self::$testUrl.'manage', self::$testUrl.'manage'], + [true, false, 'en', self::$testUrl.'manage/', self::$testUrl.'manage'], + [true, false, 'en', self::$testUrl.'manage/0', self::$testUrl.'manage/0'], + [true, false, 'en', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'manage/0?ex=2&ex2=a'], // Do not hide default FORCE SHOWING - [false, true, 'es', $this->test_url, $this->test_url.'es'], - [false, true, 'es', $this->test_url.'es', $this->test_url.'es'], - [false, true, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'], - [false, true, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'], - [false, true, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'], - [false, true, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [false, true, 'es', $this->test_url.'en/view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [false, true, 'es', $this->test_url.'manage/1', $this->test_url.'es/administrar/1'], - [false, true, 'es', $this->test_url.'manage', $this->test_url.'es/administrar'], - [false, true, 'es', $this->test_url.'manage/', $this->test_url.'es/administrar'], - [false, true, 'es', $this->test_url.'manage/0', $this->test_url.'es/administrar/0'], - [false, true, 'es', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'es/administrar/0?ex=2&ex2=a'], + [false, true, 'es', self::$testUrl, self::$testUrl.'es'], + [false, true, 'es', self::$testUrl.'es', self::$testUrl.'es'], + [false, true, 'es', self::$testUrl.'en/about', self::$testUrl.'es/acerca'], + [false, true, 'es', self::$testUrl.'ver/1', self::$testUrl.'es/ver/1'], + [false, true, 'es', self::$testUrl.'view/1/project', self::$testUrl.'es/ver/1/proyecto'], + [false, true, 'es', self::$testUrl.'view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [false, true, 'es', self::$testUrl.'en/view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [false, true, 'es', self::$testUrl.'manage/1', self::$testUrl.'es/administrar/1'], + [false, true, 'es', self::$testUrl.'manage', self::$testUrl.'es/administrar'], + [false, true, 'es', self::$testUrl.'manage/', self::$testUrl.'es/administrar'], + [false, true, 'es', self::$testUrl.'manage/0', self::$testUrl.'es/administrar/0'], + [false, true, 'es', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'es/administrar/0?ex=2&ex2=a'], // Do not hide default FORCE SHOWING - [false, true, 'en', $this->test_url.'en', $this->test_url.'en'], - [false, true, 'en', $this->test_url.'about', $this->test_url.'en/about'], - [false, true, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'], - [false, true, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'], - [false, true, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'], - [false, true, 'en', $this->test_url.'en/view/1/project/1', $this->test_url.'en/view/1/project/1'], - [false, true, 'en', $this->test_url.'manage/1', $this->test_url.'en/manage/1'], - [false, true, 'en', $this->test_url.'manage', $this->test_url.'en/manage'], - [false, true, 'en', $this->test_url.'manage/', $this->test_url.'en/manage'], - [false, true, 'en', $this->test_url.'manage/0', $this->test_url.'en/manage/0'], - [false, true, 'en', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'en/manage/0?ex=2&ex2=a'], + [false, true, 'en', self::$testUrl.'en', self::$testUrl.'en'], + [false, true, 'en', self::$testUrl.'about', self::$testUrl.'en/about'], + [false, true, 'en', self::$testUrl.'ver/1', self::$testUrl.'en/ver/1'], + [false, true, 'en', self::$testUrl.'view/1/project', self::$testUrl.'en/view/1/project'], + [false, true, 'en', self::$testUrl.'view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [false, true, 'en', self::$testUrl.'en/view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [false, true, 'en', self::$testUrl.'manage/1', self::$testUrl.'en/manage/1'], + [false, true, 'en', self::$testUrl.'manage', self::$testUrl.'en/manage'], + [false, true, 'en', self::$testUrl.'manage/', self::$testUrl.'en/manage'], + [false, true, 'en', self::$testUrl.'manage/0', self::$testUrl.'en/manage/0'], + [false, true, 'en', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'en/manage/0?ex=2&ex2=a'], // Hide default FORCE SHOWING - [true, true, 'es', $this->test_url, $this->test_url.'es'], - [true, true, 'es', $this->test_url.'es', $this->test_url.'es'], - [true, true, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'], - [true, true, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'], - [true, true, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'], - [true, true, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [true, true, 'es', $this->test_url.'en/view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'], - [true, true, 'es', $this->test_url.'manage/1', $this->test_url.'es/administrar/1'], - [true, true, 'es', $this->test_url.'manage', $this->test_url.'es/administrar'], - [true, true, 'es', $this->test_url.'manage/', $this->test_url.'es/administrar'], - [true, true, 'es', $this->test_url.'manage/0', $this->test_url.'es/administrar/0'], - [true, true, 'es', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'es/administrar/0?ex=2&ex2=a'], + [true, true, 'es', self::$testUrl, self::$testUrl.'es'], + [true, true, 'es', self::$testUrl.'es', self::$testUrl.'es'], + [true, true, 'es', self::$testUrl.'en/about', self::$testUrl.'es/acerca'], + [true, true, 'es', self::$testUrl.'ver/1', self::$testUrl.'es/ver/1'], + [true, true, 'es', self::$testUrl.'view/1/project', self::$testUrl.'es/ver/1/proyecto'], + [true, true, 'es', self::$testUrl.'view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [true, true, 'es', self::$testUrl.'en/view/1/project/1', self::$testUrl.'es/ver/1/proyecto/1'], + [true, true, 'es', self::$testUrl.'manage/1', self::$testUrl.'es/administrar/1'], + [true, true, 'es', self::$testUrl.'manage', self::$testUrl.'es/administrar'], + [true, true, 'es', self::$testUrl.'manage/', self::$testUrl.'es/administrar'], + [true, true, 'es', self::$testUrl.'manage/0', self::$testUrl.'es/administrar/0'], + [true, true, 'es', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'es/administrar/0?ex=2&ex2=a'], // Hide default FORCE SHOWING - [true, true, 'en', $this->test_url.'en', $this->test_url.'en'], - [true, true, 'en', $this->test_url.'about', $this->test_url.'en/about'], - [true, true, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'], - [true, true, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'], - [true, true, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'], - [true, true, 'en', $this->test_url.'en/view/1/project/1', $this->test_url.'en/view/1/project/1'], - [true, true, 'en', $this->test_url.'manage/1', $this->test_url.'en/manage/1'], - [true, true, 'en', $this->test_url.'manage', $this->test_url.'en/manage'], - [true, true, 'en', $this->test_url.'manage/', $this->test_url.'en/manage'], - [true, true, 'en', $this->test_url.'manage/0', $this->test_url.'en/manage/0'], - [true, true, 'en', $this->test_url.'manage/0?ex=2&ex2=a', $this->test_url.'en/manage/0?ex=2&ex2=a'], + [true, true, 'en', self::$testUrl.'en', self::$testUrl.'en'], + [true, true, 'en', self::$testUrl.'about', self::$testUrl.'en/about'], + [true, true, 'en', self::$testUrl.'ver/1', self::$testUrl.'en/ver/1'], + [true, true, 'en', self::$testUrl.'view/1/project', self::$testUrl.'en/view/1/project'], + [true, true, 'en', self::$testUrl.'view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [true, true, 'en', self::$testUrl.'en/view/1/project/1', self::$testUrl.'en/view/1/project/1'], + [true, true, 'en', self::$testUrl.'manage/1', self::$testUrl.'en/manage/1'], + [true, true, 'en', self::$testUrl.'manage', self::$testUrl.'en/manage'], + [true, true, 'en', self::$testUrl.'manage/', self::$testUrl.'en/manage'], + [true, true, 'en', self::$testUrl.'manage/0', self::$testUrl.'en/manage/0'], + [true, true, 'en', self::$testUrl.'manage/0?ex=2&ex2=a', self::$testUrl.'en/manage/0?ex=2&ex2=a'], ]; } - public function testGetURLFromRouteNameTranslated() + public function testGetURLFromRouteNameTranslated(): void { $this->assertEquals( - $this->test_url.'es/acerca', + self::$testUrl.'es/acerca', app('laravellocalization')->getURLFromRouteNameTranslated('es', 'LaravelLocalization::routes.about') ); $this->assertEquals( - $this->test_url.'en/about', + self::$testUrl.'en/about', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.about') ); $this->assertEquals( - $this->test_url.'en/view/1', + self::$testUrl.'en/view/1', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => 1]) ); app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); $this->assertEquals( - $this->test_url.'about', + self::$testUrl.'about', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.about') ); $this->assertEquals( - $this->test_url.'es/acerca', + self::$testUrl.'es/acerca', app('laravellocalization')->getURLFromRouteNameTranslated('es', 'LaravelLocalization::routes.about') ); $this->assertEquals( - $this->test_url.'es/ver/1', + self::$testUrl.'es/ver/1', app('laravellocalization')->getURLFromRouteNameTranslated('es', 'LaravelLocalization::routes.view', ['id' => 1]) ); $this->assertEquals( - $this->test_url.'view/1', + self::$testUrl.'view/1', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => 1]) ); $this->assertNotEquals( - $this->test_url.'en/view/1', + self::$testUrl.'en/view/1', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => 1]) ); app('config')->set('laravellocalization.hideDefaultLocaleInURL', false); $this->assertNotEquals( - $this->test_url.'view/1', + self::$testUrl.'view/1', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => 1]) ); $this->assertEquals( - $this->test_url.'en/view/1', + self::$testUrl.'en/view/1', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => 1]) ); } - public function testLocalizedParameterFromTranslateUrl() + public function testLocalizedParameterFromTranslateUrl(): void { $model = new ModelWithTranslatableRoutes(); $this->assertEquals( - $this->test_url.'en/view/company', + self::$testUrl.'en/view/company', app('laravellocalization')->getURLFromRouteNameTranslated('en', 'LaravelLocalization::routes.view', ['id' => $model]) ); $this->assertEquals( - $this->test_url.'es/ver/empresa', + self::$testUrl.'es/ver/empresa', app('laravellocalization')->getURLFromRouteNameTranslated('es', 'LaravelLocalization::routes.view', ['id' => $model]) ); } - public function testGetNonLocalizedURL() + public function testGetNonLocalizedURL(): void { $this->assertEquals( - $this->test_url, - app('laravellocalization')->getNonLocalizedURL($this->test_url.'en') + self::$testUrl, + app('laravellocalization')->getNonLocalizedURL(self::$testUrl.'en') ); $this->assertEquals( - $this->test_url, - app('laravellocalization')->getNonLocalizedURL($this->test_url.'es') + self::$testUrl, + app('laravellocalization')->getNonLocalizedURL(self::$testUrl.'es') ); $this->assertEquals( - $this->test_url.'view/1', - app('laravellocalization')->getNonLocalizedURL($this->test_url.'en/view/1') + self::$testUrl.'view/1', + app('laravellocalization')->getNonLocalizedURL(self::$testUrl.'en/view/1') ); $this->assertEquals( - $this->test_url.'ver/1', - app('laravellocalization')->getNonLocalizedURL($this->test_url.'es/ver/1') + self::$testUrl.'ver/1', + app('laravellocalization')->getNonLocalizedURL(self::$testUrl.'es/ver/1') ); } - public function testGetDefaultLocale() + public function testGetDefaultLocale(): void { $this->assertEquals( 'en', @@ -627,7 +626,7 @@ public function testGetDefaultLocale() ); } - public function testGetSupportedLocales() + public function testGetSupportedLocales(): void { $this->assertEquals( $this->supportedLocales, @@ -635,7 +634,7 @@ public function testGetSupportedLocales() ); } - public function testGetCurrentLocaleName() + public function testGetCurrentLocaleName(): void { $this->assertEquals( 'English', @@ -650,7 +649,7 @@ public function testGetCurrentLocaleName() ); } - public function testGetCurrentLocaleRegional() + public function testGetCurrentLocaleRegional(): void { $this->assertEquals( 'en_GB', @@ -665,7 +664,7 @@ public function testGetCurrentLocaleRegional() ); } - public function testGetCurrentLocaleDirection() + public function testGetCurrentLocaleDirection(): void { $this->assertEquals( 'ltr', @@ -681,7 +680,7 @@ public function testGetCurrentLocaleDirection() ); } - public function testGetCurrentLocaleScript() + public function testGetCurrentLocaleScript(): void { app('laravellocalization')->setLocale('es'); $this->refreshApplication('es'); @@ -700,7 +699,7 @@ public function testGetCurrentLocaleScript() ); } - public function testGetCurrentLocaleNativeReading() + public function testGetCurrentLocaleNativeReading(): void { $this->assertEquals( 'English', @@ -716,7 +715,7 @@ public function testGetCurrentLocaleNativeReading() ); } - public function testGetCurrentLocale() + public function testGetCurrentLocale(): void { $this->assertEquals( 'en', @@ -737,7 +736,7 @@ public function testGetCurrentLocale() ); } - public function testGetSupportedLanguagesKeys() + public function testGetSupportedLanguagesKeys(): void { $this->assertEquals( ['en', 'es'], @@ -745,7 +744,7 @@ public function testGetSupportedLanguagesKeys() ); } - public function testGetConfigRepository() + public function testGetConfigRepository(): void { $this->assertEquals( app('config'), @@ -753,7 +752,7 @@ public function testGetConfigRepository() ); } - public function testCreateUrlFromUri() + public function testCreateUrlFromUri(): void { $this->assertEquals( 'http://localhost/view/1', @@ -770,10 +769,8 @@ public function testCreateUrlFromUri() } - /** - * @dataProvider accept_language_variations_data - */ - public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = null) { + #[DataProvider('accept_language_variations_data')] + public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = null): void { $full_config = include __DIR__ . '/full-config/config.php'; @@ -795,7 +792,7 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = } - public function accept_language_variations_data() { + public static function accept_language_variations_data(): array { $variations = [ ['en-GB', 'en-GB'], ['en-US', 'en-US'], @@ -816,7 +813,7 @@ public function accept_language_variations_data() { return $dataset; } - public function testLanguageNegotiationWithMapping() { + public function testLanguageNegotiationWithMapping(): void { $accept_string = 'en-GB'; $must_resolve_to = 'uk'; @@ -848,7 +845,7 @@ public function testLanguageNegotiationWithMapping() { $this->assertEquals($must_resolve_to, $language); } - public function testSetLocaleWithMapping() + public function testSetLocaleWithMapping(): void { app('config')->set('laravellocalization.localesMapping', [ 'en' => 'custom', From a19ddcc634dff6b06ae35ecb7f06b2a0444e9807 Mon Sep 17 00:00:00 2001 From: jordyvanderhaegen Date: Wed, 27 Nov 2024 22:06:34 +0100 Subject: [PATCH 40/40] fix: custom translator not receiving updated locale (#919) --- .../LaravelLocalization.php | 1 + tests/CustomTranslatorTest.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/CustomTranslatorTest.php diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 228ece5..7052929 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -194,6 +194,7 @@ public function setLocale($locale = null) } $this->app->setLocale($this->currentLocale); + $this->translator->setLocale($this->currentLocale); // Regional locale such as de_DE, so formatLocalized works in Carbon $regional = $this->getCurrentLocaleRegional(); diff --git a/tests/CustomTranslatorTest.php b/tests/CustomTranslatorTest.php new file mode 100644 index 0000000..8bb7f1e --- /dev/null +++ b/tests/CustomTranslatorTest.php @@ -0,0 +1,33 @@ +app, + $this->app['config'], + $translator, + $this->app['router'], + $this->app['request'], + $this->app['url'] + ); + + $localization->setLocale('es'); + + $this->assertEquals('es', $translator->getLocale()); + } +}