Translations in Laravel.

Flavio Rosselli
Aug 13 · 3 min read

What do you need to know about Laravel Localization?

  1. Configuring The Locale.
  2. Translation files types.
  3. Translation files location.
  4. Functions, Helpers, and Directives available.
  5. How to retrieve the Translation Strings.
  6. Improve the experience using Placeholders.
  7. Pluralization.
  8. How to translate installed package language files.

1. Configuring The Locale

• The default language for your application is stored in the config/app.php configuration file (‘locale’ => ‘en’,).
• You may configure a fallback language, which will be used when the active language does not contain a given translation string (‘fallback_locale’ => ‘en’,).
• You may also change the active language at runtime using the App::setLocale() on the App facade.

2. Translation files types

There are two types of translation files:
• Files that returns an Array of Keyed Strings.
• JSON Translation Strings As Keys.

# array of keyed strings.
return [
'welcome' => 'Welcome to our application'

# translation strings as keys
"I love programming.": "Me encanta programar."

3. Translation files location

Language translation strings are stored in files within the resources/lang directory.
• For Array of Keyed Strings, each language supported by the application should be within a subdirectory.
• For Translation Strings As Keys, if your application has a Spanish translation, you should create a resources/lang/es.json file.


4. Functions, Helpers, and Directives available.

Use the App::getLocale() and App::isLocale() on the App facade to determine the current locale and check if the locale is a given value.
All Functions, Helpers, and Directives are demonstrated in this article.

• App::setLocale()
• App::getLocale()
• App::isLocale()
• @lang()
• __()
• trans()
• trans_choice()

5. How to retrieve the Translation Strings

# welcome translation string from resources/lang/messages.php
# using Array of Keyed Strings
echo __('messages.welcome');

# using Translation Strings as Keys (json)
echo __('I love programming.');

# in Blade, use {{ }} or @lang directive.
{{ __('messages.welcome') }}

If the specified translation string does not exist, the __() will return the translation string key, in this example would return messages.welcome.

6. Improve the experience using Placeholders

You may define placeholders in your translation strings those are prefixed with a : (just for Array of Keyed Strings). To replace the placeholders when retrieving a translation string, pass an array of replacements as the second argument to the __().

return [
'welcome' => 'Welcome to our application',
'welcome' => 'Welcome, :name', // Welcome, dayle
'welcome' => 'Welcome, :NAME', // Welcome, DAYLE
'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle
# using placeholders
echo __('messages.welcome', ['name' => 'dayle']);

7. Pluralization

• Use the pipe character (|) to separate the singular and the plural forms of a string. You may even create more complex pluralization rules for multiple number ranges.
• Use the trans_choice() to retrieve the line with pluralization options”.
• You may also define placeholder attributes in pluralization strings. These placeholders may be replaced by passing an array as the third argument to the trans_choice().

# singular|plural
'apples' => 'There is one apple|There are many apples',

trans_choice('pluralization.apples', 1) // There is one apple
trans_choice('pluralization.apples', 2) // There are many apples

# multiple number ranges
'amount' => '{0} There are none|[1,19] There are some|[20,*] There are many',

trans_choice('pluralization.amount', 0) // There are none
trans_choice('pluralization.amount', 12) // There are some
trans_choice('pluralization.amount', 22) // There are many

# with the :count placeholder
'amount count' => '{0} There are none|{1} There is one|[2,*] There are :count',

trans_choice('pluralization.amount count', 0) // There are none
trans_choice('pluralization.amount count', 1) // There is one
trans_choice('pluralization.amount count', 2) // There are 2

# with placeholders
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago'

trans_choice('pluralization.minutes_ago', 5, ['value' => 5]) // 5 minutes ago

8. How to translate installed package language files

Some packages may ship with their own language files. Instead of changing the package’s core files, you may override them by placing files in the resources/lang/vendor/{package}/{locale} directory.

For example, if you need to override the English translation strings in messages.php for a package named skyrim/hearthfire, you should place a language file at resources/lang/vendor/hearthfire/en/messages.php.

Have a lovely day.

Flavio Rosselli

Written by

Web Developer who lives travelling.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade