-
Notifications
You must be signed in to change notification settings - Fork 462
/
internal.ts
415 lines (374 loc) · 13.8 KB
/
internal.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* eslint-disable @typescript-eslint/no-explicit-any */
import { isArray, isString, isFunction } from '@intlify/shared'
import {
findBrowserLocale,
getLocalesRegex,
isI18nInstance,
isComposer,
isExportedGlobalComposer,
isVueI18n
} from 'vue-i18n-routing'
import JsCookie from 'js-cookie'
import { parse, serialize } from 'cookie-es'
import { hasProtocol } from 'ufo'
import isHTTPS from 'is-https'
import { useRequestHeaders, useRequestEvent } from '#imports'
import {
nuxtI18nOptionsDefault,
localeMessages,
additionalMessages,
NUXT_I18N_MODULE_ID
} from '#build/i18n.options.mjs'
import type { NuxtApp } from '#imports'
import type { I18nOptions, Locale, VueI18n, LocaleMessages, DefineLocaleMessage } from 'vue-i18n'
import type { Route, RouteLocationNormalized, RouteLocationNormalizedLoaded, LocaleObject } from 'vue-i18n-routing'
import type { DeepRequired } from 'ts-essentials'
import type { NuxtI18nOptions, NuxtI18nInternalOptions, DetectBrowserLanguageOptions } from '#build/i18n.options.mjs'
export function formatMessage(message: string) {
return NUXT_I18N_MODULE_ID + ' ' + message
}
function isLegacyVueI18n(target: any): target is VueI18n {
return target != null && ('__VUE_I18N_BRIDGE__' in target || '_sync' in target)
}
export function callVueI18nInterfaces(i18n: any, name: string, ...args: any[]): any {
const target: unknown = isI18nInstance(i18n) ? i18n.global : i18n
// prettier-ignore
const [obj, method] = [target, (target as any)[name]]
return Reflect.apply(method, obj, [...args])
}
export function getVueI18nPropertyValue<Return = any>(i18n: any, name: string): Return {
const target: unknown = isI18nInstance(i18n) ? i18n.global : i18n
// prettier-ignore
const ret = isComposer(target)
? (target as any)[name].value
: isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target)
? (target as any)[name]
: (target as any)[name]
return ret as Return
}
export function defineGetter<K extends string | number | symbol, V>(obj: Record<K, V>, key: K, val: V) {
Object.defineProperty(obj, key, { get: () => val })
}
export function proxyNuxt<T extends (...args: any) => any>(nuxt: NuxtApp, target: T) {
return function () {
return Reflect.apply(
target,
{
i18n: (nuxt as any).$i18n,
getRouteBaseName: nuxt.$getRouteBaseName,
localePath: nuxt.$localePath,
localeRoute: nuxt.$localeRoute,
switchLocalePath: nuxt.$switchLocalePath,
localeHead: nuxt.$localeHead,
route: (nuxt as any).$router.currentRoute.value,
router: (nuxt as any).$router,
store: undefined
},
// eslint-disable-next-line prefer-rest-params
arguments
) as (this: NuxtApp, ...args: Parameters<T>) => ReturnType<T>
}
}
/**
* Parses locales provided from browser through `accept-language` header.
*
* @param input - Accept-Language header value.
* @return An array of locale codes. Priority determined by order in array.
*/
export function parseAcceptLanguage(input: string): string[] {
// Example input: en-US,en;q=0.9,nb;q=0.8,no;q=0.7
// Contains tags separated by comma.
// Each tag consists of locale code (2-3 letter language code) and optionally country code
// after dash. Tag can also contain score after semicolon, that is assumed to match order
// so it's not explicitly used.
return input.split(',').map(tag => tag.split(';')[0])
}
async function loadMessage(context: NuxtApp, loader: () => Promise<any>) {
let message: LocaleMessages<DefineLocaleMessage> | null = null
try {
const getter = await loader().then(r => r.default || r)
// TODO: support for js, cjs, mjs
if (isFunction(getter)) {
console.error(formatMessage('Not support executable file (e.g. js, cjs, mjs)'))
} else {
message = getter
}
} catch (e: any) {
// eslint-disable-next-line no-console
console.error(formatMessage('Failed locale loading: ' + e.message))
}
return message
}
const loadedLocales: Locale[] = []
export async function loadLocale(
context: NuxtApp,
locale: Locale,
setter: (locale: Locale, message: LocaleMessages<DefineLocaleMessage>) => void
) {
if (process.server || process.dev || !loadedLocales.includes(locale)) {
const loader = localeMessages[locale]
if (loader != null) {
const message = await loadMessage(context, loader)
if (message != null) {
setter(locale, message)
loadedLocales.push(locale)
}
} else {
console.warn(formatMessage('Could not find ' + locale + ' locale in localeMessages'))
}
}
}
const loadedAdditionalLocales: Locale[] = []
export async function loadAdditionalLocale(
context: NuxtApp,
locale: Locale,
merger: (locale: Locale, message: LocaleMessages<DefineLocaleMessage>) => void
) {
if (process.server || process.dev || !loadedAdditionalLocales.includes(locale)) {
const additionalLoaders = additionalMessages[locale] || []
for (const additionalLoader of additionalLoaders) {
const message = await loadMessage(context, additionalLoader)
if (message != null) {
merger(locale, message)
loadedAdditionalLocales.push(locale)
}
}
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function getBrowserLocale(options: Required<NuxtI18nInternalOptions>, context?: any): string | undefined {
let ret: string | undefined
if (process.client) {
if (navigator.languages) {
// get browser language either from navigator if running on client side, or from the headers
ret = findBrowserLocale(options.__normalizedLocales, navigator.languages as string[])
__DEBUG__ && console.log('getBrowserLocale navigator.languages', navigator.languages)
}
} else if (process.server) {
const header = useRequestHeaders(['accept-language'])
__DEBUG__ && console.log('getBrowserLocale accept-language', header)
const accept = header['accept-language']
if (accept) {
ret = findBrowserLocale(options.__normalizedLocales, parseAcceptLanguage(accept))
}
}
return ret
}
export function getLocaleCookie(
context: any,
{
useCookie = nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie,
cookieKey = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey,
localeCodes = []
}: Pick<DetectBrowserLanguageOptions, 'useCookie' | 'cookieKey'> & {
localeCodes?: readonly string[]
} = {}
): string | undefined {
__DEBUG__ && console.log('getLocaleCookie', { useCookie, cookieKey, localeCodes })
if (useCookie) {
let localeCode: string | undefined
if (process.client) {
localeCode = JsCookie.get(cookieKey)
} else if (process.server) {
const cookie = useRequestHeaders(['cookie'])
if ('cookie' in cookie) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parsedCookie = parse((cookie as any)['cookie']) as Record<string, string>
localeCode = parsedCookie[cookieKey]
__DEBUG__ && console.log('getLocaleCookie cookie', parsedCookie[cookieKey])
}
}
if (localeCode && localeCodes.includes(localeCode)) {
return localeCode
}
}
}
export function setLocaleCookie(
locale: string,
context: any,
{
useCookie = nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie,
cookieKey = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey,
cookieDomain = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieDomain,
cookieSecure = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieSecure,
cookieCrossOrigin = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieCrossOrigin
}: Pick<
DetectBrowserLanguageOptions,
'useCookie' | 'cookieDomain' | 'cookieKey' | 'cookieSecure' | 'cookieCrossOrigin'
> = {}
) {
if (!useCookie) {
return
}
const date = new Date()
const cookieOptions: Record<string, any> = {
expires: new Date(date.setDate(date.getDate() + 365)),
path: '/',
sameSite: cookieCrossOrigin ? 'none' : 'lax',
secure: cookieCrossOrigin || cookieSecure
}
if (cookieDomain) {
cookieOptions.domain = cookieDomain
}
if (process.client) {
JsCookie.set(cookieKey, locale, cookieOptions)
} else if (process.server) {
if (context.res) {
const { res } = context
let headers = res.getHeader('Set-Cookie') || []
if (!isArray(headers)) {
headers = [String(headers)]
}
const redirectCookie = serialize(cookieKey, locale, cookieOptions)
headers.push(redirectCookie)
res.setHeader('Set-Cookie', headers)
}
}
}
export type DetectBrowserLanguageNotDetectReason =
| 'unknown'
| 'not_found_match'
| 'not_redirect_on_root'
| 'not_redirect_on_no_prefix'
export type DetectBrowserLanguageFrom = 'unknown' | 'cookie' | 'navigator_or_header' | 'fallback'
export type DetectBrowserLanguageFromResult = {
locale: string
stat: boolean
reason?: DetectBrowserLanguageNotDetectReason
from?: DetectBrowserLanguageFrom
}
export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
route: string | Route | RouteLocationNormalized | RouteLocationNormalizedLoaded,
context: any,
nuxtI18nOptions: DeepRequired<NuxtI18nOptions<Context>>,
nuxtI18nInternalOptions: DeepRequired<NuxtI18nInternalOptions>,
localeCodes: string[] = [],
locale: Locale = ''
): DetectBrowserLanguageFromResult {
const { strategy } = nuxtI18nOptions
const { redirectOn, alwaysRedirect, useCookie, fallbackLocale } =
nuxtI18nOptions.detectBrowserLanguage as DetectBrowserLanguageOptions
const path = isString(route) ? route : route.path
__DEBUG__ &&
console.log(
'detectBrowserLanguage: (path, strategy, alwaysRedirect, redirectOn, locale) -',
path,
strategy,
alwaysRedirect,
redirectOn,
locale
)
if (strategy !== 'no_prefix') {
if (redirectOn === 'root') {
if (path !== '/') {
__DEBUG__ && console.log('detectBrowserLanguage: not root')
return { locale: '', stat: false, reason: 'not_redirect_on_root' }
}
} else if (redirectOn === 'no prefix') {
if (!alwaysRedirect && path.match(getLocalesRegex(localeCodes as string[]))) {
__DEBUG__ && console.log('detectBrowserLanguage: no prefix')
return { locale: '', stat: false, reason: 'not_redirect_on_no_prefix' }
}
}
}
let localeFrom: DetectBrowserLanguageFrom = 'unknown'
let cookieLocale: string | undefined
let matchedLocale: string | undefined
// get preferred language from cookie if present and enabled
if (useCookie) {
matchedLocale = cookieLocale = getLocaleCookie(context, { ...nuxtI18nOptions.detectBrowserLanguage, localeCodes })
localeFrom = 'cookie'
__DEBUG__ && console.log('detectBrowserLanguage: cookieLocale', cookieLocale)
}
// try to get locale from either navigator or header detection
if (!matchedLocale) {
matchedLocale = getBrowserLocale(nuxtI18nInternalOptions, context)
localeFrom = 'navigator_or_header'
__DEBUG__ && console.log('detectBrowserLanguage: browserLocale', matchedLocale)
}
__DEBUG__ &&
console.log(
'detectBrowserLanguage: (matchedLocale, cookieLocale, localeFrom) -',
matchedLocale,
cookieLocale,
localeFrom
)
// set fallback locale if that is not matched locale
const finalLocale = matchedLocale || fallbackLocale
if (!matchedLocale && fallbackLocale) {
localeFrom = 'fallback'
}
__DEBUG__ &&
console.log(
'detectBrowserLanguage: first finaleLocale (finaleLocale, lcoaleForm) -',
finalLocale,
cookieLocale,
localeFrom
)
const vueI18nLocale = locale || (nuxtI18nOptions.vueI18n as I18nOptions).locale
__DEBUG__ && console.log('detectBrowserLanguage: vueI18nLocale', vueI18nLocale)
// handle cookie option to prevent multiple redirections
if (finalLocale && (!useCookie || alwaysRedirect || !cookieLocale)) {
if (strategy === 'no_prefix') {
return { locale: finalLocale, stat: true, from: localeFrom }
} else {
if (finalLocale !== vueI18nLocale /* && path !== '/'*/) {
__DEBUG__ && console.log('detectBrowserLanguage: finalLocale !== vueI18nLocale', finalLocale)
return { locale: finalLocale, stat: true, from: localeFrom }
} else {
if (alwaysRedirect && path === '/') {
return { locale: finalLocale, stat: true, from: localeFrom }
}
}
}
}
return { locale: '', stat: false, reason: 'not_found_match' }
}
export function getHost() {
let host: string | undefined
if (process.client) {
host = window.location.host
} else if (process.server) {
const header = useRequestHeaders(['x-forwarded-host', 'host'])
let detectedHost: string | undefined
if ('x-forwarded-host' in header) {
detectedHost = header['x-forwarded-host']
} else if ('host' in header) {
detectedHost = header['host']
}
host = isArray(detectedHost) ? detectedHost[0] : detectedHost
}
return host
}
export function getLocaleDomain(locales: LocaleObject[]): string {
let host = getHost() || ''
if (host) {
const matchingLocale = locales.find(locale => locale.domain === host)
if (matchingLocale) {
return matchingLocale.code
} else {
host = ''
}
}
return host
}
export function getDomainFromLocale(localeCode: Locale, locales: LocaleObject[], nuxt?: NuxtApp): string | undefined {
// lookup the `differentDomain` origin associated with given locale.
const lang = locales.find(locale => locale.code === localeCode)
if (lang && lang.domain) {
if (hasProtocol(lang.domain)) {
return lang.domain
}
let protocol
if (process.server) {
// @ts-ignore TODO: fix type error
const { req } = useRequestEvent(nuxt)
protocol = req && isHTTPS(req) ? 'https' : 'http'
} else {
protocol = window.location.protocol.split(':')[0]
}
return protocol + '://' + lang.domain
}
console.warn(formatMessage('Could not find domain name for locale ' + localeCode))
}
/* eslint-enable @typescript-eslint/no-explicit-any */