-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbrowser.ts
28 lines (22 loc) · 968 Bytes
/
browser.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
import { locales, locale } from './core'
import { get } from 'svelte/store'
interface IENavigatorLanguage {
userLanguage?: string
}
export const getBrowserLocale = (defaultLocale = 'en') => {
const targets = typeof window === 'undefined'
? [defaultLocale] // there is no window (sapper | node/webpack)
: window.navigator.languages || // user language preferences list
[
(window.navigator as IENavigatorLanguage).userLanguage || // IE 10-
window.navigator.language, // browser ui language
]
const currentLocales = get(locales)
for (let i = 0; i < targets.length; i = i + 1) {
if (currentLocales.includes(targets[i])) return targets[i] // exact match
const bestMatch = currentLocales.find((locale: any) => targets[i].startsWith(locale))
if (bestMatch) return bestMatch // en-US -> en
}
const currentLocale = get(locale)
return currentLocale || currentLocales[0] // default to current or just first
}