This package makes Laravel's translation work in tandem with frontend localisation frameworks, such as i18next. So you can share the same translation strings between your Laravel backend, and your React or Vue frontend.
Normally, your Laravel translation strings that interpolate variables would look like this:
{
"greeting": "Welcome, :name!"
}
But with this package, you can also define your translation strings like this:
{
"greeting": "Welcome, {{name}}!"
}
Meaning you can share that same translation string with your frontend localisation framework.
It works for pluralisation too. Laravel usually expects pluralisation strings to be delimited by a pipe character, like this:
{
"apples": "There is one apple|There are many apples"
}
But with this package, you can also define your pluralisation strings like this:
{
"apples_one": "There is one apple",
"apples_other": "There are many apples"
}
It's completely backwards compatible with Laravel's syntax too, so your Laravel application will consume either syntax.
You can install the package via composer:
composer require danjohnson95/laravel-i18n-compatibility
In Laravel, you can use your localisation strings exactly as you would normally.
{
"greeting": "Welcome, {{name}}!",
"apples_one": "There is one apple",
"apples_other": "There are many apples"
}
public function index()
{
return response()->json([
'greeting' => __('greeting', ['name' => 'Dan']),
'apples' => trans_choice('apples', ['count' => 1]),
]);
}