Adding a "System Language" option to the "Locale Preference" (#1241)

Closes: #1205

* Added "Follow System" option to
`Settings -> General Settings -> Locale Preference`
and set it to default

* Changed double quotes to single quotes

* Removed unnecessary comment

* Changed `app` accessing method

* Cleaned up if/else statements

* Checks similar locales as fallback

* Changed label to "System Language"

* Changed locale filtering logic to use the filter() method
This commit is contained in:
Milu 2021-05-06 18:45:49 +02:00 committed by GitHub
parent 99fa539c61
commit fc5429ec59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -103,6 +103,10 @@ export default Vue.extend({
const names = [] const names = []
Object.keys(this.$i18n.messages).forEach((locale) => { Object.keys(this.$i18n.messages).forEach((locale) => {
if (locale === 'system') {
names.push('System Language')
return
}
const localeName = this.$i18n.messages[locale]['Locale Name'] const localeName = this.$i18n.messages[locale]['Locale Name']
if (typeof localeName !== 'undefined') { if (typeof localeName !== 'undefined') {
names.push(localeName) names.push(localeName)

View File

@ -27,26 +27,36 @@ Vue.component('FontAwesomeIcon', FontAwesomeIcon)
Vue.use(VueI18n) Vue.use(VueI18n)
// List of locales approved for use // List of locales approved for use
const activeLocales = ['en-US', 'en_GB', 'ar', 'bg', 'cs', 'da', 'de-DE', 'el', 'es', 'es-MX', 'fi', 'fr-FR', 'gl', 'he', 'hu', 'hr', 'id', 'is', 'it', 'ja', 'nb_NO', 'nl', 'nn', 'pl', 'pt', 'pt-BR', 'pt-PT', 'ru', 'sk', 'sl', 'sv', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW'] const activeLocales = ['system', 'en-US', 'en_GB', 'ar', 'bg', 'cs', 'da', 'de-DE', 'el', 'es', 'es-MX', 'fi', 'fr-FR', 'gl', 'he', 'hu', 'hr', 'id', 'is', 'it', 'ja', 'nb_NO', 'nl', 'nn', 'pl', 'pt', 'pt-BR', 'pt-PT', 'ru', 'sk', 'sl', 'sv', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW']
const messages = {} const messages = {}
/* eslint-disable-next-line */ /* eslint-disable-next-line */
const fileLocation = isDev ? 'static/locales/' : `${__dirname}/static/locales/` const fileLocation = isDev ? 'static/locales/' : `${__dirname}/static/locales/`
// Take active locales and load respective YAML file // Take active locales and load respective YAML file
activeLocales.forEach((locale) => { activeLocales.forEach((locale) => {
// Import elctrons app object to access getLocale function
const { app } = require('@electron/remote')
try { try {
// File location when running in dev // File location when running in dev
const doc = yaml.load(fs.readFileSync(`${fileLocation}${locale}.yaml`)) if (locale === 'system') {
messages[locale] = doc const systemsLocale = activeLocales.filter((currentValue) => { return currentValue.startsWith(app.getLocale()) })
if (systemsLocale.length) {
const doc = yaml.load(fs.readFileSync(`${fileLocation}${systemsLocale[0]}.yaml`))
messages[locale] = doc
}
} else {
const doc = yaml.load(fs.readFileSync(`${fileLocation}${locale}.yaml`))
messages[locale] = doc
}
} catch (e) { } catch (e) {
console.log(e) console.log(e)
} }
}) })
const i18n = new VueI18n({ const i18n = new VueI18n({
locale: 'en-US', // set locale locale: 'system', // set locale standard is to follow the systems locale
fallbackLocale: { fallbackLocale: {
default: 'en-US' default: 'en-US' // for the case systems locale has no corresponding .yaml file en-US gets set
}, },
messages // set locale messages messages // set locale messages
}) })