Warning
This package is abandoned, you should avoid using it.
Use localization feature of laravel/framework instead.
This is a custom package designed for Laravel Eloquent. It provides helpers used to manage flat JSON files for localisation.
Configure composer repository:
composer config repositories.healthengine/laravel-i18n git git@github.com:HealthEngineAU/laravel-i18n.git
Install:
composer require healthengine/laravel-i18n
Update providers in config/app.php
to replace Laravel's stock translation provider with the one of this library:
<?php
use Illuminate\Support\ServiceProvider;
return [
// ...
'providers' => ServiceProvider::defaultProviders()->merge([
// ...
])->replace([
Illuminate\Translation\TranslationServiceProvider::class => Healthengine\I18n\TranslationServiceProvider::class,
]),
// ...
];
Publish resources:
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,
];