mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-30 15:45:14 +02:00
Update:Set device language to match the first server connected to #448
This commit is contained in:
parent
9298065934
commit
fdb26b7700
3 changed files with 44 additions and 4 deletions
|
@ -117,6 +117,9 @@ export default {
|
||||||
deviceData() {
|
deviceData() {
|
||||||
return this.$store.state.deviceData || {}
|
return this.$store.state.deviceData || {}
|
||||||
},
|
},
|
||||||
|
deviceSettings() {
|
||||||
|
return this.deviceData.deviceSettings || {}
|
||||||
|
},
|
||||||
networkConnected() {
|
networkConnected() {
|
||||||
return this.$store.state.networkConnected
|
return this.$store.state.networkConnected
|
||||||
},
|
},
|
||||||
|
@ -800,6 +803,7 @@ export default {
|
||||||
|
|
||||||
this.$store.commit('setServerSettings', serverSettings)
|
this.$store.commit('setServerSettings', serverSettings)
|
||||||
this.$store.commit('libraries/setEReaderDevices', ereaderDevices)
|
this.$store.commit('libraries/setEReaderDevices', ereaderDevices)
|
||||||
|
this.$setServerLanguageCode(serverSettings.language)
|
||||||
|
|
||||||
// Set library - Use last library if set and available fallback to default user library
|
// Set library - Use last library if set and available fallback to default user library
|
||||||
var lastLibraryId = await this.$localStore.getLastLibraryId()
|
var lastLibraryId = await this.$localStore.getLastLibraryId()
|
||||||
|
@ -816,6 +820,19 @@ export default {
|
||||||
|
|
||||||
var serverConnectionConfig = await this.$db.setServerConnectionConfig(this.serverConfig)
|
var serverConnectionConfig = await this.$db.setServerConnectionConfig(this.serverConfig)
|
||||||
|
|
||||||
|
// Set the device language to match the servers if this is the first server connection
|
||||||
|
if (!this.serverConnectionConfigs.length && serverSettings.language !== 'en-us') {
|
||||||
|
const deviceSettings = {
|
||||||
|
...this.deviceSettings,
|
||||||
|
languageCode: serverSettings.language
|
||||||
|
}
|
||||||
|
const updatedDeviceData = await this.$db.updateDeviceSettings(deviceSettings)
|
||||||
|
if (updatedDeviceData) {
|
||||||
|
this.$store.commit('setDeviceData', updatedDeviceData)
|
||||||
|
this.$setLanguageCode(updatedDeviceData.deviceSettings?.languageCode || 'en-us')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.$store.commit('user/setUser', user)
|
this.$store.commit('user/setUser', user)
|
||||||
this.$store.commit('user/setServerConnectionConfig', serverConnectionConfig)
|
this.$store.commit('user/setServerConnectionConfig', serverConnectionConfig)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import Vue from "vue"
|
||||||
import enUsStrings from '../strings/en-us.json'
|
import enUsStrings from '../strings/en-us.json'
|
||||||
|
|
||||||
const defaultCode = 'en-us'
|
const defaultCode = 'en-us'
|
||||||
|
let $localStore = null
|
||||||
|
|
||||||
const languageCodeMap = {
|
const languageCodeMap = {
|
||||||
'cs': { label: 'Čeština', dateFnsLocale: 'cs' },
|
'cs': { label: 'Čeština', dateFnsLocale: 'cs' },
|
||||||
|
@ -85,7 +86,7 @@ async function loadi18n(code) {
|
||||||
|
|
||||||
translations[code] = strings
|
translations[code] = strings
|
||||||
Vue.prototype.$languageCodes.current = code
|
Vue.prototype.$languageCodes.current = code
|
||||||
localStorage.setItem('lang', code)
|
$localStore.setLanguage(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]
|
||||||
|
@ -115,16 +116,19 @@ Vue.prototype.$setServerLanguageCode = (code) => {
|
||||||
|
|
||||||
// Initialize with language code in localStorage if valid
|
// Initialize with language code in localStorage if valid
|
||||||
async function initialize() {
|
async function initialize() {
|
||||||
const localLanguage = localStorage.getItem('lang')
|
const localLanguage = await $localStore.getLanguage()
|
||||||
if (!localLanguage) return
|
if (!localLanguage) return
|
||||||
|
|
||||||
if (!languageCodeMap[localLanguage]) {
|
if (!languageCodeMap[localLanguage]) {
|
||||||
console.warn('Invalid local language code', localLanguage)
|
console.warn('Invalid local language code', localLanguage)
|
||||||
localStorage.setItem('lang', defaultCode)
|
$localStore.setLanguage(defaultCode)
|
||||||
} else {
|
} else {
|
||||||
Vue.prototype.$languageCodes.local = localLanguage
|
Vue.prototype.$languageCodes.local = localLanguage
|
||||||
loadi18n(localLanguage)
|
loadi18n(localLanguage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
initialize()
|
|
||||||
|
|
||||||
|
export default ({ app, store }, inject) => {
|
||||||
|
$localStore = app.$localStore
|
||||||
|
initialize()
|
||||||
|
}
|
|
@ -160,6 +160,25 @@ class LocalStorage {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setLanguage(lang) {
|
||||||
|
try {
|
||||||
|
await Preferences.set({ key: 'lang', value: lang })
|
||||||
|
console.log('[LocalStorage] Set lang', lang)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[LocalStorage] Failed to set lang', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getLanguage() {
|
||||||
|
try {
|
||||||
|
var obj = await Preferences.get({ key: 'lang' }) || {}
|
||||||
|
return obj.value || null
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[LocalStorage] Failed to get lang', error)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue