audiobookshelf/client/plugins/i18n.js

115 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-11-07 00:56:44 +01:00
import Vue from "vue"
2022-11-09 00:10:08 +01:00
import enUsStrings from '../strings/en-us.json'
import { supplant } from './utils'
2022-11-07 00:56:44 +01:00
const defaultCode = 'en-us'
const languageCodeMap = {
'de': 'Deutsch',
'en-us': 'English',
// 'es': 'Español',
2022-11-14 17:45:26 +01:00
'fr': 'Français',
2022-11-12 23:55:42 +01:00
'hr': 'Hrvatski',
2022-11-12 15:07:53 +01:00
'it': 'Italiano',
'pl': 'Polski',
2022-11-10 03:46:31 +01:00
'zh-cn': '简体中文 (Simplified Chinese)'
}
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
2022-11-08 01:27:17 +01:00
}
Vue.prototype.$strings = { ...enUsStrings }
2022-11-08 01:27:17 +01:00
Vue.prototype.$getString = (key, subs) => {
if (!Vue.prototype.$strings[key]) return ''
if (subs && Array.isArray(subs) && subs.length) {
return supplant(Vue.prototype.$strings[key], subs)
}
return Vue.prototype.$strings[key]
}
2022-11-07 00:56:44 +01:00
2022-11-09 00:10:08 +01:00
var translations = {
[defaultCode]: enUsStrings
}
2022-11-07 00:56:44 +01:00
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)
2022-11-09 00:10:08 +01:00
resolve(null)
2022-11-07 00:56:44 +01:00
})
})
}
async function loadi18n(code) {
if (!code) return false
if (Vue.prototype.$languageCodes.current == code) {
2022-11-07 00:56:44 +01:00
// already set
return false
2022-11-07 00:56:44 +01:00
}
2022-11-07 00:56:44 +01:00
const strings = translations[code] || await loadTranslationStrings(code)
2022-11-09 00:10:08 +01:00
if (!strings) {
console.warn(`Invalid lang code ${code}`)
return false
2022-11-09 00:10:08 +01:00
}
2022-11-07 00:56:44 +01:00
translations[code] = strings
Vue.prototype.$languageCodes.current = code
localStorage.setItem('lang', code)
2022-11-07 00:56:44 +01:00
for (const key in Vue.prototype.$strings) {
Vue.prototype.$strings[key] = strings[key] || translations[defaultCode][key]
}
2023-01-30 00:52:28 +01:00
console.log(`ConfigDateFnsLocale = ${translations[code].ConfigDateFnsLocale}`)
Vue.prototype.$setDateFnsLocale(translations[code].ConfigDateFnsLocale)
2022-11-07 00:56:44 +01:00
console.log('i18n strings=', Vue.prototype.$strings)
Vue.prototype.$eventBus.$emit('change-lang', code)
return true
2022-11-07 00:56:44 +01:00
}
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 {
Vue.prototype.$languageCodes.local = localLanguage
loadi18n(localLanguage)
}
}
initialize()
2022-11-07 00:56:44 +01:00