|
| 1 | +--- |
| 2 | +title: I18n module |
| 3 | +description: Learn how to create multi-language websites using Nuxt Content with the @nuxtjs/i18n module. |
| 4 | +--- |
| 5 | + |
| 6 | +Nuxt Content integrates with [`@nuxtjs/i18n`](https://i18n.nuxtjs.org/) to create multi-language websites. When both modules are configured together, you can organize content by language and automatically serve the correct content based on the user's locale. |
| 7 | + |
| 8 | +## Setup |
| 9 | + |
| 10 | +::prose-steps |
| 11 | +### Install the required module |
| 12 | + |
| 13 | +```bash [terminal] |
| 14 | +npm install @nuxtjs/i18n |
| 15 | +``` |
| 16 | + |
| 17 | +### Configure your `nuxt.config.ts` |
| 18 | + |
| 19 | +```ts [nuxt.config.ts] |
| 20 | +export default defineNuxtConfig({ |
| 21 | + modules: ['@nuxt/content', '@nuxtjs/i18n'], |
| 22 | + i18n: { |
| 23 | + locales: [ |
| 24 | + { code: 'en', name: 'English', language: 'en-US', dir: 'ltr' }, |
| 25 | + { code: 'fr', name: 'French', language: 'fr-FR' }, |
| 26 | + { code: 'fa', name: 'Farsi', language: 'fa-IR', dir: 'rtl' }, |
| 27 | + ], |
| 28 | + strategy: 'prefix_except_default', |
| 29 | + defaultLocale: 'en', |
| 30 | + } |
| 31 | +}) |
| 32 | +``` |
| 33 | + |
| 34 | +### Define collections for each language |
| 35 | + |
| 36 | +Create separate collections for each language in your `content.config.ts`: |
| 37 | + |
| 38 | +```ts [content.config.ts] |
| 39 | +const commonSchema = ...; |
| 40 | + |
| 41 | +export default defineContentConfig({ |
| 42 | + collections: { |
| 43 | + // English content collection |
| 44 | + content_en: defineCollection({ |
| 45 | + type: 'page', |
| 46 | + source: { |
| 47 | + include: 'en/**', |
| 48 | + prefix: '', |
| 49 | + }, |
| 50 | + schema: commonSchema, |
| 51 | + }), |
| 52 | + // French content collection |
| 53 | + content_fr: defineCollection({ |
| 54 | + type: 'page', |
| 55 | + source: { |
| 56 | + include: 'fr/**', |
| 57 | + prefix: '', |
| 58 | + }, |
| 59 | + schema: commonSchema, |
| 60 | + }), |
| 61 | + // Farsi content collection |
| 62 | + content_fa: defineCollection({ |
| 63 | + type: 'page', |
| 64 | + source: { |
| 65 | + include: 'fa/**', |
| 66 | + prefix: '', |
| 67 | + }, |
| 68 | + schema: commonSchema, |
| 69 | + }), |
| 70 | + }, |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +### Create dynamic pages |
| 75 | + |
| 76 | +Create a catch-all page that fetches content based on the current locale: |
| 77 | + |
| 78 | +```vue [pages/\[...slug\].vue] |
| 79 | +<script setup lang="ts"> |
| 80 | +import { withLeadingSlash } from 'ufo' |
| 81 | +import type { Collections } from '@nuxt/content' |
| 82 | +
|
| 83 | +const route = useRoute() |
| 84 | +const { locale, localeProperties } = useI18n() |
| 85 | +const slug = computed(() => withLeadingSlash(String(route.params.slug))) |
| 86 | +
|
| 87 | +const { data: page } = await useAsyncData('page-' + slug.value, async () => { |
| 88 | + // Build collection name based on current locale |
| 89 | + const collection = ('content_' + locale.value) as keyof Collections |
| 90 | + const content = await queryCollection(collection).path(slug.value).first() |
| 91 | +
|
| 92 | + // Optional: fallback to default locale if content is missing |
| 93 | + if (!content && locale.value !== 'en') { |
| 94 | + return await queryCollection('content_en').path(slug.value).first() |
| 95 | + } |
| 96 | +
|
| 97 | + return content |
| 98 | +}, { |
| 99 | + watch: [locale], // Refetch when locale changes |
| 100 | +}) |
| 101 | +</script> |
| 102 | +
|
| 103 | +<template> |
| 104 | + <ContentRenderer v-if="page" :value="page" /> |
| 105 | + <div v-else> |
| 106 | + <h1>Page not found</h1> |
| 107 | + <p>This page doesn't exist in {{ locale }} language.</p> |
| 108 | + </div> |
| 109 | +</template> |
| 110 | +``` |
| 111 | +:: |
| 112 | + |
| 113 | +That's it! 🚀 Your multi-language content site is ready. |
| 114 | + |
| 115 | +## Content Structure |
| 116 | + |
| 117 | +Organize your content files in language-specific folders to match your collections: |
| 118 | + |
| 119 | +``` |
| 120 | +content/ |
| 121 | + en/ |
| 122 | + index.md |
| 123 | + about.md |
| 124 | + blog/ |
| 125 | + post-1.md |
| 126 | + fr/ |
| 127 | + index.md |
| 128 | + about.md |
| 129 | + blog/ |
| 130 | + post-1.md |
| 131 | + fa/ |
| 132 | + index.md |
| 133 | + about.md |
| 134 | +``` |
| 135 | + |
| 136 | +Each language folder should contain the same structure to ensure content parity across locales. |
| 137 | + |
| 138 | +## Fallback Strategy |
| 139 | + |
| 140 | +You can implement a fallback strategy to show content from the default locale when content is missing in the current locale: |
| 141 | + |
| 142 | +```ts [pages/\[...slug\].vue] |
| 143 | +const { data: page } = await useAsyncData('page-' + slug.value, async () => { |
| 144 | + const collection = ('content_' + locale.value) as keyof Collections |
| 145 | + let content = await queryCollection(collection).path(slug.value).first() |
| 146 | + |
| 147 | + // Fallback to default locale if content is missing |
| 148 | + if (!content && locale.value !== 'en') { |
| 149 | + content = await queryCollection('content_en').path(slug.value).first() |
| 150 | + } |
| 151 | + |
| 152 | + return content |
| 153 | +}) |
| 154 | +``` |
| 155 | + |
| 156 | +::prose-warning |
| 157 | +Make sure to handle missing content gracefully and provide clear feedback to users when content is not available in their preferred language. |
| 158 | +:: |
| 159 | + |
| 160 | +## Complete Examples |
| 161 | + |
| 162 | +You can see a complete working example: |
| 163 | +- **Source**: https://github.com/nuxt/content/tree/main/examples/i18n |
| 164 | +- **Live Demo**: https://content3-i18n.nuxt.dev/ |
0 commit comments