localization
- How to use short keys
- Retrieve Translate string
- Rewrite the language file of the expansion pack
Introduction
Laravel localization feature provides an easy way to retrieve strings from different language files, allowing your application to better support multi-language development. Language files are placed in the
resources/lang
directory by default. In this directory, the corresponding language files are stored in the corresponding subdirectories, for example:/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' ];
Local Settings
The default language settings for an application are saved in the
config/app.php
configuration file. You can modify the current settings as needed, and you can also use thesetLocale
method of theApp
Facade to dynamically change the language used during application running:Route::get('welcome/{locale}', function ($locale) { App::setLocale($locale); // });
You can also set "Backup language", which will be used when the current language does not contain the given translation string. Like the default language, the alternate language can also be set in the
config/app.php
configuration file:'fallback_locale' => 'en',
Determine the current locale
you You can use the
getLocale
andisLocale
methods of theApp
Facade to determine the current locale or check whether the locale is a given value:$locale = App::getLocale();if (App::isLocale('en')) { // }
Define translation string
Use short keys
Usually, translation strings are stored in files in the
resources/lang
directory. In this directory, there should be a corresponding subdirectory for 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 // resources/lang/en/messages.php return [ 'welcome' => 'Welcome to our application' ];
Use translation strings as keys
For applications with a large number of translation requirements, if each translation statement must use "short keys" one by one. definition, it can easily get confusing when you try to reference these "short keys" in views. Therefore, Laravel also supports using the string "default" translation as a keyword to define a translation string.
Translation files using translation strings as keys are stored in JSON format in the
resources/lang
directory. For example, if your app has Spanish translations, you should create a newresources/lang/es.json
file in this directory:{ "I love programming.": "Me encanta programar." }
Retrieve the translation string
You can use the helper function
__
to retrieve from the language file,__
The function accepts the file name plus the key name where the translation string is located as its first argument. For example, we want to retrieve the translation stringwelcome
in theresources/lang/messages.php
language file:echo __('messages.welcome'); echo __('I love programming.');
If you are using the Blade template engine, you can Use the
{{ }}
syntax in the view file or use the@lang
directive to print the translation string:{{ __('messages.welcome') }} @lang('messages.welcome')
If the specified translation string does not exist, then
__
The function will directly return the key name of the translation string. Therefore, if the key-value pair corresponding to the translation string in the above example does not exist, the__
function will directly returnmessages.welcome
.{note}
@lang
The directive does not escape any output. When you use this directive, you must be entirely responsible for escaping the output yourself.Parameter replacement in the translation string
If needed, you can Define placeholders. All placeholders have a
:
prefix. For example, you can define a welcome message using the placeholder name:'welcome' => 'Welcome, :name',
You can pass an array as the second parameter in the
__
function, which will replace the value in the array into In the placeholder of the translation string:echo __('messages.welcome', ['name' => 'dayle']);
If your placeholder contains the first letter in capital letters or all capital letters, the translated content will also be capitalized accordingly:
'welcome' => 'Welcome, :NAME', // Welcome, DAYLE 'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle
Plural numbers
Plural numbers are a complicated issue, because different languages have different rules for plural numbers. Use 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 even create more complex pluralization rules, specifying translation strings for multiple ranges of numbers:
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
After defining a translation string with plural options, you can use the
trans_choice
function to retrieve the content of a given "quantity". For example, setting "Total Number" to 10 corresponds to the quantity range 1 to 19, so you will get the plural statement There are some:echo trans_choice('messages.apples', 10);
You can also insert placeholders in the plural string.
trans_choice
The array passed in the third parameter of the function will replace the placeholder:'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago', echo trans_choice('time.minutes_ago', 5, ['value' => 5]);
If you want the "number" parameter passed to the
trans_choice
function to be displayed in In the translation string, you can use the:count
placeholder:'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',
Rewrite the language file of the expansion package
Some expansion packages may come with their own language files. You can override them by placing files in the
resources/lang/vendor/{package}/{locale}
directory instead of directly modifying the extension package's core files.For example, when you need to rewrite the English language file
messages.php
of theskyrim/hearthfire
extension package, you need to store the file asresources/lang /vendor/hearthfire/en/messages.php
. In this file, you only need to define the translation strings you want to modify. Any translation strings that have not been overridden will still be loaded from the expansion pack's original language files.