Laravel5 Intl Translator
, (*1)
Introduction
Laravel5 Intl Translator uses php-intl extension to provide translation for your application., (*2)
Please mind that this package breaks framework default behaviour for validators., (*3)
Due to MessageFormatter::formatMessage
method, Validator::replacer
method should return array of parameters as key-value pair, instead replacing placeholders in message., (*4)
Besides that app('translator')->get($key)
always returns message in raw format (unparsed). Translated messages are returned by:, (*5)
app('translator')->trans($key)
app('translator')->formatMessage($locale, $message, $params)
Requirements
- Laravel 5.2 or 5.3
- php-intl extension installed
Please feel free to contribute to this package for other Laravel versions support!, (*6)
Installation
If you do not have php-intl extension you can install it by following command (Ubuntu, Debian), (*7)
$ sudo apt-get install php-intl
If you have other OS, you can use it's respective package manager, (*8)
Laravel 5.3
composer require skysplit/laravel5-intl-translation=^2.0
Laravel 5.2
composer require skysplit/laravel5-intl-translation=^1.0
All versions
In your config/app.php
providers
Remove line, (*9)
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
And add line:, (*10)
Skysplit\Laravel\Translation\TranslationServiceProvider::class,
Skysplit\Laravel\Translation\ValidationServiceProvider::class,
Publishing config and language files
Be careful! This will override your existing resources/lang/{lang}
files!
Check Currently adapted locales table to see which files could be overriden., (*11)
php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --force
If you would like to publish only config, (*12)
php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --tag=config
If you would like to publish only one language files set, (*13)
php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --force --tag="lang.{locale}[,lang.{other_locale}]"
Currently adapted locales
Locale |
Published files |
en |
auth.php , validation.php
|
pl |
auth.php , pagination.php , passwords.php , validation.php
|
Usage examples
Both trans()
and trans_choice()
helper functions use this translator, so the only thing you have to change is your language files., (*14)
For detailed documentation please visit php's MessageFormatter docs and links related there, (*15)
Placeholders
app/resources/lang/en/custom.php
, (*16)
return [
'placeholder' => 'Hello there, {username}!'
]
view.blade.php
, (*17)
{{ trans('custom.placeholder', ['username' => 'Jane']); }}
Returns, (*18)
Hello there, Jane!
Select
app/resources/lang/en/custom.php
, (*19)
return [
'select' => '{gender, select, male{He} female{She} other{It}} has two legs and is {gender}!'
]
view.blade.php
, (*20)
{{ trans('custom.select', ['gender' => 'male']); }}
{{ trans('custom.select', ['gender' => 'female']); }}
{{ trans('custom.select', ['gender' => 'penguin']); }}
Returns, (*21)
He has two legs and is male!
She has two legs and is female!
It has two legs and is penguin!
Plurals
app/resources/lang/en/custom.php
, (*22)
return [
'plural' => 'Jon has {n, plural, =0{no apples} one{# apple} other{# apples}}'
]
view.blade.php
, (*23)
{{ trans_choice('custom.plural', 0); }}
{{ trans_choice('custom.plural', 1); }}
{{ trans_choice('custom.plural', 2); }}
Returns, (*24)
Jon has no apples
Jon has 1 apples
Jon has 2 apples
Instead of trans_choice()
you can you use trans()
helper as well., (*25)
resources/lang/en/custom.php
, (*26)
return [
'custom.plural' => 'Jon has {0, plural, =0{no apples} one{# apple} other{# apples}}, {grapes, plural, =0{no grapes} one{# grape} other{# grapes} and {oranges, plural, =0{no oranges} one{# orange} other{# oranges}}'
];
view.blade.php
, (*27)
{{ trans('custom.plural', [3, 'grapes' => 1, 'oranges' => 0]) }}
Returns, (*28)
Jon has 3 apples, 1 grape and no oranges
As you can see, the only thing trans_choice()
do is passing first argument as n
parameter to trans()
helper., (*29)
Plural offset
You can set offset for your plural rules. Consider this example:, (*30)
You {n, plural, offset:1 =0{do not like this yet} =1{liked this} one{and one other person liked this} other{and # others liked this}}
Result:, (*31)
You do not like this yet // n = 0
You liked this // n = 1
You and one other person liked this // n = 2
You and 2 others liked this // n = 3
You and 3 others liked this // n = 4
Plural rule are often very complex for languages. Intl does handle it for you.
For example in Polish few
rule is applied when n % 10 = 2..4 and n % 100 != 12..14
, while many
rule is applied when n != 1 and n % 10 = 0..1
or n % 10 = 5..9
or n % 100 = 12..14
.
In Serbian =1
will match when n = 1
, but one
will apply when n = 1, 21, 31, 41
etc., (*32)
Remember! You always have to provide other
rule for plural translations., (*33)
For more details about pluralization please visit CLDR Plural Rules specificaton and CLDR Language plural rules., (*34)
PHP's MessageFormatter also supports ordinal, spellout, number, date, time and duration formatting.
For detailed information please visit this great Yii2 Framework i18n Guide which covers every intl topic wonderfully., (*35)