forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (114 loc) · 3.23 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
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
const { resolve, join } = require('path')
const { readdirSync } = require('fs')
const i18nExtensions = require('vue-i18n-extensions')
const {
MODULE_NAME,
ROOT_DIR,
PLUGINS_DIR,
TEMPLATES_DIR,
DEFAULT_OPTIONS,
NESTED_OPTIONS,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY
} = require('./helpers/constants')
const { makeRoutes } = require('./helpers/routes')
const {
getLocaleCodes,
getLocaleFromRoute,
getForwarded,
getHostname,
getLocaleDomain,
syncVuex
} = require('./helpers/utils')
module.exports = function (userOptions) {
const pluginsPath = join(__dirname, PLUGINS_DIR)
const templatesPath = join(__dirname, TEMPLATES_DIR)
const requiredPlugins = ['main', 'routing']
const options = { ...DEFAULT_OPTIONS, ...userOptions }
// 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] }
}
}
const templatesOptions = {
...options,
MODULE_NAME,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
getLocaleCodes,
getLocaleFromRoute,
getForwarded,
getHostname,
getLocaleDomain,
syncVuex,
isSpa: this.options.mode === 'spa'
}
// Generate localized routes
const pagesDir = this.options.dir && this.options.dir.pages ? this.options.dir.pages : 'pages'
this.extendRoutes((routes) => {
const localizedRoutes = makeRoutes(routes, {
...options,
pagesDir
})
routes.splice(0, routes.length)
routes.unshift(...localizedRoutes)
})
// Plugins
for (const file of requiredPlugins) {
this.addPlugin({
src: resolve(pluginsPath, `${file}.js`),
fileName: join(ROOT_DIR, `plugin.${file}.js`),
options: templatesOptions
})
}
// Templates
for (const file of readdirSync(templatesPath)) {
this.addTemplate({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
}
// SEO plugin
if (options.seo) {
this.addPlugin({
src: resolve(pluginsPath, `seo.js`),
fileName: join(ROOT_DIR, `plugin.seo.js`),
options: templatesOptions
})
}
// Add vue-i18n to vendors if using Nuxt 1.x
if (this.options.build.vendor) {
this.options.build.vendor.push('vue-i18n')
}
// Add vue-i18n-loader if applicable
if (options.vueI18nLoader) {
this.extendBuild(config => {
const loaders = config.module.rules.find(el => el.loader === 'vue-loader').options.loaders
if (loaders) {
// vue-loader under 15.0.0
loaders.i18n = '@kazupon/vue-i18n-loader'
} else {
// vue-loader after 15.0.0
config.module.rules.push({
resourceQuery: /blockType=i18n/,
type: 'javascript/auto',
loader: '@kazupon/vue-i18n-loader'
})
}
})
}
this.options.router.middleware.push('i18n')
this.options.render.bundleRenderer.directives = this.options.render.bundleRenderer.directives || {}
this.options.render.bundleRenderer.directives.t = i18nExtensions.directive
}