feat: i18n for nodejs
This commit is contained in:
parent
688e0a4c7e
commit
40d1ddbab8
1 changed files with 12 additions and 1 deletions
|
@ -1,4 +1,6 @@
|
||||||
|
|
||||||
|
import isNodeJs from 'detect-node'
|
||||||
|
|
||||||
import en from './en'
|
import en from './en'
|
||||||
import es from './es'
|
import es from './es'
|
||||||
|
|
||||||
|
@ -24,8 +26,17 @@ const locales = (<L extends { [n: string]: LOCALE } /** type checking */> (l: L)
|
||||||
|
|
||||||
// detect browser language
|
// detect browser language
|
||||||
const lang = (() => {
|
const lang = (() => {
|
||||||
|
let userLangs: readonly string[]
|
||||||
|
if (!isNodeJs) {
|
||||||
|
userLangs = navigator.languages
|
||||||
|
} else {
|
||||||
|
const env = process.env
|
||||||
|
const l = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE || ''
|
||||||
|
userLangs = [l.slice(0, 2)]
|
||||||
|
}
|
||||||
|
|
||||||
const names = Object.keys(locales)
|
const names = Object.keys(locales)
|
||||||
const _lang = navigator.languages.find(l => {
|
const _lang = userLangs.find(l => {
|
||||||
// find the first occurrence of valid languages
|
// find the first occurrence of valid languages
|
||||||
return names.includes(l)
|
return names.includes(l)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue