Basic Usage
Installation
Install the package using the console command:
Managing locales
Add locales
Now you can add new localizations using the lang:add
console command:
Update locales
Now you can update existing localizations using the lang:update
console command:
We also recommend adding the call code to the post-update-cmd
section of the scripts
block in the composer.json
file:
This allows localization files to be automatically updated after dependency updates, rather than manually running the php artisan lang:update
console command.
Routes
Via Localization Group
This method allows you to easily manage group prefixes using localized routes.
In addition to the main middleware for this group, LocalizationByParameterPrefix
, the following are also involved part: LocalizationByCookie
, LocalizationByHeader
and LocalizationBySession
.
You can use one of three interaction methods:
The final application will register two types of routes - with and without the {locale}
prefix. For example:
URL | Pattern | Name |
---|---|---|
https://example.com/fr/foo/show |
|
|
https://example.com/foo/show |
|
|
The name prefix can be changed in the routes
section of the config/localization.php
file.
Examples
Request:
Response:
Request:
Response:
Request:
Response:
Request:
Response:
Models
Get Translation
To get the value of a property for the current localization, simply make a call to the model property. For example:
If the value of the main locale is missing, the method will return the value of the fallback locale.
You can also use the getTranslation
method:
To get a value for a specific locale, pass the localization code as the second argument. For example:
Set Translations
Single value
To add or update a translation to your application's current locale, simply assign a value to the model property. For example:
You can also set a new value using the setTranslation
method:
These methods will set a new value to the currently active locale. To set the value to a specific locale, pass its code as the third parameter in this method. For example:
It is also possible to use mass filling:
Of course, filling can also be done when creating the model. For example:
Plural value
If you need to quickly fill a property with translations from several localizations, you can use any of the following ways:
You can also assign iterable
objects when bulk populating. For example:
Please note that there is no strict need to indicate exact locations for each of the fields. For example, the example above will populate the title
field for French, English, and German, while the description
field will only set values for French and German.
JSON Fallback
After installing, you need to add a link to the service provider in the providers
section of the config/app.php
settings file:
Now JSON keys will correctly output the value based on the selected localization.