App+Settings: Make locale changes reactive between windows

This commit moves the locale relevant code to the settings' store module
and incorporates it with its current architecture.

I should clarify that this makes the value present in the `localStorage`
nonmeaningful, as that value is no longer being used in any way.
This commit is contained in:
Svallinn 2021-06-20 02:45:37 +01:00
parent d3e6d57f20
commit 9859a7ce18
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
4 changed files with 40 additions and 73 deletions

View File

@ -88,7 +88,6 @@ export default Vue.extend({
this.grabHistory() this.grabHistory()
this.grabAllPlaylists() this.grabAllPlaylists()
this.checkThemeSettings() this.checkThemeSettings()
await this.checkLocale()
if (this.usingElectron) { if (this.usingElectron) {
console.log('User is using Electron') console.log('User is using Electron')
@ -110,35 +109,6 @@ export default Vue.extend({
}) })
}, },
methods: { methods: {
checkLocale: async function () {
const locale = localStorage.getItem('locale')
if (locale === null || locale === 'system') {
const systemLocale = await this.getLocale()
const findLocale = Object.keys(this.$i18n.messages).find((locale) => {
const localeName = locale.replace('-', '_')
return localeName.includes(systemLocale.replace('-', '_'))
})
if (typeof findLocale !== 'undefined') {
this.$i18n.locale = findLocale
localStorage.setItem('locale', 'system')
} else {
this.$i18n.locale = 'en-US'
localStorage.setItem('locale', 'en-US')
}
} else {
this.$i18n.locale = locale
}
const payload = {
isDev: this.isDev,
locale: this.$i18n.locale
}
this.getRegionData(payload)
},
checkThemeSettings: function () { checkThemeSettings: function () {
let baseTheme = localStorage.getItem('baseTheme') let baseTheme = localStorage.getItem('baseTheme')
let mainColor = localStorage.getItem('mainColor') let mainColor = localStorage.getItem('mainColor')
@ -415,9 +385,7 @@ export default Vue.extend({
'grabAllProfiles', 'grabAllProfiles',
'grabHistory', 'grabHistory',
'grabAllPlaylists', 'grabAllPlaylists',
'getRegionData',
'getYoutubeUrlInfo', 'getYoutubeUrlInfo',
'getLocale',
'getExternalPlayerCmdArgumentsData', 'getExternalPlayerCmdArgumentsData',
'setUpListenerToSyncSettings' 'setUpListenerToSyncSettings'
]) ])

View File

@ -23,7 +23,6 @@ export default Vue.extend({
showInvidiousInstances: false, showInvidiousInstances: false,
instanceNames: [], instanceNames: [],
instanceValues: [], instanceValues: [],
currentLocale: '',
backendValues: [ backendValues: [
'invidious', 'invidious',
'local' 'local'
@ -93,6 +92,9 @@ export default Vue.extend({
thumbnailPreference: function () { thumbnailPreference: function () {
return this.$store.getters.getThumbnailPreference return this.$store.getters.getThumbnailPreference
}, },
currentLocale: function () {
return this.$store.getters.getCurrentLocale
},
regionNames: function () { regionNames: function () {
return this.$store.getters.getRegionNames return this.$store.getters.getRegionNames
}, },
@ -173,8 +175,6 @@ export default Vue.extend({
}) })
this.updateInvidiousInstanceBounce = debounce(this.updateInvidiousInstance, 500) this.updateInvidiousInstanceBounce = debounce(this.updateInvidiousInstance, 500)
this.currentLocale = localStorage.getItem('locale')
}, },
beforeDestroy: function () { beforeDestroy: function () {
if (this.invidiousInstance === '') { if (this.invidiousInstance === '') {
@ -196,41 +196,6 @@ export default Vue.extend({
} }
}, },
updateLocale: async function (locale) {
if (locale === 'system') {
const systemLocale = await this.getLocale()
const findLocale = Object.keys(this.$i18n.messages).find((locale) => {
const localeName = locale.replace('-', '_')
return localeName.includes(systemLocale.replace('-', '_'))
})
if (typeof findLocale !== 'undefined') {
this.$i18n.locale = findLocale
this.currentLocale = 'system'
localStorage.setItem('locale', 'system')
} else {
// Translating this string isn't needed because the user will always see it in English
this.showToast({
message: 'Locale not found, defaulting to English (US)'
})
this.$i18n.locale = 'en-US'
this.currentLocale = 'en-US'
localStorage.setItem('locale', 'en-US')
}
} else {
this.$i18n.locale = locale
this.currentLocale = locale
localStorage.setItem('locale', locale)
}
const payload = {
isDev: this.isDev,
locale: this.currentLocale
}
this.getRegionData(payload)
},
...mapActions([ ...mapActions([
'showToast', 'showToast',
'updateEnableSearchSuggestions', 'updateEnableSearchSuggestions',
@ -245,8 +210,7 @@ export default Vue.extend({
'updateThumbnailPreference', 'updateThumbnailPreference',
'updateInvidiousInstance', 'updateInvidiousInstance',
'updateForceLocalBackendForLegacy', 'updateForceLocalBackendForLegacy',
'getRegionData', 'updateCurrentLocale'
'getLocale'
]) ])
} }
}) })

View File

@ -75,7 +75,7 @@
:value="currentLocale" :value="currentLocale"
:select-names="localeNames" :select-names="localeNames"
:select-values="localeOptions" :select-values="localeOptions"
@change="updateLocale" @change="updateCurrentLocale"
/> />
<ft-select <ft-select
:placeholder="$t('Settings.General Settings.Region for Trending')" :placeholder="$t('Settings.General Settings.Region for Trending')"

View File

@ -1,4 +1,5 @@
import { settingsDb } from '../datastores' import { settingsDb } from '../datastores'
import i18n from '../../i18n/index'
/* /*
* Due to the complexity of the settings module in FreeTube, a more * Due to the complexity of the settings module in FreeTube, a more
@ -215,6 +216,40 @@ const state = {
} }
const stateWithSideEffects = { const stateWithSideEffects = {
currentLocale: {
defaultValue: 'en-US',
sideEffectsHandler: async function ({ dispatch }, value) {
const defaultLocale = 'en-US'
let targetLocale = value
if (value === 'system') {
const systemLocale = await dispatch('getSystemLocale')
targetLocale = Object.keys(i18n.messages).find((locale) => {
const localeName = locale.replace('-', '_')
return localeName.includes(systemLocale.replace('-', '_'))
})
// Go back to default value if locale is unavailable
if (!targetLocale) {
targetLocale = defaultLocale
// Translating this string isn't necessary
// because the user will always see it in the default locale
// (in this case, English (US))
dispatch('showToast',
{ message: `Locale not found, defaulting to ${defaultLocale}` }
)
}
}
i18n.locale = targetLocale
dispatch('getRegionData', {
isDev: process.env.NODE_ENV === 'development',
locale: targetLocale
})
}
},
defaultVolume: { defaultVolume: {
defaultValue: 1, defaultValue: 1,
sideEffectsHandler: (_, value) => { sideEffectsHandler: (_, value) => {