-
Notifications
You must be signed in to change notification settings - Fork 474
/
index.js
92 lines (82 loc) · 3.48 KB
/
index.js
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
import { resolve, join } from 'path'
import { readdirSync } from 'fs'
import { directive as i18nExtensionsDirective } from '@intlify/vue-i18n-extensions'
import { MODULE_NAME, COMPONENT_OPTIONS_KEY, DEFAULT_OPTIONS, LOCALE_CODE_KEY, LOCALE_ISO_KEY, LOCALE_DIR_KEY, LOCALE_DOMAIN_KEY, LOCALE_FILE_KEY, NESTED_OPTIONS, ROOT_DIR, STRATEGIES } from './helpers/constants'
import { getLocaleCodes } from './helpers/utils'
import { buildHook, createExtendRoutesHook } from './core/hooks'
export default function (userOptions) {
const options = { ...DEFAULT_OPTIONS, ...userOptions, ...this.options.i18n }
// Options that have nested config options must be merged
// individually with defaults to prevent missing options
for (const key of NESTED_OPTIONS) {
if (options[key] !== false) {
options[key] = { ...DEFAULT_OPTIONS[key], ...options[key] }
}
}
if (!Object.values(STRATEGIES).includes(options.strategy)) {
// eslint-disable-next-line no-console
console.error('[' + MODULE_NAME + '] Invalid "strategy" option "' + options.strategy + '" (must be one of: ' + Object.values(STRATEGIES).join(', ') + ').')
return
}
if (options.lazy) {
if (!options.langDir) {
throw new Error(`[${MODULE_NAME}] When using the "lazy" option you must also set the "langDir" option.`)
}
if (!options.locales.length || typeof options.locales[0] === 'string') {
throw new Error(`[${MODULE_NAME}] When using the "langDir" option the "locales" option must be a list of objects.`)
}
for (const locale of options.locales) {
if (!locale[LOCALE_FILE_KEY]) {
throw new Error(`[${MODULE_NAME}] All locale objects must have the "file" property set when using "lazy".\nFound none in:\n${JSON.stringify(locale, null, 2)}.`)
}
}
options.langDir = this.nuxt.resolver.resolveAlias(options.langDir)
}
// Templates (including plugins).
// This is done here rather than in the build hook to ensure the order the plugins are added
// is predictable between different modules.
const localeCodes = getLocaleCodes(options.locales)
const nuxtOptions = this.options
const { trailingSlash } = nuxtOptions.router
const templatesOptions = {
...options,
IS_UNIVERSAL_MODE: nuxtOptions.mode === 'universal',
MODULE_NAME,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
LOCALE_DIR_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
localeCodes,
trailingSlash
}
const templatesPath = join(__dirname, '/templates')
for (const file of readdirSync(templatesPath)) {
if (file.startsWith('plugin.')) {
if (file === 'plugin.seo.js' && !options.seo) {
continue
}
this.addPlugin({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
} else {
this.addTemplate({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
}
}
if (options.strategy !== STRATEGIES.NO_PREFIX && localeCodes.length) {
this.extendRoutes(createExtendRoutesHook(this, options))
}
this.nuxt.hook('build:before', () => buildHook(this, options))
this.options.alias['~i18n-klona'] = require.resolve('klona/full').replace('.js', '.mjs')
this.options.router.middleware.push('nuxti18n')
this.options.render.bundleRenderer.directives = this.options.render.bundleRenderer.directives || {}
this.options.render.bundleRenderer.directives.t = i18nExtensionsDirective
}