mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-09 17:04:58 +02:00
Add:Start of localization i18n #1103
This commit is contained in:
parent
3282ac67e4
commit
eb463a2958
13 changed files with 367 additions and 55 deletions
47
client/plugins/i18n.js
Normal file
47
client/plugins/i18n.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
import Vue from "vue"
|
||||
|
||||
const defaultCode = 'en-us'
|
||||
|
||||
Vue.prototype.$i18nCode = ''
|
||||
Vue.prototype.$strings = {}
|
||||
|
||||
var translations = {}
|
||||
|
||||
function loadTranslationStrings(code) {
|
||||
return new Promise((resolve) => {
|
||||
import(`../strings/${code}`).then((fileContents) => {
|
||||
resolve(fileContents.default)
|
||||
}).catch((error) => {
|
||||
console.error('Failed to load i18n strings', code, error)
|
||||
resolve({})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function loadi18n(code) {
|
||||
if (Vue.prototype.$i18nCode == code) {
|
||||
// already set
|
||||
return
|
||||
}
|
||||
|
||||
const currentCode = Vue.prototype.$i18nCode
|
||||
const strings = translations[code] || await loadTranslationStrings(code)
|
||||
|
||||
translations[code] = strings
|
||||
Vue.prototype.$i18nCode = code
|
||||
|
||||
if (!currentCode) {
|
||||
// first load
|
||||
Vue.prototype.$strings = strings
|
||||
return
|
||||
}
|
||||
|
||||
for (const key in Vue.prototype.$strings) {
|
||||
Vue.prototype.$strings[key] = strings[key] || translations[defaultCode][key]
|
||||
}
|
||||
console.log('i18n strings=', Vue.prototype.$strings)
|
||||
}
|
||||
|
||||
Vue.prototype.$i18nUpdate = loadi18n
|
||||
|
||||
loadi18n(defaultCode)
|
Loading…
Add table
Add a link
Reference in a new issue