Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x Set default locale via Middleware #923

Merged
merged 47 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ae105c0
Preserve "locale" cookie value on redirect to localized URL
okovpashko Mar 28, 2020
0c3ba96
Merge branch 'master' into fix-locale-cookie
Dec 1, 2024
443610a
Fix test
Dec 1, 2024
4d825e2
Merge pull request #920 from mcamara/fix-locale-cookie
iwasherefirst2 Dec 1, 2024
af5b239
test: remove redundant url variable (#922)
jordyvanderhaegen Dec 2, 2024
420fb7e
Draft first SetLanguage Middleware.
Dec 13, 2024
4231904
Save readme to README_LEGACY.md
Dec 13, 2024
6cfe4b1
Slowly update Readme to 3.x
Dec 13, 2024
f27c009
Remove outdated caching readme
Dec 13, 2024
43de0b5
Fix currentLocale call
Dec 14, 2024
8a43f34
Remove custom cache
Dec 14, 2024
01084f6
Set current locale in middleware
Dec 14, 2024
5894b52
Define macro route
Dec 14, 2024
af830c1
use Constructor property promotion
Dec 14, 2024
739bca8
Remove setLocale, no longer used.
Dec 14, 2024
b96c0f0
use constructor property promotion
Dec 14, 2024
68ffc0b
Remove set locale. Should be set via middleware.
Dec 14, 2024
2db5da1
Allow to set locale via middleware.
Dec 14, 2024
35d770f
Update usage section
Dec 14, 2024
9e6727c
Remove cache commands
Dec 14, 2024
2495943
Ignore IDE files
Dec 14, 2024
d77f0fa
Merge branch 'master' into 3.x-inital-draft
Dec 14, 2024
e4c24b2
Ignore IDE files
Dec 14, 2024
8cb88f9
fix: macro being registered multiple times
jordyvanderhaegen Dec 14, 2024
0416af8
tests: update route registration to use new localized macro
jordyvanderhaegen Dec 14, 2024
6c9cfd6
Ensure default routes are always present
Dec 14, 2024
0712088
chore: remove redundant exception class
jordyvanderhaegen Dec 14, 2024
a446268
Restrict {locale} parameter to configuration values.
Dec 14, 2024
49ef221
Add tests for LocaleSetMiddleware
Dec 14, 2024
c30b54e
Merge
Dec 14, 2024
b869c8d
Restrict {locale} parameter to configuration values.
Dec 14, 2024
a1ece90
Fix tests
Dec 14, 2024
0d27b01
Remove unintended gitignore file
Dec 14, 2024
3e39b18
chore: cleanup old route caching leftovers
jordyvanderhaegen Dec 14, 2024
720545f
Merge remote-tracking branch 'origin/3.x-inital-draft' into 3.x-inita…
jordyvanderhaegen Dec 14, 2024
2d41e11
Rename middleware
Dec 15, 2024
5dd1cbf
Mark todo for skip question
Dec 15, 2024
4af4d7c
Add default locale
Dec 15, 2024
b6aa26f
Rename test to match testcase
Dec 15, 2024
99402ef
Rename SetLocale Test class
Dec 15, 2024
662e8a8
Use supported locales from LaravelLocalization
Dec 15, 2024
d8ff93e
Avoid code duplications
Dec 15, 2024
dacac57
Restore getCurrentLocaleRegional
Dec 15, 2024
8882d96
Avoid code duplication
Dec 15, 2024
bb60c89
Change visibility of constructor properties
Dec 15, 2024
2bb596e
chore: improve version references
jordyvanderhaegen Dec 18, 2024
5b8ffbb
fix: call to undefined method
jordyvanderhaegen Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .idea/.gitignore
iwasherefirst2 marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 0 additions & 73 deletions CACHING.md

This file was deleted.

109 changes: 61 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes.

This documentation covers version 3.x, compatible exclusively with Laravel 9 and later.
We strongly recommend using this version, as it resolves numerous compatibility issues present in versions 1.x and 2.x,
and seamlessly integrates with Laravel's default caching.
If you need to use an older version, please switch to `legacy` branch by clicking [here](https://github.com/mcamara/laravel-localization/tree/legacy).

iwasherefirst2 marked this conversation as resolved.
Show resolved Hide resolved
The package offers the following:

- Detect language from browser
Expand Down Expand Up @@ -35,25 +40,18 @@ The package offers the following:
- <a href="#changelog">Changelog</a>
- <a href="#license">License</a>

## Laravel compatibility
## Laravel Compatibility

| Laravel Version | laravel-localization Version | PHP Requirement |
|------------------|-----------------------------|--------------------------|
| 9.x - 11.x | 3.x | PHP >= 8.2 |

Laravel | laravel-localization
:-------------|:----------
4.0.x | 0.13.x
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.
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
For older versions (0.x to 2.x) switch to `legacy` branch by clicking [here](https://github.com/mcamara/laravel-localization/tree/legacy).
jordyvanderhaegen marked this conversation as resolved.
Show resolved Hide resolved

## Installation

Install the package via composer: `composer require mcamara/laravel-localization`

For Laravel 5.4 and below it necessary to [register the service provider](/ADDITIONS.md#for-laravel-5.4-and-below).

### Config Files

In order to edit the default configuration you may execute:
Expand All @@ -74,6 +72,8 @@ The configuration options are:
- **utf8suffix** Allow changing utf8suffix for CentOS etc.
- **urlsIgnored** Ignore specific urls.



### Register Middleware

You may register the package middleware in the `app/Http/Kernel.php` file:
Expand Down Expand Up @@ -123,52 +123,65 @@ Add the following to your routes file:

```php
// routes/web.php
Route::localized(function () {
// put your routes here
// Example route..
Route::get('/', function()
{
return __('test.hi');
})->name('route_name_example_hi');

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
Route::get('/', function()
{
return View::make('hello');
});

Route::get('test',function(){
return View::make('test');
});
});
Route::get('/test', function()
{
return __('test.color');
});
}, [\Mcamara\LaravelLocalization\Middleware\LocaleSetLanguage::class]);
```
All routes in this group are localized. The package sets your application locale `App::getLocale()` according to your url. The locale may then be used for [Laravel's localization features](http://laravel.com/docs/localization).

/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
### Example

```
Once you’ve added the localized group to your `routes/web.php` file,
your application supports all locales listed in the `supportedLocales` configuration (e.g., `en` and `es` by default).

Once this route group is added to the routes file, a user can access all locales added into `supportedLocales` (`en` and `es` by default).
For example, the above route file creates the following addresses:
#### If `hideDefaultLocaleInURL` is set to `false`

If `hideDefaultLocaleInURL` is set to `false`, routes are created **once**, using the `{locale}` placeholder in the URL.

Command output from `php artisan route:list`

```
GET|HEAD {locale} ............. route_name_example_hi
GET|HEAD {locale}/test ..............................
```
// Set application language to English
http://url-to-laravel/en
http://url-to-laravel/en/test

// Set application language to Spanish
http://url-to-laravel/es
http://url-to-laravel/es/test
For example, if `en` and `es` are configured as ``supportedLocales`, you can access these URLs:

// Set application language to English or Spanish (depending on browsers default locales)
// if nothing found set to default locale
http://url-to-laravel
http://url-to-laravel/test
```
The package sets your application locale `App::getLocale()` according to your url. The locale may then be used for [Laravel's localization features](http://laravel.com/docs/localization).
// English (locale: 'en')
localhost/en
localhost/en/test

You may add middleware to your group like this:
// Spanish (locale: 'es')
localhost/es
localhost/es/test
```

```php
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function(){ //...
});
#### If `hideDefaultLocaleInURL` is set to `true`
If `hideDefaultLocaleInURL` is set to `true`, each route defined in the `localized` group is created **twice**:

1. **With `{locale}` placeholder**: Allows URLs like `/en` or `/es`.
2. **Without any prefix**: These routes serve as fallback routes for the default locale.

Routes without a locale prefix are automatically named with the `default_lang.` prefix.

Command output from `php artisan route:list`

```
GET|HEAD / ....... default_lang.route_name_example_hi
GET|HEAD test ......................... default_lang.
GET|HEAD {locale} ............. route_name_example_hi
GET|HEAD {locale}/test ..............................
```

### Recommendations
Expand Down
Loading