-
Notifications
You must be signed in to change notification settings - Fork 1
/
gridsome.client.js
179 lines (132 loc) · 5.06 KB
/
gridsome.client.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
177
178
179
export default function (Vue, options, { appOptions, router, head }) {
if( !options.locales){
throw new Error(`TranslateIt plugin is missing a required "locales" option.`)
}
// helpers for components
Vue.prototype.$localesList = options.locales
Vue.prototype.$defaultLocale = options.defaultLocale
// is set in initLocale()
let locale
// Setup locale. This function can be used as helper in Gridsome project
function setLocale(lang) {
locale = lang // update variable for plugin
Vue.prototype.$locale = locale // update helper
localStorage.setItem('locale', locale) // update browser preference
}
// helper
Vue.prototype.$setLocale = setLocale
// function that splits path to segments, e.g. to check locales within path
function splitPath(path) {
if (!path.startsWith('/')) {
path = '/' + path
}
if (!path.endsWith('/')) {
path = path + '/'
}
return path.split('/')
}
// Initialize locale different from default, if applyable
function initLocale() {
if(process.isClient) {
// 1) try get locales from url
const browserUrl = splitPath(window.location.href)
const localeFromBrowser = browserUrl.filter(el => options.locales.includes(el))[0] // in case there are several locales in url, we get first
if(localeFromBrowser) {
setLocale(localeFromBrowser)
}
else{
// 2) Check if we have saved preferences of user
if( localStorage.locale && localStorage.locale !== 'undefined') {
setLocale(localStorage.locale)
}
else {
// if non of these conditions are worked, locale will remain default
setLocale(options.defaultLocale)
}
}
}
Vue.prototype.$locale = locale
}
// Transform path according to locale
function translatePath(pathToResolve, targetLocale) {
if (!targetLocale) {
return pathToResolve
}
const pathToResolveSegments = splitPath(pathToResolve)
// delete all possible locales from path
const newPathSegments = pathToResolveSegments.filter(el => !options.locales.includes(el))
const pathWithoutLocale = newPathSegments.join('/')
// Check if the path need to be translated
if( (options.exclude.find((e) => pathWithoutLocale.startsWith(e)))
|| (!options.slugifyDefaultLocale && (targetLocale === options.defaultLocale))
){
return pathWithoutLocale
}
else {
// Insert locale before page path
//If path contains one of 'collections', push locale after that
// expected /'collections'/path-to-page -> /'collections'/locale/path-to-page
let i
if( i = newPathSegments.findIndex(el => options.collections.includes(el)) > 0 ) {
newPathSegments.splice(i+1, 0, targetLocale);
}
// Else insert at the end
// expected /path-to-page -> /locale/path-to-page
else {
newPathSegments.splice(1, 0, targetLocale)
}
return newPathSegments.join('/')
}
}
// Add translate path helper
Vue.prototype.$tp = translatePath
// Translate messages if applyable
function translateString(alias) {
if( !options.translations ){
console.error('TranslateIt: file with translations not found')
return
}
let hasAlias = 0
for (const item of options.translations) {
if (item.alias === alias){
hasAlias++
// Check if there is translation for current alias, if no show default locale string
if (eval(`item.${locale}`)){
return eval(`item.${locale}`)
}
else {
return eval(`item.${options.defaultLocale}`)
}
}
}
// If for some reason there is no translation for current alias, plugin will insert alias text
if(hasAlias == 0) {
return alias;
}
}
// Add translate string helper
Vue.prototype.$ts = translateString;
if(process.isClient) {
//Rewrite route according to locale
router.beforeEach(async (to, from, next) => {
// do not rewrite build paths
if (process.isServer) {
return next()
}
initLocale()
const newPath = translatePath(to.path || '/', locale)
if (newPath === to.path) {
return next()
} else {
return next({
path: newPath,
// replace: true
})
}
})
// Set lang attribute for <html>
router.afterEach(() => {
head.htmlAttrs = { 'lang' : locale }
})
}
}