Skip to content

Commit 0c3ba96

Browse files
author
Adam Nielsen
committed
Merge branch 'master' into fix-locale-cookie
2 parents ae105c0 + a19ddcc commit 0c3ba96

21 files changed

+595
-322
lines changed

.github/workflows/run-tests.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: true
14+
matrix:
15+
os: [ubuntu-latest]
16+
php: [8.4, 8.3, 8.2]
17+
laravel: [11.*, 10.*]
18+
stability: [prefer-lowest, prefer-stable]
19+
include:
20+
- laravel: 11.*
21+
testbench: 9.*
22+
- laravel: 10.*
23+
testbench: 8.*
24+
25+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
26+
27+
steps:
28+
- name: Checkout code
29+
uses: actions/checkout@v4
30+
31+
- name: Setup PHP
32+
uses: shivammathur/setup-php@v2
33+
with:
34+
php-version: ${{ matrix.php }}
35+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
36+
coverage: none
37+
38+
- name: Setup problem matchers
39+
run: |
40+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
41+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
42+
43+
- name: Install dependencies
44+
run: |
45+
composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.64" "orchestra/testbench-browser-kit:${{ matrix.testbench }}" --no-interaction --no-update
46+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
47+
48+
- name: Execute tests
49+
run: vendor/bin/phpunit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.lock
66
composer.phar
77
Thumbs.db
88
phpunit.xml
9+
.phpunit.cache

.travis.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

CACHING.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ If you want to cache the routes in all languages, you will need to use special A
66

77
For the route caching solution to work, it is required to make a minor adjustment to your application route provision.
88

9+
10+
**before laravel 11**
911
In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait:
1012

1113
```php
@@ -14,7 +16,23 @@ class RouteServiceProvider extends ServiceProvider
1416
{
1517
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
1618
```
19+
**after laravel 11**
20+
In your App's `AppServiceProvider`, use the `CachedTranslatedRouteLoader` class in register method:
1721

22+
```php
23+
<?php
24+
class AppServiceProvider extends ServiceProvider
25+
{
26+
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
27+
/**
28+
* Bootstrap any application services.
29+
*/
30+
public function boot(): void
31+
{
32+
RouteServiceProvider::loadCachedRoutesUsing(fn() => $this->loadCachedRoutes());
33+
...
34+
}
35+
```
1836

1937
## Usage
2038

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### 2.0.0
2+
- Support [contextual binding](https://laravel.com/docs/container#contextual-binding) ([#879](https://github.com/mcamara/laravel-localization/pull/879))
3+
4+
For guidance on the upgrade process, please refer to the [UPGRADING.md](/UPGRADING.md) file.
5+
16
### 1.4.0
27
- Added compatibility with Laravel 6
38

README.md

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Laravel Localization
22

3-
[![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)
4-
5-
[![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)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/mcamara/laravel-localization.svg?style=flat-square)](https://packagist.org/packages/mcamara/laravel-localization)
5+
![GitHub Actions](https://github.com/mcamara/laravel-localization/actions/workflows/run-tests.yml/badge.svg)
66
[![Open Source Helpers](https://www.codetriage.com/mcamara/laravel-localization/badges/users.svg)](https://www.codetriage.com/mcamara/laravel-localization)
77
[![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)
88

@@ -43,10 +43,10 @@ The package offers the following:
4343
4.1.x | 0.13.x
4444
4.2.x | 0.15.x
4545
5.0.x/5.1.x | 1.0.x
46-
5.2.x-5.4.x (PHP 7 not required) | 1.2.x
47-
5.2.x-5.8.x (PHP 7 required) | 1.3.x
48-
5.2.0-6.x (PHP 7 required) | 1.4.x
49-
5.2.0-7.x (PHP 7 required) | 1.5.x
46+
5.2.x-5.4.x (PHP 7 not required) | 1.2.
47+
5.2.0-6.x (PHP version >= 7 required) | 1.4.x
48+
6.x-10.x (PHP version >= 7 required) | 1.8.x
49+
10.x-11.x (PHP version >= 8.2 required) | 2.0.x
5050

5151
## Installation
5252

@@ -89,7 +89,7 @@ class Kernel extends HttpKernel {
8989
*
9090
* @var array
9191
*/
92-
protected $routeMiddleware = [
92+
protected $middlewareAliases = [
9393
/**** OTHER MIDDLEWARE ****/
9494
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
9595
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
@@ -100,6 +100,23 @@ class Kernel extends HttpKernel {
100100
}
101101
```
102102

103+
If you are using Laravel 11, you may register in `bootstrap/app.php` file in closure `withMiddleware`:
104+
105+
```php
106+
return Application::configure(basePath: dirname(__DIR__))
107+
// Other application configurations
108+
->withMiddleware(function (Middleware $middleware) {
109+
$middleware->alias([
110+
/**** OTHER MIDDLEWARE ALIASES ****/
111+
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
112+
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
113+
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
114+
'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
115+
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class,
116+
]);
117+
})
118+
```
119+
103120
## Usage
104121

105122
Add the following to your routes file:
@@ -175,7 +192,7 @@ and `useAcceptLanguageHeader` in `config/laravellocalization.php`:
175192

176193
Whenever a locale is present in the url, it will be stored in the session by this middleware.
177194

178-
In there is no locale present in the url, then this middleware will check the following
195+
If there is no locale present in the url, then this middleware will check the following
179196

180197
- If no locale is saved in session and `useAcceptLanguageHeader` is set to true, compute locale from browser and redirect to url with locale.
181198
- 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
186203

187204
Similar to LocaleSessionRedirect, but it stores value in a cookie instead of a session.
188205

189-
Whenever a locale is present in the url, it will be stored in the session by this middleware.
206+
Whenever a locale is present in the url, it will be stored in the cookie by this middleware.
190207

191208
In there is no locale present in the url, then this middleware will check the following
192209

@@ -217,7 +234,7 @@ aswell as the `hideDefaultLocaleInURL` and [Translated Routes](#translated-route
217234

218235
```php
219236
// If current locale is Spanish, it returns `/es/test`
220-
<a href="{{ LaravelLocalization::localizeUrl('(/test)') }}">@lang('Follow this link')</a>
237+
<a href="{{ LaravelLocalization::localizeUrl('/test') }}">@lang('Follow this link')</a>
221238
```
222239

223240
#### Get localized URL for an specific locale
@@ -246,6 +263,13 @@ Returns a route, [localized](#translated-routes) to the desired locale. If the t
246263
// Returns /es/acerca
247264
{{ LaravelLocalization::getURLFromRouteNameTranslated('es', 'routes.about') }}
248265
```
266+
**Example of a localized link using routes with attributes**
267+
268+
```php
269+
// An array of attributes can be provided.
270+
// Returns /en/archive/ghosts, /fr/archive/fantômes, /pt/arquivo/fantasmas, etc.
271+
<a href="{{ LaravelLocalization::getURLFromRouteNameTranslated( App::currentLocale(), 'routes.archive', array('category' => 'ghosts')) }}">Ghost Stories</a>
272+
```
249273

250274

251275
### Get Supported Locales
@@ -289,6 +313,20 @@ Return current locale's name as string (English/Spanish/Arabic/ ..etc).
289313
{{ LaravelLocalization::getCurrentLocaleName() }}
290314
```
291315

316+
### Get Current Locale Native Name
317+
Return current locale's native name as string (English/Español/عربى/ ..etc).
318+
319+
```php
320+
{{ LaravelLocalization::getCurrentLocaleNative() }}
321+
```
322+
323+
### Get Current Locale Regional Name
324+
Return current locale's regional name as string (en_GB/en_US/fr_FR/ ..etc).
325+
326+
```php
327+
{{ LaravelLocalization::getCurrentLocaleRegional() }}
328+
```
329+
292330
### Get Current Locale Direction
293331

294332
Return current locale's direction as string (ltr/rtl).
@@ -360,11 +398,14 @@ Note that Route Model Binding is supported.
360398
You may translate your routes. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish)
361399
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:
362400

363-
It is necessary that at least the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#LaravelLocalizationRoutes)).
401+
It is necessary that at least the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#installation)).
364402

365403
For each language, add a `routes.php` into `resources/lang/**/routes.php` folder.
366404
The file contains an array with all translatable routes. For example, like this:
367405

406+
> 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`).
407+
> If your project has `lang` folder in the root, you must add a `routes.php` into `lang/**/routes.php` folder.
408+
368409
```php
369410
<?php
370411
// resources/lang/en/routes.php
@@ -461,7 +502,40 @@ To cache your routes, use:
461502
php artisan route:trans:cache
462503
```
463504

464-
... instead of the normal `route:cache` command.
505+
... instead of the normal `route:cache` command. Using `artisan route:cache` will **not** work correctly!
506+
507+
For the route caching solution to work, it is required to make a minor adjustment to your application route provision.
508+
509+
**before laravel 11**
510+
511+
In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait:
512+
513+
```php
514+
<?php
515+
class RouteServiceProvider extends ServiceProvider
516+
{
517+
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
518+
```
519+
520+
**after laravel 11**
521+
522+
In your App's `AppServiceProvider`, use the `CachedTranslatedRouteLoader` class in register method:
523+
524+
```php
525+
<?php
526+
class AppServiceProvider extends ServiceProvider
527+
{
528+
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
529+
/**
530+
* Bootstrap any application services.
531+
*/
532+
public function boot(): void
533+
{
534+
RouteServiceProvider::loadCachedRoutesUsing(fn() => $this->loadCachedRoutes());
535+
...
536+
}
537+
```
538+
465539

466540

467541
For more details see [here](CACHING.md).
@@ -490,6 +564,10 @@ will not work. Instead, one has to use
490564
</form>
491565
```
492566

567+
568+
Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored'
569+
to prevent of processing this type of requests
570+
493571
### MethodNotAllowedHttpException
494572

495573
If you do not localize your post url and use a redirect middleware,
@@ -521,7 +599,7 @@ protected function refreshApplicationWithLocale($locale)
521599
self::setUp();
522600
}
523601

524-
protected function tearDown()
602+
protected function tearDown(): void
525603
{
526604
putenv(LaravelLocalization::ENV_ROUTE_KEY);
527605
parent::tearDown();

UPGRADING.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## From v1 to v2
2+
This package now uses [dependency injection](https://laravel.com/docs/container#introduction) to retrieve dependencies from the container.
3+
4+
This modification is a breaking change, especially if you had made extensions to the `__construct` method within the `Mcamara\LaravelLocalization\LaravelLocalization` class.
5+
You may now use depdency injection in your own implementation and forward the dependencies to the parent constructor.
6+
```php
7+
use Mcamara\LaravelLocalization\LaravelLocalization;
8+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
9+
use Illuminate\Contracts\Foundation\Application;
10+
use Illuminate\Contracts\Routing\UrlGenerator;
11+
use Illuminate\Contracts\Translation\Translator;
12+
use Illuminate\Http\Request;
13+
use Illuminate\Routing\Router;
14+
15+
class MyLaravelLocalization extends LaravelLocalization
16+
{
17+
public function __construct(
18+
mixed $myCustomVariable,
19+
Application $app,
20+
ConfigRepository $configRepository,
21+
Translator $translator,
22+
Router $router,
23+
Request $request,
24+
UrlGenerator $url
25+
) {
26+
parent::__construct($app, $configRepository, $translator, $router, $request, $url);
27+
}
28+
}
29+
```
30+
31+
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.
32+
```diff
33+
use Mcamara\LaravelLocalization\LaravelLocalization;
34+
35+
-$this->app->singleton(LaravelLocalization::class, function () {
36+
- return new MyLaravelLocalization();
37+
-});
38+
+$this->app->singleton(LaravelLocalization::class, MyLaravelLocalization::class);
39+
```
40+
41+
For more information, please see the following PR [#879](https://github.com/mcamara/laravel-localization/pull/879/files)

circle.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)