Localization of view rendering: enable your application to easily support multiple languages
brief introduction
Larave's localization feature allows you to easily implement multilingual support in applications. The language string is stored in resources/lang
The directory contains subdirectories of each language supported by the application: /resources /lang /en messages.php /es messages.php
All language files return an array of key value pairs, for example: <? php return [ 'welcome' => 'Welcome to our application' ];
Configure Locale options
The application default language is stored in the configuration file config/app.php
Of course, you can modify the value to meet the application needs. You can also use App
On the facade setLocale
Method to change the current language: Route::get('welcome/{locale}', function ($locale) { App::setLocale($locale); // });
You can also configure an "alternate language". When the current language does not contain a given language line, the alternate language will be returned. Like the default language, the alternate language is also in the configuration file config/app.php
Configuration in: 'fallback_locale' => 'en',
Judge the current local language
You can use App
On the facade getLocale
and isLocale
Method to obtain the current local language or check whether it matches the given local language:
$locale = App::getLocale(); if (App::isLocale('en')) { // }
Define translation string
Use Abbreviation Keys
Typically, translation strings are stored in resources/lang
In the files under the directory, this directory contains the subdirectories corresponding to each language supported by the application: /resources /lang /en messages.php /es messages.php
All language files return string arrays with corresponding abbreviation keys, for example: <? php // resources/lang/en/messages.php return [ 'welcome' => 'Welcome to our application' ];
Use translation string as key
For applications that have heavy requirements on translation, defining a "short key" for each string will become more and more difficult to understand, or even cause confusion, when it is referenced in the view. For this reason, Larvel also supports the use of the "default" translation string as a key to define the translation string. The translation file using the translation string as the key is stored in the JSON file resources/lang
Directory. For example, if your application has a Spanish translation, you need to create a resources/lang/es.json
File:
{ "I love programming.": "Me encanta la programación." }
Get translation string
You can use auxiliary functions __
Get the line from the language file. This method receives the key of the file and the translation string as the first parameter. For example, we can get the line from the language file resources/lang/messages.php
Get in welcome
Corresponding translation string: echo __('messages.welcome'); echo __('I love programming.');
Of course, if you use Blade template engine , you can use {{ }}
Syntax Print translation string or use @lang
Directives: {{ __('messages.welcome') }} @lang('messages.welcome')
If the specified translation string does not exist, __
The function will return the key of the translated string. So, using the above example, if the translated string does not exist, __
The function will return messages.welcome
。
Replace parameters in the translation string
If necessary, you can define placeholders in the translation string. All placeholders have one :
Prefix, for example, you can use placeholders name
Define a welcome
Message: 'welcome' => 'Welcome, :name',
To replace the placeholder when getting the translation string, pass a replacement array as __
The second parameter of the function: echo __('messages.welcome', ['name' => 'laravel']);
If the placeholders are all in uppercase, or the first letter is uppercase, the corresponding incoming values will also keep the same format as the placeholders: 'welcome' => 'Welcome, :NAME', // Welcome, LARAVEL 'goodbye' => 'Goodbye, :Name', // Goodbye, Laravel
complex
Complex number is a complex problem, because different languages have different rules for complex number. By using the pipe character "|", you can distinguish the singular and plural forms of a string: 'apples' => 'There is one apple|There are many apples',
You can also create more complex complex rules that specify translation strings for multiple numeric ranges: 'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
After that, you can use trans_choice
The function obtains the language lines with a given number of lines. In this example, since the number of lines is greater than 1, the plural form of the translation string will be returned: echo trans_choice('messages.apples', 10);
Overwrite the language file of the Vendor package
Some expansion packs may process language files themselves. You can put your own files in resources/lang/vendor/{package}/{locale}
Directory down to cover them rather than destroy the core files of these packages to adjust these sentences. So, for example, if you need to override the name skyrim/hearthfire
In the expansion pack messages.php
The English sentence in the file can create a resources/lang/vendor/hearthfire/en/messages.php
File. In this file, you only need to define the sentences you want to overwrite, and the sentences not covered are still loaded from the original language file of the expansion package.