forked from nuxt-modules/i18n
/
module.ts
295 lines (247 loc) · 8.39 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import createDebug from 'debug'
import { isBoolean, isObject, isString } from '@intlify/shared'
import { defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addPlugin, addTemplate, addImports } from '@nuxt/kit'
import { resolve, relative, isAbsolute } from 'pathe'
import { setupAlias, resolveVueI18nAlias } from './alias'
import { setupPages } from './pages'
import { extendMessages } from './messages'
import { extendBundler } from './bundler'
import { generateLoaderOptions } from './gen'
import { NUXT_I18N_MODULE_ID, DEFAULT_OPTIONS } from './constants'
import { formatMessage, getNormalizedLocales, resolveLocales, getPackageManagerType } from './utils'
import { distDir, runtimeDir, pkgModulesDir } from './dirs'
import type { NuxtI18nOptions } from './types'
import type { DefineLocaleMessage, LocaleMessages } from 'vue-i18n'
export * from './types'
const debug = createDebug('@nuxtjs/i18n:module')
export default defineNuxtModule<NuxtI18nOptions>({
meta: {
name: NUXT_I18N_MODULE_ID,
configKey: 'i18n',
compatibility: {
nuxt: '^3.0.0-rc.11',
bridge: false
}
},
defaults: DEFAULT_OPTIONS,
async setup(i18nOptions, nuxt) {
const options = i18nOptions as Required<NuxtI18nOptions>
debug('options', options)
/**
* Check vertions
*/
checkOptions(options)
if (isNuxt2(nuxt)) {
throw new Error(
formatMessage(
`We will release >=7.3 <8, See about GitHub Discussions https://github.com/nuxt-community/i18n-module/discussions/1287#discussioncomment-3042457: ${getNuxtVersion(
nuxt
)}`
)
)
}
if (!isNuxt3(nuxt)) {
throw new Error(formatMessage(`Cannot support nuxt version: ${getNuxtVersion(nuxt)}`))
}
if (options.strategy === 'no_prefix' && options.differentDomains) {
console.warn(
formatMessage(
'The `differentDomains` option and `no_prefix` strategy are not compatible. Change strategy or disable `differentDomains` option.'
)
)
}
/**
* resolve lang directory
*/
if (isString(options.langDir) && isAbsolute(options.langDir)) {
console.warn(
formatMessage(
`\`langdir\` is set to an absolute path (${options.langDir}) but should be set a path relative to \`srcDir\` (${nuxt.options.srcDir}). Absolute paths will not work in production, see https://v8.i18n.nuxtjs.org/options/lazy#langdir for more details.`
)
)
}
const langPath = isString(options.langDir) ? resolve(nuxt.options.srcDir, options.langDir) : null
debug('langDir path', langPath)
/**
* resolve locale info
*/
const normalizedLocales = getNormalizedLocales(options.locales)
const hasLocaleFiles = normalizedLocales.length > 0
const localeCodes = normalizedLocales.map(locale => locale.code)
const localeInfo = langPath != null ? await resolveLocales(langPath, normalizedLocales) : []
debug('localeInfo', localeInfo)
/**
* resolve vue-i18n options
*/
// prettier-ignore
options.vueI18n = isObject(options.vueI18n)
? options.vueI18n
: isString(options.vueI18n)
? resolve(nuxt.options.rootDir, options.vueI18n)
: { legacy: false }
/**
* extend messages via 3rd party nuxt modules
*/
const additionalMessages = await extendMessages(nuxt, localeCodes, options)
/**
* setup nuxt/pages
*/
if (options.strategy !== 'no_prefix' && localeCodes.length) {
await setupPages(options, nuxt, { trailingSlash: options.trailingSlash })
}
/**
* setup module alias
*/
await setupAlias(nuxt)
/**
* add plugin and templates
*/
// for core plugin
addPlugin(resolve(runtimeDir, 'plugins/i18n'))
// for compoables
nuxt.options.alias['#i18n'] = resolve(distDir, 'runtime/composables.mjs')
nuxt.options.build.transpile.push('#i18n')
// TODO: We don't want to resolve the following as a template,
// but in the runtime dir we want to use as an ESM (e.g. internal and utils)
// for internal
addTemplate({
filename: 'i18n.internal.mjs',
src: resolve(distDir, 'runtime/internal.mjs')
})
// for utils
addTemplate({
filename: 'i18n.utils.mjs',
src: resolve(distDir, 'runtime/utils.mjs')
})
// for loading options
const localesRelativeBasePath = relative(nuxt.options.buildDir, nuxt.options.srcDir)
debug('localesRelativeBasePath', localesRelativeBasePath)
addTemplate({
filename: 'i18n.options.mjs',
write: true,
getContents: () => {
return generateLoaderOptions(
options.lazy,
options.langDir,
localesRelativeBasePath,
{
localeCodes,
localeInfo,
additionalMessages,
nuxtI18nOptions: options,
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: {
__normalizedLocales: normalizedLocales
}
},
{
ssg: nuxt.options._generate,
ssr: nuxt.options.ssr,
dev: nuxt.options.dev
}
)
}
})
/**
* To be plugged for `PageMeta` type definition on `NuxtApp`
*/
if (!!options.dynamicRouteParams) {
addPlugin(resolve(runtimeDir, 'plugins/meta'))
}
/**
* add extend type definition
*/
// prettier-ignore
const isLegacyMode = () => {
return isString(options.types)
? options.types === 'legacy'
: isObject(options.vueI18n) && isBoolean(options.vueI18n.legacy)
? options.vueI18n.legacy
: false
}
// To be plugged for `$i18n` type definition on `NuxtApp`
addPlugin(resolve(runtimeDir, isLegacyMode() ? 'plugins/legacy' : 'plugins/composition'))
nuxt.hook('prepare:types', ({ references }) => {
const vueI18nTypeFilename = resolve(runtimeDir, 'types')
references.push({ path: resolve(nuxt.options.buildDir, vueI18nTypeFilename) })
})
/**
* extend bundler
*/
await extendBundler(nuxt, {
nuxtOptions: options as Required<NuxtI18nOptions>,
hasLocaleFiles,
langPath
})
/**
* auto imports
*/
const pkgMgr = await getPackageManagerType()
const vueI18nPath = await resolveVueI18nAlias(pkgModulesDir, nuxt, pkgMgr)
debug('vueI18nPath for auto-import', vueI18nPath)
await addImports([
{ name: 'useI18n', from: vueI18nPath },
...[
'useRouteBaseName',
'useLocalePath',
'useLocaleRoute',
'useSwitchLocalePath',
'useLocaleHead',
'useBrowserLocale',
'useCookieLocale',
'defineI18nRoute'
].map(key => ({
name: key,
as: key,
from: resolve(runtimeDir, 'composables')
}))
])
/**
* transpile @nuxtjs/i18n
*/
// https://github.com/nuxt/framework/issues/5257
nuxt.options.build.transpile.push('@nuxtjs/i18n')
nuxt.options.build.transpile.push('@nuxtjs/i18n-edge')
/**
* Optimiaztion deps
*/
// Optimize vue-i18n to ensure we share the same symbol
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {}
nuxt.options.vite.optimizeDeps.exclude = nuxt.options.vite.optimizeDeps.exclude || []
nuxt.options.vite.optimizeDeps.exclude.push('vue-i18n')
}
})
function checkOptions(options: NuxtI18nOptions) {
// check `lazy` and `langDir` option
if (options.lazy && !options.langDir) {
throw new Error(formatMessage('When using the "lazy" option you must also set the "langDir" option.'))
}
// check `langDir` option
if (options.langDir) {
const locales = options.locales || []
if (!locales.length || isString(locales[0])) {
throw new Error(formatMessage('When using the "langDir" option the "locales" must be a list of objects.'))
}
for (const locale of locales) {
if (isString(locale) || !(locale.file || locale.files)) {
throw new Error(
formatMessage(
`All locales must be objects and have the "file" or "files" property set when using "langDir".\nFound none in:\n${JSON.stringify(
locale,
null,
2
)}.`
)
)
}
}
}
}
declare module '@nuxt/schema' {
interface NuxtConfig {
i18n?: NuxtI18nOptions
}
interface NuxtHooks {
'i18n:extend-messages': (messages: LocaleMessages<DefineLocaleMessage>[], localeCodes: string[]) => Promise<void>
}
}