Internationalization (i18n)
Usage
Locale
Use the locale prop with the locale you want to use from @bitrix24/b24ui-nuxt/locale:
<script setup lang="ts">
import { fr } from '@bitrix24/b24ui-nuxt/locale'
</script>
<template>
<B24App :locale="fr">
<RouterView />
</B24App>
</template>
Custom locale
You can create your own locale using the defineLocale utility:
<script setup lang="ts">
import type { Messages } from '@bitrix24/b24ui-nuxt'
import { defineLocale } from '@bitrix24/b24ui-nuxt/composables/defineLocale'
const locale = defineLocale<Messages>({
name: 'My custom locale',
code: 'en',
locale: 'en-AU',
dir: 'ltr',
messages: {
// implement pairs
}
})
</script>
<template>
<B24App :locale="locale">
<RouterView />
</B24App>
</template>
locale parameter, there you need to pass the iso code of the language. Example:hiHindi (language)de-AT: German (language) as used in Austria (region)
Extend locale
You can customize an existing locale by overriding its messages using the extendLocale utility:
<script setup lang="ts">
import { en } from '@bitrix24/b24ui-nuxt/locale'
import { extendLocale } from '@bitrix24/b24ui-nuxt/composables/defineLocale.js'
const locale = extendLocale(en, {
locale: 'en-AU',
messages: {
commandPalette: {
placeholder: 'Search a component...'
}
}
})
</script>
<template>
<B24App :locale="locale">
<RouterView />
</B24App>
</template>
Dynamic locale
To dynamically switch between languages, you can use the Vue I18n plugin.
Install the Vue I18n package
pnpm add vue-i18n@11
yarn add vue-i18n@11
npm install vue-i18n@11
bun add vue-i18n@11
Use the Vue I18n plugin in your main.ts
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { createI18n } from 'vue-i18n'
import b24UiPlugin from '@bitrix24/b24ui-nuxt/vue-plugin'
import App from './App.vue'
const app = createApp(App)
const router = createRouter({
routes: [],
history: createWebHistory()
})
const i18n = createI18n({
legacy: false,
locale: 'en',
availableLocales: ['en', 'de'],
messages: {
en: {
// ...
},
de: {
// ...
}
}
})
app.use(router)
app.use(i18n)
app.use(b24UiPlugin)
app.mount('#app')
Set the locale prop using useI18n
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import * as locales from '@bitrix24/b24ui-nuxt/locale'
const { locale } = useI18n()
</script>
<template>
<B24App :locale="locales[locale]">
<RouterView />
</B24App>
</template>
Dynamic direction
Each locale has a dir property which will be used by the App component to set the directionality of all components.
In a multilingual application, you might want to set the lang and dir attributes on the <html> element dynamically based on the user's locale, which you can do with the useHead composable:
<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { useHead } from '@unhead/vue'
import * as locales from '@bitrix24/b24ui-nuxt/locale'
const { locale } = useI18n()
const lang = computed(() => locales[locale.value].locale)
const dir = computed(() => locales[locale.value].dir)
useHead({
htmlAttrs: {
lang,
dir
}
})
</script>
<template>
<B24App :locale="locales[locale]">
<RouterView />
</B24App>
</template>
Supported languages
By default, the en locale is used.
العربية
Code: ar | Locale: ar
Português (Brasil)
Code: br | Locale: pt-BR
Deutsch
Code: de | Locale: de
English
Code: en | Locale: en
Français
Code: fr | Locale: fr
Bahasa Indonesia
Code: id | Locale: id
Indian
Code: in | Locale: hi_IN
Italiano
Code: it | Locale: it
日本語
Code: ja | Locale: ja
Қазақша
Code: kz | Locale: kk
Español
Code: la | Locale: es
Bahasa Melayu
Code: ms | Locale: ms
Polski
Code: pl | Locale: pl
Русский
Code: ru | Locale: ru
中文(简体)
Code: sc | Locale: zh-CN
中文(繁體)
Code: tc | Locale: zh-TW
ภาษาไทย
Code: th | Locale: th
Türkçe
Code: tr | Locale: tr
Українська
Code: ua | Locale: uk
Tiếng Việt
Code: vn | Locale: vi