T I M E

Waktu selalu menyediakan kita untuk bisa memilih. Waktu memberikan kita kesempatan untuk berpikir (jernih). Terkadang, sebab hati ataupun perasaan yang tak terkendali. Keputusan yang dibuat menjadi…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Multilingual website using AdonisJs

There are numerous ways to add support for multiple languages to your website. However, a common one is to use a dropdown to switch/change language. In this blog post, we will implement the same.

Let’s get started by creating the required routes and render a view with a greeting in the selected language.

The antl.availableLocales method returns an array of available languages based upon the available translations. So in order for this method to return some values, we need to create translations for couple of languages.

All translations lives inside resources/locales/ directory. Start by creating two new directories for english and french languages.

English translation inside resources/locales/en/greetings.json

The following JSON file will live inside resources/locales/fr/greetings.json

Finally, create resources/views/home.edge file with the following HTML inside it. We make use of bootstrap to style the webpage and show a dropdown to select one of the available languages.

Till now we have a route, which renders an HTML page with an option to switch the language. Next, we need to write the logic to save the user selected language inside cookies.

The code is pretty simple, as we just save the value of user selected language inside a cookie named lang and then redirect the user back to the original page.

Finally, we need a middleware to read the selected language from the cookie and then set as the default language for the current HTTP request.

Run the following command to create a new middleware

Replace the file contents with the following snippet.

The middleware has a simple job, read the value of the cookie and then call antl.switchLocale to update the language for the current HTTP request.

Also, we are sharing the locales with all the views by calling view.share method. After this, you can remove the locales sharing from the routes file.

Finally, make sure to register the middleware inside the global middleware list in start/kernel.js file.

Add a comment

Related posts:

Where Will King James Decide To Take His Throne This Summer?

LeBron James has just completed his eighth consecutive NBA Finals series. He and the Cleveland Cavaliers fell short in a dismantling sweep against the Golden State Warriors who are arguably the…

Wood smoke and grief

Grief is a funny thing. Not ha ha funny, at least, not usually, but funny as in hmmm. It doesn’t age, and as much as it feels it will actually kill you, it doesn’t, at least, not usually. I was…

Stories Written in Stars

From stories written in the stars, to specks of dust that form the very ground we walk on, now becoming two hearts breathing the breath of life into each other. Raised from our fruitful roots; born…