Not so long ago, I decided to take a closer look at the increasingly popular Laravel framework, the benefit of the documentation and articles on it is enough. But what kind of training without practice? It was decided to transfer to it a rather simple, self-written site, which quietly hung on the hosting itself and did not bring trouble.
One of the features of the site was the switching of the interface language, implemented by the yaml dictionary file and a simple parser. To transfer it to the logic of the framework was a matter of mechanical rather than creative, but getting it to remember the chosen language turned out to be a little more difficult. Below I present my version of the implementation of this task.
Configuring Application Configuration
First you need to specify the desired locale and add an array of languages we use in config / app.php
'locale' => 'ru',
Create dictionaries
Dictionaries in Laravel are just php arrays. How to create them is well described in the
documentation . We need a dictionary for each language used in the application. We create in resources / lang a folder for each language (I have only two, en and ru) and in each folder we create our own dictionary, for example, messages.php. I don’t need to explain how to use them in templates.
Create Middleware
By default, Laravel is always loaded with the language selected in the configuration. To change it, we need to intercept the request before it receives the view and set the language using the App :: setLocale (); method. Middleware is such a piece of code that will be executed every time a user requests a page and allows us to do it. In it, we will check which language the user has already selected (we specify it in the user’s session) if he has already visited our site, and if there is no one, add the default language to the session (for example, ru).
')
At the same time, we will verify that the language selected by the user is valid (for this we have indicated the available languages in the configuration earlier).
Using the console and artisan we create middleware for our switch. Let's call it, for example, Locale.
php artisan make:middleware Locale
And we write our code to the handle method. Here the variables specified in the config will be useful.
public function handle($request, Closure $next) { $raw_locale = Session::get('locale');
Do not forget to add to the beginning of the file
use App; use Config; use Session;
We register our Middleware
In the app / Http / Kernel.php file, we add the StartSession middleware (for initializing the session, thanks to
hovdev for the hint) and our middleware to the end of the array so that it
runs every time the application is loaded.
protected $middleware = [ ... \Illuminate\Session\Middleware\StartSession::class, \App\Http\Middleware\Locale::class, ];
Create a route to switch language
Register the path by which we will switch the language in the user session. For this purpose, I chose a link like mysite / setlocale / en. When you click on it, we change the value of lang (previously installed by our middleware) to the one indicated at the end of the link. Along the way, we check that the selected language is valid by a familiar method and redirect the user to the same page from which the script was called.
Route::get('setlocale/{locale}', function ($locale) { if (in_array($locale, \Config::get('app.locales'))) {
That's all.