Chapter 9 - Laravel Localization

Hello Artisan's, welcome to the 8th chapter of being an Artisanary. In this chapter we'll discuss about Laravel localization. So if you already complete the previous chapters/sections, then you're good to go, if not my recommendation would be please complete the previous chapters. Because we'll use the same old repository that we use in chapter 4. 

Note: Tested on Laravel 10.0

Table of Contents

  1. Understanding the Concept of Localization
  2. Usage of Localization

Understanding the Concept of Localization

Laravel 10 uses a new localization system called "Localizable Strings". This new system allows developers to define translation strings inline, directly in their code, without the need for separate language files.

Suppose, your default locale is  "es" (Spanish), you can create a new es.json file inside the resources/lang directory and add your translations there.

Here's an example of how you can define a translation for a specific string in Spanish:

  1. Create a new file called es.json inside the resources/lang directory if it doesn't already exist.
  2. Inside the es.json file, define your translation string using the key-value format, where the key is the original string and the value is the translated string in Spanish. See the below json file.
resources/lang/es.json
{
    "You're logged in!" : "Bienvenido a mi dashboard!"
}

If you want to add translations for other languages, you can create additional language files with the appropriate language code (e.g. fr.json for French, de.json for German, etc.) and define your translations there. Then you can switch to the desired language by setting the locale option in the config/app.php file.

Usage of Localization

We can use the translation using two different way, one is using trans() and another is using __(). 

Using trans()

//in controllers
trans("You're logged in!") //which will print Bienvenido a mi dashboard!
//in blade file
{{ trans("You're logged in!") }} //which will print Bienvenido a mi dashboard!

Using __()

//in controllers
__("You're logged in!") //which will print Bienvenido a mi dashboard!
//in blade file
{{ __("You're logged in!") }} //which will print Bienvenido a mi dashboard!

We can also give dynamic data if we want by passing an array. Suppose consider the “You're logged in” key, here we also want to show the current logged in user name, then what can we do? First of all we've to make some slight change in es.json file what we define in #step1 as follows

{
    "You're logged in!" : ":name! bienvenido a tu tablero"
}

Then if we translate this key in the blade file the we need to call this by below

 {{ __("You're logged in!",['name' => auth()->user()->name]) }}
 // it'll print Tanvir Ahmed! bienvenido a tu tablero

Good to know that __() method receives up to three parameters. As we already seen two parameters. The third parameter is locale, this is useful when you want to ignore any keywords from being translate. Let's look at the below keyword

{{ __("You're logged in!",['name' => auth()->user()->name],'en') }}
 // it'll print You're logged in!

But as I already seen that if we don't provide the third parameter then we get translatable string as the “Tanvir Ahmed! bienvenido a tu tablero”.

So, it's time to say goodbye for today. We saw the details about Laravel localization. And yes keep up to date with the github repository. That's it for today. See you in my next chapter. Happy coding 🙂.