Skip to content

Variable: buildI18nOptions ​

const buildI18nOptions: ComponentOptionsBuilder<I18nOptions>

Build the component options of an I18n component, to register it in the components of an application. Name it i18n: the I18nMiddleware and commanderTranslator use the component of that name by default. The options are validated when the component is created, not here.

Param ​

options

Where the messages come from (directory, messages), the default language, the fallbacks, the behavior on a missing message, the currency and time zone of the formatted arguments, the validation messages. Without options, the component has no messages and its default language is en.

Returns ​

The component options ({class: I18n, ...options}).

Example ​

typescript
import {Application} from 'lakutata'
import {buildI18nOptions, type I18n} from 'lakutata/com/i18n'

Application
    .alias({'@locales': '/srv/shop/locales'})
    .run(() => ({
        id: 'shop.app',
        name: 'Shop',
        components: {
            i18n: buildI18nOptions({
                //locales/en.json, locales/zh-CN.json, or a directory per language
                directory: '@locales',
                defaultLocale: 'en',
                //A message missing in zh-TW is looked up in zh-CN, then zh, then en
                fallback: {'zh-TW': 'zh-CN'},
                currency: 'CNY',
                timeZone: 'Asia/Shanghai'
            })
        }
    }))
    .onLaunched(async (app: Application): Promise<void> => {
        const i18n: I18n = await app.getObject('i18n')
        console.log(i18n.getLocales())
    })

Released under the MIT License.