- Introduction
- Defining Translation Strings
- Retrieving Translation Strings
- Overriding Package Language Files
Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application. Language strings are stored in files within the resources/translations directory. Within this directory there should be a subdirectory for each language supported by the application:
/resources
/translations
/en-us
messages.ini
/zh-cn
messages.ini
All language files return an array of key-value strings. For example:
welcome=Hello, world
title = %s Demo
email = Email address
password = Password
submit = Submit{note} For languages that differ by territory, you should name the language directories according to the ISO 15897. For example, "en_US" should be used for British English rather than "en-us".
The default language for your application is stored in the config/application.conf configuration file. You may modify this value to suit the needs of your application. You may also change the active language at runtime using the setLocale method in hunt.framework.Simplify.
You may use the getLocale to determine the current locale:
string locale = getLocale();
if (locale == "en-us") {
//
}
Typically, translation strings are stored in files within the resources/translations directory. Within this directory there should be a subdirectory for each language supported by the application:
/resources
/translations
/en-us
messages.ini
/zh-cn
messages.ini
All language files return an array of keyed strings. For example:
welcome=Hello, world
title = %s Demo
email = Email address
password = Password
submit = SubmitFor applications with heavy translation requirements, defining every string with a "short key" can become quickly confusing when referencing them in your views. For this reason, Laravel also provides support for defining translation strings using the "default" translation of the string as the key.
Translation files that use translation strings as keys are stored as INI files in the resources/translations directory. For example, if your application has a Spanish translation, you should create a resources/translations/es/message.ini file:
I-love-programming = Me encanta programar.You may retrieve lines from language files using the trans helper function. The trans method accepts the key of the translation string as its first argument. For example, let's retrieve the welcome translation string from the resources/translations/en-us/messages.ini language file:
writeln(trans("welcome"));If you are using the View template, you may use the {{ }} syntax to echo the translation string:
<title>{{trans("welcome")}}</title>If the specified translation string does not exist, the trans function will return the translation string key. So, using the example above, the trans function would return welcome if the translation string does not exist.
If you wish, you may define placeholders in your translation strings. All placeholders are samme format specifications as std.format. For example, you may define a welcome message with a placeholder name:
welcome=Welcome, %sTo replace the placeholders when retrieving a translation string, pass an array of replacements as the second argument to the __ function:
writeln(trans("welcome", "dayle"));Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override them by placing files (ordered by the file name) in the same directory.