Do you want to create a multi-language website in PHP? A multi-language website helps to increase audiences from several regions. The Laravel framework which is built on PHP can help you to build such websites easily. In this article, we study how to build a multi-language website using the Laravel Framework.
Laravel comes with a feature called Localization at its core. Language localization is the process of translating content into different languages. Using this feature, we can add support for multi-languages to the Laravel website.
In Laravel, you can add and retrieve translation strings in various languages. Through these translation strings, you will 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 them one by one.
Define Translation Strings 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. For this, create a de
directory under the resources/lang
folder. Here the code ‘de’ is used for German territory.
For Localization, it is recommended to use folder names as per the territory according to ISO values. You can get a list of codes for various languages here.
Next, create a file messages.php
under the de
folder. This file contains an array of keyed strings. As an example, I am adding the German translation for the English word ‘welcome’.
<?php
return [
'welcome' => 'Herzlich willkommen',
];
The ‘welcome’ is a user-defined key. This key will be used to retrieve translation strings in the German language. We will see this retrieval in the later part of the tutorial. You can add as many keys in this file with their respective translation. While adding keys, you must use unique names. 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 for each translation will be difficult. 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. In this JSON file, pass the translation string as a key and their translation as a value.
For instance, if you want a German translation of the English sentence ‘How are you?’ then in the de.json
file add the below pair.
{
"How are you?": "Wie geht es dir?"
}
The translation string key should be exactly what you wish to translate. This will be much easier as in the code you will directly pass a translation string.
Set the Locale for Laravel Website
When we install Laravel, the default application language is set to ‘en’. To change this default language, open the config/app.php
file. In this file, you will find the 'locale' => 'en'
. Set here another language code and your application converts in that language automatically.
Alternatively, you can set the language runtime using the setLocale
method of App
facade. The following code will help you to set the language on the fly using the constructor.
use Illuminate\Support\Facades\App;
public function __construct() {
App::setLocale('de');
}
Get Translation Strings and Replace It
To get the translated text from language files, Laravel provides a helper method __
. Look at the below statements where I am fetching translation strings from both messages.php
and de.json
.
{{ __('messages.welcome') }} //Output: Herzlich willkommen
{{ __('How are you?') }} //Output: Wie geht es dir?
In the controller, you can print the translations using the echo
function.
echo __('messages.welcome');
echo __('How are you?');
Placeholders in Translation Strings
Laravel also allows you to pass placeholders in the translation strings. You may define placeholders prefixed with a :
operator.
'welcome' => 'Herzlich willkommen, :name',
Or in de.json
file,
{
"How are you?": "Wie geht es dir, :name?"
}
To replace these placeholders in the application you need to pass key=>value pair as follows. The key will be the placeholder string.
{{ __('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
- A Guide to Upload and Compress Images in Laravel
- How to Install and Use CKEditor in Laravel
- How to Use Laravel Tinker
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Could you tell me how to create multiple languages in laravel by using voyager admin framework and metronic theme package?