AdonisJS

AdonisJS: Internationalization and Localization

2 min read
AdonisJS: Internationalization and Localization

In this article, we can try to Define the AdonisJS Internationalization and Localization Module, How to Manage the Internationalization and Localization module of AdonisJS, Config Internationalization, and Localization module, configure Internationalization and Localization and Usage.

Internationalization and Localization

This @adonisjs/i18n official package adds support for internationalization and localization to your own AdonisJS applications or projects.

Internationalization helpers allow to you perform language-sensitive formatting of specific values such as currency, date, and name.

The localization layer allows you to store translations and reference them within the validation errors, auth exceptions, Edge templates, and so on.

Here the I18n package must be configured and installed separately.

Install

You can check our previous article: AdonisJS: REST API simple CRUD Operation. IF you want then buy a good, reliable, secure web hosting service from here: click herenpm i @adonisjs/i18n

Configure

node ace configure @adonisjs/i18n

CREATE: app/Middleware/DetectUserLocale.ts

CREATE: ./resources/lang

CREATE: config/i18n.ts

UPDATE: .adonisrc.json { providers += "@adonisjs/i18n" }

Usage

Below is a basic example of importing the installed package and formatting values for applications. import I18n from '@ioc:Adonis/Addons/I18n'

I18n.locale('en-US').formatDate(new Date())
// 10/8/2021

I18n.locale('fr').formatCurrency(100, { currency: 'EUR' })
// 100,00 €

const luxonDate = DateTime.local().minus({ minutes: 10 })
I18n.locale('pt').formatRelativeTime(luxonDate, 'auto')
// há 10 minutos


So, here you can make use of the formatMessage method to format stored translations, and the method we can accept is the message key as the first argument and the data as the second argument.import I18n from '@ioc:Adonis/Addons/I18n' I18n .locale('en-US') .formatMessage('messages.greeting', { name: 'Virk' })

Usage during HTTP requests

Therefore, it is recommended to use the ctx.i18n object during the HTTP requests and isolated instances of I18n class for the current request. Route.get('/', async ({ i18n }) => { return i18n.formatCurrency(100, { currency: 'EUR' }) })

You can purchase your hosting from Cloudsurph.com, Cloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.

Config

Here the configuration is stored inside the config/i18n.ts file. So, you can always find the up-to-date config stub on GitHub. import Application from '@ioc:Adonis/Core/Application' import { I18nConfig } from '@ioc:Adonis/Addons/I18n'

const i18nConfig: I18nConfig = {
translationsFormat: 'icu',
defaultLocale: 'en',

// Optional
supportedLocales: [],
fallbackLocales: {},

provideValidatorMessages: true,
loaders: {
fs: {
enabled: true,
location: Application.resourcesPath('lang'),
},
},
}

export default i18nConfig

If you want to know more about AdonisJS Model relationships then please visit AdonisJs main website.

If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.

✏️ Edit This Post