From fc5429ec59e36007097debc78b5cc20c4a13276b Mon Sep 17 00:00:00 2001 From: Milu <43616825+milupol@users.noreply.github.com> Date: Thu, 6 May 2021 18:45:49 +0200 Subject: [PATCH] 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 --- .../general-settings/general-settings.js | 4 ++++ src/renderer/main.js | 20 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/renderer/components/general-settings/general-settings.js b/src/renderer/components/general-settings/general-settings.js index 7d015b95..f4428570 100644 --- a/src/renderer/components/general-settings/general-settings.js +++ b/src/renderer/components/general-settings/general-settings.js @@ -103,6 +103,10 @@ export default Vue.extend({ const names = [] Object.keys(this.$i18n.messages).forEach((locale) => { + if (locale === 'system') { + names.push('System Language') + return + } const localeName = this.$i18n.messages[locale]['Locale Name'] if (typeof localeName !== 'undefined') { names.push(localeName) diff --git a/src/renderer/main.js b/src/renderer/main.js index 4404b54c..0cdbd45b 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -27,26 +27,36 @@ Vue.component('FontAwesomeIcon', FontAwesomeIcon) Vue.use(VueI18n) // 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 = {} /* eslint-disable-next-line */ const fileLocation = isDev ? 'static/locales/' : `${__dirname}/static/locales/` // Take active locales and load respective YAML file activeLocales.forEach((locale) => { + // Import elctrons app object to access getLocale function + const { app } = require('@electron/remote') try { // File location when running in dev - const doc = yaml.load(fs.readFileSync(`${fileLocation}${locale}.yaml`)) - messages[locale] = doc + if (locale === 'system') { + 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) { console.log(e) } }) const i18n = new VueI18n({ - locale: 'en-US', // set locale + locale: 'system', // set locale standard is to follow the systems locale 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 })