When developing web applications, allowing users to view content in different languages can be a significant advantage. Laravel provides a flexible structure for managing such multilingual applications. But how does Laravel determine the current language or which languages are available in the application? In this post, we’ll explore these questions.
Determining the Current Language in Laravel
Laravel stores the necessary information for determining the current language and available languages in the config/app.php
file. Some of the important keys in this file are:
/* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by the translation service provider. You are free to set this value | to any of the locales which will be supported by the application. | */ 'locale' => 'en', /* |-------------------------------------------------------------------------- | Application Fallback Locale |-------------------------------------------------------------------------- | | The fallback locale determines the locale to use when the current one | is not available. You may change the value to correspond to any of | the language folders that are provided through your application. | */ 'fallback_locale' => 'en', Thelocale
key sets the default language of the application. If no other language is set within the application, this language will be used. Thefallback_locale
acts as a backup language when the current language is unavailable. For example, if your application doesn’t support the Turkish language and a user requests it, the system will revert to the fallback locale, which in this example is English (en
).
Defining Supported Languages
To define the supported languages in Laravel, you can add a new key to the config/app.php
file. This key will list all the locales your application supports. For example:
'available_locales' => [ 'English' => 'en', 'Russian' => 'ru', 'French' => 'fr', ], This key allows you to specify the languages your application supports. Laravel can then reference this list when determining the appropriate locale to use, ensuring that only supported languages are used.
Storing Language Files
You can store language files in your Laravel application using the following path: resources/lang/{en,tr}/{myfile.php}
. With this structure, you can create separate files for different languages and store them in specific directories.
Example Language Files
Like many applications, Laravel stores translations in files. For example, English translations might be in the en/messages.php
file, while Turkish translations could be in the tr/messages.php
file. For instance:
<?php // lang/en/messages.php return [ 'anasayfa' => 'Home', ]; <?php // lang/tr/messages.php return [ 'anasayfa' => 'Anasayfa', ];
These files contain key-value pairs that provide translations for different texts in your application.
Using Translations in the Frontend
Laravel makes it easy to use translations on the frontend as well. For example, the anasayfa
translation defined above can be called like this:
@lang('messages.anasayfa')
With this simple method, you can easily manage language support in the frontend of your application.