What is Localization in laravel and how implement it?

how to make Localization Applications in laravel

Hi,

We’ll look into localization in Laravel In this post. Laravel makes it simple to add localization. when many nations utilize your app. It is preferable to keep only one instance rather than creating many. The optimum method in this scenario is localization. The user can change the application’s language. Stay with the article, you get a step-by-step guide on localization in Laravel.

Laravel allows you to support multiple languages within your application. They store all language files inside the resources/lang directory. You can create a subdirectory for each language for your application. So the language directory looks below:

 /resources
   /lang
      /en
         messages.php
      /es
         messages.php

All language files return an array of key-value pairs strings. For example:

<?php

  return [
     'welcome' => 'Welcome to our application',
  ];

Configuring the Locale:-

We are setting the default language of the application inside the config/app.php file. You may also change language at runtime using the setLocale method on the App facade.

  Route::get('welcome/{locale}', function ($locale) {
      if (! in_array($locale, ['en', 'es', 'fr'])) {
        abort(400);
      }
   App::setLocale($locale);

   //
  });

If Active language does not contain a translation string, the string will return from the default language file. You can change or configure it inside the config/app.php file.

 'fallback_locale' => 'en',

Determining the current locale:

The getLocale method gets the current locale. The isLocale method checks if a value locale.

 $locale = App::getLocale();

 if (App::isLocale('en')) {
   //
 }

Defining Translation Strings:-

Using Translation strings as keys:

Translation files that use translation strings as keys stored as JSON files in the resources/lang directory. If your application has a Spanish translation, create a resource/lang/es.json file.

 {
      "I love programming.": "Me encanta programar."
 }

Retrieving translation strings:

You may retrieve lines from language files using the _ helper function. The _ method accepts a filename and key name. Some examples using the __ helper method:

 echo __('messages.welcome');

You may also write a whole string that replaces it accordingly.

 echo __('I love programming.');

 {{ __('messages.welcome') }}
 @lang('messages.welcome')

If the translation string does not exist, the __ function will return the translation string key.

Replacing parameters in translation strings:

You may define parameters in your translation strings. We prefix all parameters with ‘:’ (COLON).

 'welcome' => 'Welcome, :name',

Pass an array to __ helper method as the second argument. It will replace parameters when retrieving.

 echo __('messages.welcome', ['name' => 'dayle']);

You define a parameter in capital letters or first letter capital. It will capitalize accordingly on the translation value.

Pluralization:-

You may distinguish singular and plural forms of a string using the “pipe” character.

  'apples' => 'There is one apple|There are many apples',
  'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',

you may use the trans_choice function to retrieve the line for a given count.

 echo trans_choice('messages.apples', 10);

Pass the Third argument to the trans_choice function to replace the passing array.

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

 echo trans_choice('time.minutes_ago', 5, ['value' => 5]);

Overriding Package Language Files:-

Some packages contain their language files. You may override them by placing files in the resources/lang/vendor/{package}/{locale} directory and not changing the core files from the third-party packages.

Any translation strings you don’t override will still load from the package’s original language files.

I hope this article (How To Make Localization Applications In Laravel) helps you better understand the defined translation strings, replace parameters from translation strings in view, and override package language files. For this article, I followed this link.

If you have questions, please leave a comment and I will respond as soon as possible.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers,

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top