Add:Server setting for default language #1103

This commit is contained in:
advplyr 2022-11-08 18:09:07 -06:00
parent 400e34a4c7
commit 6e064eeafb
8 changed files with 91 additions and 24 deletions

View file

@ -1,20 +1,31 @@
import Vue from "vue"
import enUsStrings from '../strings/en-us.json'
import { supplant } from './utils'
const defaultCode = 'en-us'
function supplant(str, subs) {
// source: http://crockford.com/javascript/remedial.html
return str.replace(/{([^{}]*)}/g,
function (a, b) {
var r = subs[b]
return typeof r === 'string' || typeof r === 'number' ? r : a
}
)
const languageCodeMap = {
'en-us': 'English',
'es': 'Español',
'it': 'Italiano',
'pl': 'Polski',
'zh-cn': '汉语 (简化字)'
}
Vue.prototype.$languageCodeOptions = Object.keys(languageCodeMap).map(code => {
return {
text: languageCodeMap[code],
value: code
}
})
Vue.prototype.$languageCodes = {
default: defaultCode,
current: defaultCode,
local: null,
server: null
}
Vue.prototype.$i18nCode = ''
Vue.prototype.$strings = enUsStrings
Vue.prototype.$strings = { ...enUsStrings }
Vue.prototype.$getString = (key, subs) => {
if (!Vue.prototype.$strings[key]) return ''
if (subs && Array.isArray(subs) && subs.length) {
@ -39,28 +50,58 @@ function loadTranslationStrings(code) {
}
async function loadi18n(code) {
if (Vue.prototype.$i18nCode == code) {
if (!code) return false
if (Vue.prototype.$languageCodes.current == code) {
// already set
return
return false
}
const strings = translations[code] || await loadTranslationStrings(code)
if (!strings) {
console.warn(`Invalid lang code ${code}`)
return
return false
}
translations[code] = strings
Vue.prototype.$i18nCode = code
Vue.prototype.$languageCodes.current = code
localStorage.setItem('lang', code)
for (const key in Vue.prototype.$strings) {
Vue.prototype.$strings[key] = strings[key] || translations[defaultCode][key]
}
console.log('i18n strings=', Vue.prototype.$strings)
return true
}
Vue.prototype.$i18nUpdate = loadi18n
Vue.prototype.$setLanguageCode = loadi18n
// Set the servers default language code, does not override users local language code
Vue.prototype.$setServerLanguageCode = (code) => {
if (!code) return
if (!languageCodeMap[code]) {
console.warn('invalid server language in', code)
} else {
Vue.prototype.$languageCodes.server = code
if (!Vue.prototype.$languageCodes.local && code !== defaultCode) {
loadi18n(code)
}
}
}
// Initialize with language code in localStorage if valid
async function initialize() {
const localLanguage = localStorage.getItem('lang')
if (!localLanguage) return
if (!languageCodeMap[localLanguage]) {
console.warn('Invalid local language code', localLanguage)
localStorage.setItem('lang', defaultCode)
} else if (localLanguage !== defaultCode) {
Vue.prototype.$languageCodes.local = localLanguage
loadi18n(localLanguage)
}
}
initialize()
const localLanguage = localStorage.getItem('lang')
if (localLanguage !== defaultCode) {
loadi18n(localLanguage)
}