This is a custom package designed for Laravel Eloquent. It provides helpers used to manage flat JSON files for localisation.
- Install the package:
composer require healthengine/laravel-i18n
- In
config/app.php
:- Replace the
Illuminate\Support\TranslationServiceProvider
withHealthEngine\I18n\TranslationServiceProvider
- Replace the
- Run
php artisan vendor:publish --provider="HealthEngine\I18n\TranslationServiceProvider"
Each language can have multiple JSON translation files.
resources/
lang/
en/
base.json
base.generated.json
other.json
zh-cn/
base.json
base.generated.json
You can define strings in your language files using i18next
style placeholders.
{
"my.greeting": "Hello, {{name}}!"
}
and then use:
i18n('greeting', ['name' => 'Bernadette'])
// -> "Hello, Bernadette!"
Basic support for HTML markup placeholders:
{
"text.with.link": "Hi, <0>{{name}}</0>. Click <1>here</1> to continue."
}
i18n('text.with.link', ['name' => 'Bernadette'], ['<b>', '<a href="#target" />'])
// -> 'Hi, <b>Bernadette</b>. Click <a href="#target">here</a> to continue.'
You can add any of the attached middlwares to associate languages with requests:
- AcceptLanguage - Set the language from the HTTP
Accept-Language
header. - HasLanguage - Use the language from the request parameter
lang
(and store it as a cookie). - DetectLanguage - Automatically determine the best language.
protected $routeMiddleware = [
'accept-lang' => \HealthEngine\I18n\Middleware\AcceptLanguage::class,
'has-lang' => \HealthEngine\I18n\Middleware\HasLanguage::class,
'detect-lang' => \HealthEngine\I18n\Middleware\DetectLanguage::class,
];