|
| 1 | +import { generateModuleHTMLComponent, generateModuleHTMLSnippet } from '../generators/generateModuleComponents' |
| 2 | +import type { ModuleConfig } from '../types' |
| 3 | + |
| 4 | +const localeDetector = `// Detect language to enable server side translations https://i18n.nuxtjs.org/docs/guide/server-side-translations |
| 5 | +export default defineI18nLocaleDetector((event, config) => { |
| 6 | + // try to get locale from query |
| 7 | + const query = tryQueryLocale(event, { lang: '' }) |
| 8 | + if (query) { |
| 9 | + return query.toString() |
| 10 | + } |
| 11 | +
|
| 12 | + // try to get locale from cookie |
| 13 | + const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' }) |
| 14 | + if (cookie) { |
| 15 | + return cookie.toString() |
| 16 | + } |
| 17 | +
|
| 18 | + // try to get locale from header accept-header |
| 19 | + const header = tryHeaderLocale(event, { lang: '' }) |
| 20 | + if (header) { |
| 21 | + return header.toString() |
| 22 | + } |
| 23 | +
|
| 24 | + // fallback to default locale |
| 25 | + return config.defaultLocale |
| 26 | +})` |
| 27 | + |
| 28 | +const englishLocaleFile = `{ |
| 29 | + "title": "Nuxt i18n", |
| 30 | + "description": "I18n (Internationalization) module for your Nuxt project powered by Vue I18n." |
| 31 | +} |
| 32 | +` |
| 33 | + |
| 34 | +const i18nDemoComponent = `<template> |
| 35 | + ${generateModuleHTMLComponent( |
| 36 | + `{{ $t('title') }}`, |
| 37 | + `{{ $t('description') }}`, |
| 38 | + 'https://i18n.nuxtjs.org/', |
| 39 | + '', |
| 40 | + '', |
| 41 | + ).html} |
| 42 | +</template> |
| 43 | +` |
| 44 | + |
| 45 | +const i18n: ModuleConfig = { |
| 46 | + humanReadableName: 'i18n', |
| 47 | + description: 'I18n (Internationalization) module for your Nuxt project powered by Vue I18n. See more: https://i18n.nuxtjs.org/', |
| 48 | + scripts: [], |
| 49 | + dependencies: [{ |
| 50 | + name: '@nuxtjs/i18n', |
| 51 | + version: '^8.3.1', |
| 52 | + isDev: true |
| 53 | + }], |
| 54 | + nuxtConfig: { |
| 55 | + modules: ['@nuxtjs/i18n'], |
| 56 | + // @ts-expect-error i18n is not set in default Nuxt Config, as the package is not installed |
| 57 | + i18n: { |
| 58 | + strategy: 'prefix_except_default', |
| 59 | + lazy: true, |
| 60 | + langDir: 'locales', |
| 61 | + defaultLocale: 'en', |
| 62 | + locales: [ |
| 63 | + { name: 'English', code: 'en', file: 'en.json' }, |
| 64 | + ], |
| 65 | + experimental: { |
| 66 | + localeDetector: './localeDetector.ts' |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + files: [{ |
| 71 | + path: 'localeDetector.ts', |
| 72 | + content: localeDetector |
| 73 | + }, { |
| 74 | + path: 'locales/en.json', |
| 75 | + content: englishLocaleFile |
| 76 | + }, { |
| 77 | + path: 'components/Welcome/I18nDemo.vue', |
| 78 | + content: i18nDemoComponent |
| 79 | + }], |
| 80 | + tasksPostInstall: [], |
| 81 | + indexVue: generateModuleHTMLSnippet('WelcomeI18nDemo'), |
| 82 | +} |
| 83 | + |
| 84 | +export default i18n |
0 commit comments