-
Notifications
You must be signed in to change notification settings - Fork 12
/
gridsome.server.js
176 lines (160 loc) · 4.42 KB
/
gridsome.server.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
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
class VueI18n {
/**
* Default plugin options
*/
static defaultOptions () {
return {
locales: [],
messages: {},
pathAliases: {},
defaultLocale: null,
enablePathRewrite: true
}
}
/**
*
* @param {*} api
* @param {*} options
*/
constructor (api, options) {
this.api = api
this.pages = api._app.pages
this.options = options
this.pagesToGenerate = []
this.pagesToReplace = {}
this.options.defaultLocale = options.defaultLocale || options.locales[0]
this.pages.hooks.createPage.tap('i18n', this.createPageHook.bind(this))
this.pages.hooks.createRoute.tap('i18n', this.createRouteHook.bind(this))
this.pages.hooks.pageContext.tap('i18n', this.pageContextHook.bind(this))
api.createManagedPages(this.createManagedPages.bind(this))
}
/**
* Create manage pages
*
* @param {function} param.findPages
* @param {function} param.createPage
*/
createManagedPages({ createPage, removePage }) {
// Create new pages
for (const page of this.pagesToGenerate) {
createPage(page)
}
// Edit existing pages
for (const pageId in this.pagesToReplace) {
const page = this.pagesToReplace[pageId]
removePage(pageId)
createPage(page)
}
}
/**
* Merge paths parts into one
*
* @param {string} parts multiple arguments
* @returns {string}
*/
mergePathParts() {
const pathParts = []
for (var i = 0; i < arguments.length; i++) {
let pathPart = arguments[i];
// skip home
if (pathPart === '/') {
continue
}
// clean leading slash
if (pathPart.endsWith('/')) {
pathPart = pathPart.substring(0, pathPart.length - 1)
}
// clean trailing slash
if (pathPart.startsWith('/')) {
pathPart = pathPart.substring(1, pathPart.length)
}
// check if remain somethings
pathParts.push(pathPart)
}
// defending from request to merge / and /
if (pathParts.length === 0) {
return '/'
}
// ensure leading and trailing slashes
return '/' + pathParts.join('/') + '/'
}
/**
* Hook into create route process
*
* @param {*} options
*/
createPageHook(options) {
// prevent a hook loop
if (options.context.locale !== undefined) {
return options
}
options.context.locale = this.options.defaultLocale
// Retrieve current route
const route = this.pages.getRoute(options.internal.route)
// Create a page clone on a path with locale segment
for (const locale of this.options.locales) {
const pathSegment = this.options.pathAliases[locale] || locale
this.pagesToGenerate.push({
path: this.mergePathParts(pathSegment, options.path),
component: route.component,
context: Object.assign({}, options.context || {},{
locale: `${locale}`
}),
route: {
name: route.name ? `${route.name}__${locale}` : undefined,
meta: Object.assign({}, options.meta || {},{
locale: `${locale}`
})
},
queryVariables: options.internal.queryVariables
})
}
// need to removed and created again for these pages
if (options.path !== '/404' && options.internal.isDynamic === false && options.internal.isManaged === false) {
this.pagesToReplace[options.id] = {
path: options.path,
component: route.component,
context: Object.assign({}, options.context || {},{
locale: this.options.defaultLocale
}),
route: {
name: route.name,
meta: Object.assign({}, options.meta || {},{
locale: this.options.defaultLocale
})
},
queryVariables: options.internal.queryVariables
}
}
return options
}
/**
* Hook into create page context
*
* @param {*} options
*/
pageContextHook(context) {
if (!context.locale) {
context.locale = this.options.defaultLocale
}
return context
}
/**
* Hook into create route process
*
* @param {*} options
*/
createRouteHook(options) {
const meta = options.internal.meta
if (meta && meta.locale) {
if (options.name === '404' && options.path !== '/404/') {
options.name = `${options.name}__${meta.locale}`
}
}
if (!meta.locale) {
options.internal.meta.locale = this.options.defaultLocale
}
return options
}
}
module.exports = VueI18n