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

@ -72,9 +72,14 @@
</ui-tooltip> </ui-tooltip>
</div> </div>
<div class="flex items-center py-2"> <div class="py-2">
<p class="pr-4">{{ $strings.LabelSettingsDateFormat }}</p> <p class="px-1 text-sm font-semibold">{{ $strings.LabelSettingsDateFormat }}</p>
<ui-dropdown v-model="newServerSettings.dateFormat" :items="dateFormats" small class="max-w-40" @input="(val) => updateSettingsKey('dateFormat', val)" /> <ui-dropdown v-model="newServerSettings.dateFormat" :items="dateFormats" small class="max-w-48" @input="(val) => updateSettingsKey('dateFormat', val)" />
</div>
<div class="py-2">
<p class="px-1 text-sm font-semibold">{{ $strings.LabelLanguageDefaultServer }}</p>
<ui-dropdown v-model="newServerSettings.language" :items="$languageCodeOptions" small class="max-w-48" @input="updateServerLanguage" />
</div> </div>
</div> </div>
@ -321,6 +326,10 @@ export default {
bookshelfView: !val ? this.$constants.BookshelfView.DETAIL : this.$constants.BookshelfView.STANDARD bookshelfView: !val ? this.$constants.BookshelfView.DETAIL : this.$constants.BookshelfView.STANDARD
}) })
}, },
updateServerLanguage(val) {
this.$setLanguageCode(val)
this.updateSettingsKey('language', val)
},
updateSettingsKey(key, val) { updateSettingsKey(key, val) {
this.updateServerSettings({ this.updateServerSettings({
[key]: val [key]: val

View File

@ -128,6 +128,7 @@ export default {
this.$store.commit('setServerSettings', serverSettings) this.$store.commit('setServerSettings', serverSettings)
this.$store.commit('setSource', Source) this.$store.commit('setSource', Source)
this.$store.commit('feeds/setFeeds', feeds) this.$store.commit('feeds/setFeeds', feeds)
this.$setServerLanguageCode(serverSettings.language)
if (serverSettings.chromecastEnabled) { if (serverSettings.chromecastEnabled) {
console.log('Chromecast enabled import script') console.log('Chromecast enabled import script')
@ -189,6 +190,7 @@ export default {
this.processing = false this.processing = false
this.isInit = res.isInit this.isInit = res.isInit
this.showInitScreen = !res.isInit this.showInitScreen = !res.isInit
this.$setServerLanguageCode(res.language)
if (this.showInitScreen) { if (this.showInitScreen) {
this.ConfigPath = res.ConfigPath || '' this.ConfigPath = res.ConfigPath || ''
this.MetadataPath = res.MetadataPath || '' this.MetadataPath = res.MetadataPath || ''

View File

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

View File

@ -135,3 +135,13 @@ Vue.prototype.$parseCronExpression = (expression) => {
description: `Run every ${weekdayText} at ${pieces[1]}:${pieces[0].padStart(2, '0')}` description: `Run every ${weekdayText} at ${pieces[1]}:${pieces[0].padStart(2, '0')}`
} }
} }
export 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
}
)
}

View File

@ -204,6 +204,7 @@
"LabelInvalidParts": "Invalid Parts", "LabelInvalidParts": "Invalid Parts",
"LabelItem": "Item", "LabelItem": "Item",
"LabelLanguage": "Language", "LabelLanguage": "Language",
"LabelLanguageDefaultServer": "Default Server Language",
"LabelLastSeen": "Last Seen", "LabelLastSeen": "Last Seen",
"LabelLastTime": "Last Time", "LabelLastTime": "Last Time",
"LabelLastUpdate": "Last Update", "LabelLastUpdate": "Last Update",

View File

@ -1,5 +1,5 @@
{ {
"ButtonHome": "Home", "ButtonHome": "主页",
"ButtonLatest": "Latest", "ButtonLatest": "Latest",
"ButtonLibrary": "Library", "ButtonLibrary": "Library",
"ButtonSeries": "Series", "ButtonSeries": "Series",

View File

@ -254,7 +254,8 @@ class Server {
// status check for client to see if server has been initialized // status check for client to see if server has been initialized
// server has been initialized if a root user exists // server has been initialized if a root user exists
const payload = { const payload = {
isInit: this.db.hasRootUser isInit: this.db.hasRootUser,
language: this.db.serverSettings.language
} }
if (!payload.isInit) { if (!payload.isInit) {
payload.ConfigPath = global.ConfigPath payload.ConfigPath = global.ConfigPath

View File

@ -53,6 +53,7 @@ class ServerSettings {
this.chromecastEnabled = false this.chromecastEnabled = false
this.enableEReader = false this.enableEReader = false
this.dateFormat = 'MM/dd/yyyy' this.dateFormat = 'MM/dd/yyyy'
this.language = 'en-us'
this.logLevel = Logger.logLevel this.logLevel = Logger.logLevel
@ -102,6 +103,7 @@ class ServerSettings {
this.chromecastEnabled = !!settings.chromecastEnabled this.chromecastEnabled = !!settings.chromecastEnabled
this.enableEReader = !!settings.enableEReader this.enableEReader = !!settings.enableEReader
this.dateFormat = settings.dateFormat || 'MM/dd/yyyy' this.dateFormat = settings.dateFormat || 'MM/dd/yyyy'
this.language = settings.language || 'en-us'
this.logLevel = settings.logLevel || Logger.logLevel this.logLevel = settings.logLevel || Logger.logLevel
this.version = settings.version || null this.version = settings.version || null
@ -153,6 +155,7 @@ class ServerSettings {
chromecastEnabled: this.chromecastEnabled, chromecastEnabled: this.chromecastEnabled,
enableEReader: this.enableEReader, enableEReader: this.enableEReader,
dateFormat: this.dateFormat, dateFormat: this.dateFormat,
language: this.language,
logLevel: this.logLevel, logLevel: this.logLevel,
version: this.version version: this.version
} }