Create Multi Language Website in PHP Using Laravel Framework

Do you want to create a multi-language website in PHP? A multi-languages website helps to increase audiences from several regions. In this article, we study how to build a multi-language website in PHP using the Laravel Framework.

Laravel has a built-in feature of Localization. Language localization is the process of translating content into different languages. Using this built-in feature of Laravel, we can add support for multi-languages to the Laravel website.

In Laravel, you can easily retrieve strings in various languages, through which you can give support to multiple languages in the application.

There are 2 ways you can define translation strings in Laravel – using short keys and using translation strings as keys. Let’s take a look at it one by one.

Laravel Translation Using Short Keys

To get started, you should first know the directory structures used for translation in Laravel.

Let’s say you need to add support for the ‘German’ language. So, create a ‘de’ directory under the ‘resources/lang’ folder.

Laravel Translation

It is recommended to use folder name as per the territory according to ISO values. You can get list of codes for languages here.

Create a file messages.php under the ‘de’ folder. This file contains an array of keyed strings. Below is the example where we are using German translation for the English word ‘welcome’.

<?php

return [
    'welcome' => 'Herzlich willkommen',
];

Here ‘welcome’ is a user-defined key. This key will be used to retrieve translation strings in the German language. You can add as many keys in this file. While adding keys, you should use unique keys in the array. Duplicate keys will result in a wrong translation.

The above process is easy if your website does not have much content. But, if your website is quite rich in content, then keeping unique keys will be difficult to handle. In such cases, go for the second option of passing translation strings as keys.

Using Translation Strings as Keys

In this approach, you don’t need to pass a unique key. What you need to do is create a file de.json under the ‘resources/lang’ directory. Next, in this JSON file, pass the translation string as keys.

For instance, you want a German translation of the English sentence ‘How are you?’ then in de.json file add the below pair.

{
    "How are you?": "Wie geht es dir?"
}

The translation string key should be exactly what you want to translate for. This will be much easier as in the code you have to pass a translation string directly.

Set the Language for Laravel Website

When we install Laravel, the default language set is ‘en’. To change the default language, open the config/app.php file. Find the key ‘locale’ and set the language code. In our case, it will be 'locale' => 'de'.

Alternatively, you can set the language runtime using the setLocale method of App facade. In the below code, I set the language in the constructor.

use App;
public function __construct() {
    App::setLocale('de');
}

Get Translation Strings and Replace It

To get the translated text from language files, Laravel provided a helper method ‘__’. In the below code I am fetching translation strings from both messages.php and de.json in the blade file.

{{ __('messages.welcome') }} //Output, Herzlich willkommen

{{ __('How are you?') }} //Output, Wie geht es dir?

In the controller you can get the translation string using the echo function.

echo __('messages.welcome');
echo __('How are you?');

Placeholders in Translation Strings

Laravel also explores translation features where you can pass placeholders in the translation strings.

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

Or in de.josn file,

{
    "How are you?": "Wie geht es dir, :name?"
}

To replace placeholders in the application we need to write code as follows.

{{ __('messages.welcome', ['name' => 'John']) }}

{{ __('How are you?', ['name' => 'John']) }}

If we write a placeholder as ‘:NAME’ the output would be ‘JOHN’ and for ‘:Name’ output is ‘John’.

I hope you understand how to create a multi-language website in PHP using the Laravel framework. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Create Multi Language Website in PHP Using Laravel Framework

  1. Could you tell me how to create multiple languages in laravel by using voyager admin framework and metronic theme package?

Leave a Reply

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