feat: i18n for nodejs

This commit is contained in:
Xmader 2020-11-24 06:01:02 -05:00
parent 688e0a4c7e
commit 40d1ddbab8
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
1 changed files with 12 additions and 1 deletions

View File

@ -1,4 +1,6 @@
import isNodeJs from 'detect-node'
import en from './en'
import es from './es'
@ -24,8 +26,17 @@ const locales = (<L extends { [n: string]: LOCALE } /** type checking */> (l: L)
// detect browser language
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 _lang = navigator.languages.find(l => {
const _lang = userLangs.find(l => {
// find the first occurrence of valid languages
return names.includes(l)
})