In this modern era, the internet becomes the main point for business and helps business to grow for goods and service. Although not everyone speaks or understands English and many countries have more than one official language. For example, Canada has two languages French, English 21.3% of Canadians speaks the French language.

Nowadays search engines like Google, MSN, Yahoo developing the capacity to run searches in foreign languages. The multilingual web app has many advantages like targeting more customer across the globe, cost-effective tool for marketing, beating competitors and stay ahead in the competition.

Laravel’s localization features provide a convenient way to retrieve/display data in various languages and easily supports different language to be used in the application. Laravel has a very convenient directory structure to store language strings. All these languages are stored in resources/lang directory.

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

All language files return an array of keyed strings. For example:
English language message file,

<?php
/* resources/lang/en/messages.php */
return [
   /* ’Key’ => ‘Message string’ */
   ‘welcome’ => 'Welcome to homepage'
];
?>

German language message file,

<?php
/* resources/lang/de/messages.php */
return [
   /* ’Key’ => ‘Message string’ */
   ‘welcome’ => 'Willkommen auf der Startseite'
];
?>

 

Configuring The Locale

The default language for your application is stored in the config/app.php configuration file. Of course, 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 on the App facade:

Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale); //Sets local languag
// code
});

You may configure a “fallback language”, which will be used when the active language does not contain a given translation string. Like the default language, the fallback language is also configured in the config/app.php configuration file:

'fallback_locale' => 'en',

 

Retrieving Translation Strings

To retrieve lines from language files using the __ helper function. The __ method accepts the file and key of the translation string as its first argument. For example, let’s retrieve the welcome translation string from the resources/lang/messages.php language file:

echo __('messages.welcome');

if you are using the Blade templating engine, you may use the {{ }} syntax to echo the translation string or use the @lang directive:

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

Replacing Parameters In Translation Strings

If you wish, you may define placeholders in your translation strings. All placeholders are prefixed with a :. For example, you may define a welcome message with a placeholder name:

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

To replace the placeholders when retrieving a translation string, pass an array of replacements as the second argument to the __ function:

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

If your placeholder contains all capital letters or only has its first letter capitalized, the translated value will be capitalized accordingly:

'welcome' => 'Welcome, :NAME', // Welcome, John
'goodbye' => 'Goodbye, :Name', // Goodbye, John

 

How Let’s Nurture helps with building Multilingual web app development?
Let’s Nurture, a leading IT company is known for custom web application development across the globe and has got the expertise in providing many e-commerce, cms solutions based applications with multilingual support. We at Let’s Nurture providing the solution for your business requirement to make easy your business processes and business growth with the latest technology. Still, if you want to know more about multilingual web app development and know to choose the right platform for your app you can contact our Let’s Nurture experts and get a free consultation.

krunal sojitra

Giving up is always an option, but it's never MY CHOICE.

Want to work with us? We're hiring!