mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-23 16:30:45 +01:00
3.2 KiB
3.2 KiB
navigation.title, title, description, select
| navigation.title | title | description | select | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Vue | Internationalization (i18n) in a Vue app | Learn how to internationalize your Vue app and support multi-directional support (LTR/RTL). |
|
Usage
Nuxt 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 @nuxt/ui/locale:
<script setup lang="ts">
import { fr } from '@nuxt/ui/locale'
</script>
<template>
<UApp :locale="fr">
<RouterView />
</UApp>
</template>
Custom locale
You also have the option to add your locale using defineLocale:
<script setup lang="ts">
import { defineLocale } from '@nuxt/ui/runtime/composables/defineLocale'
const locale = defineLocale('My custom locale', {
// implement pairs
})
</script>
<template>
<UApp :locale="locale">
<RouterView />
</UApp>
</template>
Dynamic locale
To dynamically switch between languages, you can use the VueI18n plugin.
- Install the
vue-i18npackage:
::code-group{sync="pm"}
pnpm add vue-i18n@10
yarn add vue-i18n@10
npm install vue-i18n@10
bun add vue-i18n@10
::
- Define the
createI18ninstance and register thei18nplugin in yourmain.tsfile:
import { createApp } from 'vue'
import App from './App.vue'
import { createI18n } from 'vue-i18n'
const messages = {
en: {
// ...
},
de: {
// ...
},
}
const i18n = createI18n({
legacy: false,
locale: 'en',
availableLocales: ['en', 'de'],
messages,
})
createApp(App)
.use(i18n)
.mount('#app')
- Use the
localeprop with theuseI18nlocale you want to use from@nuxt/ui/locale:
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import * as locales from '@nuxt/ui/locale'
const { locale } = useI18n()
</script>
<template>
<UApp :locale="locales[locale]">
<RouterView />
</UApp>
</template>
Direction
Use the dir prop with ltr or rtl to set the global reading direction of your app:
<template>
<UApp dir="rtl">
<NuxtPage />
</UApp>
</template>
Dynamic direction
To dynamically change the global reading direction of your app, you can use the useTextDirection composable.
- Install the
@vueuse/corepackage:
::code-group{sync="pm"}
pnpm add @vueuse/core
yarn add @vueuse/core
npm install @vueuse/core
bun add @vueuse/core
::
- Then in your
App.vue:
<script setup lang="ts">
import { computed } from 'vue'
import { useTextDirection } from '@vueuse/core'
const textDirection = useTextDirection()
const dir = computed(() => textDirection.value === 'rtl' ? 'rtl' : 'ltr')
</script>
<template>
<UApp :dir="dir">
<RouterView />
</UApp>
</template>
Supported languages
:supported-languages