Files
ui/docs/content/1.getting-started/7.i18n/1.nuxt.md
2024-11-08 17:48:42 +01:00

3.1 KiB

navigation.title, title, description, select
navigation.title title description select
Nuxt Internationalization (i18n) in a Nuxt app Learn how to internationalize your Nuxt app and support multi-directional support (LTR/RTL).
items
label icon to
Nuxt i-logos-nuxt-icon /getting-started/i18n/nuxt
label icon to
Vue i-logos-vue /getting-started/i18n/vue

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">
    <NuxtPage />
  </UApp>
</template>

Custom locale

You also have the option to add your own locale using defineLocale:

<script setup lang="ts">
const locale = defineLocale('My custom locale', {
  // implement pairs
})
</script>

<template>
  <UApp :locale="locale">
    <NuxtPage />
  </UApp>
</template>

Dynamic locale

To dynamically switch between languages, you can use the NuxtI18n module.

  1. Install the @nuxtjs/i18n package:

::code-group{sync="pm"}

pnpm add @nuxtjs/i18n@next
yarn add @nuxtjs/i18n@next
npm install @nuxtjs/i18n@next
bun add @nuxtjs/i18n@next

::

  1. Add the @nuxtjs/i18n module to your nuxt.config.ts{lang="ts-type"} and define the locales you want to support:
export default defineNuxtConfig({
  modules: [
    '@nuxt/ui',
    '@nuxtjs/i18n'
  ],
  i18n: {
    locales: [{
      code: 'de',
      name: 'Deutsch'
    }, {
      code: 'en',
      name: 'English'
    }, {
      code: 'fr',
      name: 'Français'
    }]
  }
})
  1. Use the locale prop with the useI18n locale you want to use from @nuxt/ui/locale:
<script setup lang="ts">
import * as locales from '@nuxt/ui/locale'

const { locale } = useI18n()
</script>

<template>
  <UApp :locale="locales[locale]">
    <NuxtPage />
  </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.

  1. Install the @vueuse/core package:

::code-group{sync="pm"}

pnpm add @vueuse/core
yarn add @vueuse/core
npm install @vueuse/core
bun add @vueuse/core

::

  1. Then in your app.vue:
<script setup lang="ts">
import { useTextDirection } from '@vueuse/core'

const textDirection = useTextDirection({ initialValue: 'ltr' })
const dir = computed(() => textDirection.value === 'rtl' ? 'rtl' : 'ltr')
</script>

<template>
  <UApp :dir="dir">
    <NuxtPage />
  </UApp>
</template>

Supported languages

:supported-languages