Brief introduction
Laravel localization feature allows you to easily implement multi-language support in the application. The default language strings stored in the resources/lang
directory, the directory contains application subdirectories for each language supported:
/ Resources / lang / in messages . php / es messages .php
All language files returns a key-value pair array, for example:
<?php return [ 'welcome' => 'Welcome to LaravelAcademy.org' ];
Locale Configuration Options
The default language in the application configuration file stored config/app.php
in, of course, you can modify the values to meet application needs. You can also use the runtime App
on the facade setLocale
change the current language method:
Route::get('welcome/{locale}', function ($locale) { App::setLocale($locale); // });
You can also configure a "fallback language", when the current language does not contain a given language row alternate language is returned. And the default language, the language is also an alternate configuration file config/app.php
configuration:
'fallback_locale' => 'en',
Determine the current local language
You can use App
the facade getLocale
and the isLocale
method to get the current local language or check for a match with a given local language:
$locale = App::getLocale(); if (App::isLocale('en')) { // }
Definition of translation strings
Use abbreviations keys
Typically, the translation strings stored in a resources/lang
file in the directory, this directory contains the application for each supported language corresponding subdirectory:
/ Resources / lang / in messages . php / es messages .php
All language file with a corresponding abbreviation key returns an array of strings, for example:
<?php // resources/lang/en/messages.php return [ 'welcome' => 'Welcome to LaravelAcademy.org' ];
Use translation strings as keys
For those with severe application of translation requirements, the definition of each string is a "short key" when referenced in the view becomes more and more difficult to understand, and even cause confusion. For this reason, Laravel also supports the use of "default" translated strings as keys to define the translation strings.
Use strings as keys translation translation of documents in a manner JSON file stored in resources/lang
the directory. For example, if your application has a Spanish version of the translation, we need to create a resources/lang/es.json
file:
{ "I love programming.": "Me encanta la programación." }
Get translation strings
You can use the helper __
from the acquired language file line, the method receives the file key and the translation string as the first parameter, for example, from the language files resources/lang/messages.php
acquired in welcome
the corresponding translation string:
echo __('messages.welcome'); echo __('I love programming.');
Of course, if you use the Blade templating engine, you can use the {{ }}
grammar translation strings or use print @lang
command:
{{ __('messages.welcome') }}
@lang('messages.welcome')
If the specified translation string does not exist, __
the function returns a character string translation key, therefore, using the above example, if the translation does not exist, then the string, __
the function returns messages.welcome
.
Note: The
@lang
instructions will not escape any output, use this command, you need to escape full responsibility for the operation of their own output.
Replace translation string parameters
If necessary, you can define placeholders in translation strings, all the placeholder has a :
prefix, for example, you can use placeholders name
to define a welcome
message:
'welcome' => 'Welcome, :name',
To replace the placeholder to obtain the translation strings when passing an alternative array as __
the second argument to the function:
echo __('messages.welcome', ['name' => 'laravel']);
If placeholders are in uppercase, or the first letter is capitalized, then the corresponding incoming values will remain consistent and placeholders format:
'welcome' => 'Welcome, :NAME', // Welcome, LARAVEL 'goodbye' => 'Goodbye, :Name', // Goodbye, Laravel
plural
The complex is a complex issue, because different languages have different rules for complex numbers, by using the pipe character "|", you can distinguish between singular and plural forms of a string:
'apples' => 'There is one apple|There are many apples',
You can also create multiple range of numbers specified translation string, more complex plural rules:
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
After that, you can use the trans_choice
function to obtain the number of rows in a given row language, in this embodiment, since the number of rows is greater than 1, it will return a string of plural Translation:
echo trans_choice('messages.apples', 10);
Can also define a plurality of strings placeholder properties, these placeholders would be passed to trans_choice
the function of the third array parameter substitution:
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
echo trans_choice('time.minutes_ago', 5, ['value' => 5]);
If you want to display delivered to the trans_choice
integer value of the function, you can use :count
placeholders:
'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',
Coverage extension package language files
Some expansion packs may process language files themselves. You can be your own files in resources/lang/vendor/{package}/{locale}
the directory down to cover them and not undermine these core file package to adjust these sentences.
So, for example, if you need to override the called skyrim/hearthfire
extension package messages.php
file of English sentences, you can create a resources/lang/vendor/hearthfire/en/messages.php
file. In this file only need to define what you want covered sentence, the sentence is still not covered by the original language file is loaded from the expansion pack.