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/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/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/README.md b/README.md
index 000d16f..33f3514 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,8 @@
# 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 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 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)
@@ -43,10 +43,10 @@ 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 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.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
+ 10.x-11.x (PHP version >= 8.2 required) | 2.0.x
## Installation
@@ -89,7 +89,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,
@@ -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:
@@ -175,7 +192,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.
@@ -186,7 +203,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
@@ -217,7 +234,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
@@ -246,6 +263,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
@@ -289,6 +313,20 @@ 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 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).
@@ -360,11 +398,14 @@ 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:
+> 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
$this->loadCachedRoutes());
+ ...
+ }
+```
+
For more details see [here](CACHING.md).
@@ -490,6 +564,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,
@@ -521,7 +599,7 @@ protected function refreshApplicationWithLocale($locale)
self::setUp();
}
-protected function tearDown()
+protected function tearDown(): void
{
putenv(LaravelLocalization::ENV_ROUTE_KEY);
parent::tearDown();
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/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 c643de4..aa54c99 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": [
@@ -12,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"
+ "php": "^8.2",
+ "laravel/framework": "^10.0|^11.0"
},
"require-dev": {
- "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0",
- "phpunit/phpunit": "6.0.*|^8.0"
+ "orchestra/testbench-browser-kit": "^8.5|^9.0",
+ "phpunit/phpunit": "^10.1"
},
"suggest": {
"ext-intl": "*"
@@ -26,12 +30,16 @@
"test": "vendor/bin/phpunit"
},
"autoload": {
- "classmap": [
- ],
+ "classmap": [],
"psr-0": {
"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
-
-
-
+
-
+
+
diff --git a/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/Facades/LaravelLocalization.php
index 0bc0b9f..e8b633c 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 getLocaleFromMapping(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
{
/**
diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php
index e4000b5..7052929 100644
--- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php
+++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php
@@ -2,9 +2,15 @@
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;
use Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException;
@@ -18,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;
@@ -46,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;
@@ -125,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');
@@ -191,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();
@@ -269,9 +273,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);
@@ -281,6 +282,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);
}
@@ -760,7 +764,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;
@@ -785,7 +789,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()
{
@@ -1036,12 +1040,22 @@ protected function normalizeAttributes($attributes)
*/
protected function getForcedLocale()
{
- return env(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;
+ }
+ });
+ }
}
}
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/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/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();
diff --git a/src/config/config.php b/src/config/config.php
index c04a302..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
@@ -344,4 +346,5 @@
// Defaults to []
'urlsIgnored' => ['/skipped'],
+ 'httpMethodsIgnored' => ['POST', 'PUT', 'PATCH', 'DELETE'],
];
diff --git a/tests/.gitkeep b/tests/.gitkeep
deleted file mode 100644
index e69de29..0000000
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());
+ }
+}
diff --git a/tests/LocalizerTests.php b/tests/LaravelLocalizationTest.php
similarity index 54%
rename from tests/LocalizerTests.php
rename to tests/LaravelLocalizationTest.php
index ca0a7c6..44c00ff 100644
--- a/tests/LocalizerTests.php
+++ b/tests/LaravelLocalizationTest.php
@@ -1,35 +1,20 @@
'Mcamara\LaravelLocalization\Facades\LaravelLocalization',
- ];
- }
-
- public function setUp(): void
- {
- parent::setUp();
- }
-
/**
* Set routes for testing.
*
@@ -133,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);
@@ -149,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');
@@ -173,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()
);
@@ -190,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()
);
@@ -240,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',
[],
[],
[],
@@ -279,7 +264,7 @@ public function testGetLocalizedURL()
$this->assertResponseOk();
$this->assertEquals(
- $this->test_url.'es/acerca',
+ self::$testUrl.'es/acerca',
$crawler->getContent()
);
@@ -288,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'),
[],
[],
[],
@@ -315,40 +300,40 @@ 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);
- 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(
- $this->test_url . 'about?q=2',
+ self::$testUrl . 'about?q=2',
$laravelLocalization->getLocalizedURL()
);
}
- public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute()
+ public function testGetLocalizedURLWithQueryStringAndNotTranslatedRoute(): void
{
- 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',
+ self::$testUrl . 'en/about?q=2',
$laravelLocalization->getLocalizedURL()
);
}
@@ -356,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,
@@ -367,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',
[],
[],
[],
@@ -398,7 +382,7 @@ public function testGetLocalizedUrlForIgnoredUrls() {
$this->assertResponseOk();
$this->assertEquals(
- $this->test_url2.'/skipped',
+ self::$testUrl2.'/skipped',
$crawler->getContent()
);
}
@@ -409,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(
@@ -422,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',
@@ -643,7 +626,7 @@ public function testGetDefaultLocale()
);
}
- public function testGetSupportedLocales()
+ public function testGetSupportedLocales(): void
{
$this->assertEquals(
$this->supportedLocales,
@@ -651,7 +634,7 @@ public function testGetSupportedLocales()
);
}
- public function testGetCurrentLocaleName()
+ public function testGetCurrentLocaleName(): void
{
$this->assertEquals(
'English',
@@ -666,7 +649,7 @@ public function testGetCurrentLocaleName()
);
}
- public function testGetCurrentLocaleRegional()
+ public function testGetCurrentLocaleRegional(): void
{
$this->assertEquals(
'en_GB',
@@ -681,7 +664,7 @@ public function testGetCurrentLocaleRegional()
);
}
- public function testGetCurrentLocaleDirection()
+ public function testGetCurrentLocaleDirection(): void
{
$this->assertEquals(
'ltr',
@@ -697,7 +680,7 @@ public function testGetCurrentLocaleDirection()
);
}
- public function testGetCurrentLocaleScript()
+ public function testGetCurrentLocaleScript(): void
{
app('laravellocalization')->setLocale('es');
$this->refreshApplication('es');
@@ -716,7 +699,7 @@ public function testGetCurrentLocaleScript()
);
}
- public function testGetCurrentLocaleNativeReading()
+ public function testGetCurrentLocaleNativeReading(): void
{
$this->assertEquals(
'English',
@@ -732,7 +715,7 @@ public function testGetCurrentLocaleNativeReading()
);
}
- public function testGetCurrentLocale()
+ public function testGetCurrentLocale(): void
{
$this->assertEquals(
'en',
@@ -753,7 +736,7 @@ public function testGetCurrentLocale()
);
}
- public function testGetSupportedLanguagesKeys()
+ public function testGetSupportedLanguagesKeys(): void
{
$this->assertEquals(
['en', 'es'],
@@ -761,7 +744,7 @@ public function testGetSupportedLanguagesKeys()
);
}
- public function testGetConfigRepository()
+ public function testGetConfigRepository(): void
{
$this->assertEquals(
app('config'),
@@ -769,7 +752,7 @@ public function testGetConfigRepository()
);
}
- public function testCreateUrlFromUri()
+ public function testCreateUrlFromUri(): void
{
$this->assertEquals(
'http://localhost/view/1',
@@ -786,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';
@@ -811,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'],
@@ -832,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';
@@ -864,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',
@@ -881,6 +862,8 @@ public function testSetLocaleWithMapping()
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
}
+
+
public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale()
{
app('router')->group([
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,
+ ];
+ }
+}