Skip to content

Commit 0ed008f

Browse files
authored
feat: Add i18n module for translations (#41)
1 parent e626127 commit 0ed008f

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

src/configs/i18n.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

src/configs/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import typescript from './typescript'
1010
import pnpm from './pnpm'
1111
import vscode from './vscode'
1212
import droneCI from './droneCI'
13+
import i18n from './i18n'
1314

14-
export type Modules = 'prisma' | 'sidebase-auth' | 'trpc' | 'tailwind' | 'naiveui'
15+
export type Modules = 'prisma' | 'sidebase-auth' | 'trpc' | 'tailwind' | 'naiveui' | 'i18n'
1516
export const modules: Record<Modules, ModuleConfig> = {
1617
'tailwind': tailwind,
1718
'naiveui': naiveui,
1819
'prisma': prisma,
1920
'trpc': trpc,
20-
'sidebase-auth': sidebaseAuth
21+
'sidebase-auth': sidebaseAuth,
22+
'i18n': i18n
2123
}
2224

2325
export type Configs = 'eslint' | 'github-actions' | 'typescript' | 'pnpm' | 'vscode' | 'droneCI'

src/steps/4.buildNuxtConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default async function (templateDir: string, configs: Config[], modules:
2828
nuxtConfig = defu(nuxtConfig, nuxtConfigExtension)
2929
}
3030
const nuxtConfigFile = `// https://nuxt.com/docs/api/configuration/nuxt-config
31-
export default defineNuxtConfig(${inspect(nuxtConfig, { compact: false })})
31+
export default defineNuxtConfig(${inspect(nuxtConfig, { compact: false, depth: 3 })})
3232
`
3333
await writeFile(resolver('nuxt.config.ts'), nuxtConfigFile)
3434
}

0 commit comments

Comments
 (0)