-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocaleDetector.ts
23 lines (20 loc) · 885 Bytes
/
localeDetector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Detect based on query, cookie, header
export default defineI18nLocaleDetector((event, config) => {
// try to get locale from query
const query = tryQueryLocale(event, { lang: '' }) // disable locale default value with `lang` option
if (query) {
return query.toString()
}
// try to get locale from cookie
const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' }) // disable locale default value with `lang` option
if (cookie) {
return cookie.toString()
}
// try to get locale from header (`accept-header`)
const header = tryHeaderLocale(event, { lang: '' }) // disable locale default value with `lang` option
if (header) {
return header.toString()
}
// If the locale cannot be resolved up to this point, it is resolved with the value `defaultLocale` of the locale config passed to the function
return config.defaultLocale
})