Internationalization (i18n) in Vue Application
Usage
TIP
Bitrix24 UI provides an App
component that wraps your app to provide global configurations.
Locale
Use the locale
prop with the locale you want to use from @bitrix24/b24ui-nuxt/locale
:
<script setup lang="ts">
import { it } from '@bitrix24/b24ui-nuxt/locale'
</script>
<template>
<B24App :locale="it">
<RouterView />
</B24App>
</template>
Custom locale
You also have the option to add your locale using defineLocale
:
<script setup lang="ts">
import type { Messages } from '@bitrix24/b24ui-nuxt'
import { defineLocale } from '@bitrix24/b24ui-nuxt/composables/defineLocale.js'
const locale = defineLocale<Messages>({
name: 'My custom locale',
code: 'en',
locale: 'en',
dir: 'ltr',
messages: {
// implement pairs
}
})
</script>
<template>
<B24App :locale="locale">
<RouterView />
</B24App>
</template>
TIP
Look at the locale
parameter, there you need to pass the iso code of the language. Example:
hi
Hindi (language)de-AT
: German (language) as used in Austria (region)
Dynamic locale
To dynamically switch between languages, you can use the Vue I18n plugin.
1. Install the Vue I18n package
pnpm add vue-i18n@10
yarn add vue-i18n@10
npm install vue-i18n@10
bun add vue-i18n@10
2. 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')
3. 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].code)
const dir = computed(() => locales[locale.value].dir)
useHead({
htmlAttrs: {
lang,
dir
}
})
</script>
<template>
<B24App :locale="locales[locale]">
<RouterView />
</B24App>
</template>
Supported languages
en
Englishde
Deutschla
Españolbr
Português (Brasil)fr
Françaisit
Italianopl
Polskiru
Русскийua
Українськаtr
Türkçesc
中文(简体)tc
中文(繁體)ja
日本語vn
Tiếng Việtid
Bahasa Indonesiams
Bahasa Melayuth
ภาษาไทยar
عربي,kz
Қазақша