From 4898ff4a6a7bfc83a10ddc2f32abb2da82f4819f Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Thu, 7 Oct 2021 08:04:11 +0200 Subject: refactor: locale selection cleanup (#2031) --- src/config.ts | 1 - src/helpers/i18n-helpers.ts | 56 +++++++++++++++++++-------------------------- src/stores/AppStore.js | 47 ++++++++++++------------------------- 3 files changed, 38 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/config.ts b/src/config.ts index fe9145021..82ce131f9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -198,7 +198,6 @@ export const DEFAULT_APP_SETTINGS = { spellcheckerLanguage: 'en-us', darkMode: false, splitMode: false, - locale: '', fallbackLocale: 'en-US', beta: false, isAppMuted: false, diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts index ec7dc8e98..e6c66c7d3 100644 --- a/src/helpers/i18n-helpers.ts +++ b/src/helpers/i18n-helpers.ts @@ -1,52 +1,44 @@ -export function getLocale({ - locale, locales, defaultLocale, fallbackLocale, -}) { - let localeStr = locale; - if (locales[locale] === undefined) { +export function getLocale({ locale, locales, fallbackLocale }) { + if (!locale) { + return fallbackLocale; + } + + if (!locales[locale]) { let localeFuzzy: string | undefined; for (const localStr of Object.keys(locales)) { - if (locales && Object.hasOwnProperty.call(locales, localStr) && locale.slice(0, 2) === localStr.slice(0, 2)) { - localeFuzzy = localStr; - } + if (locale.slice(0, 2) === localStr.slice(0, 2)) { + localeFuzzy = localStr; + } } - if (localeFuzzy !== undefined) { - localeStr = localeFuzzy; + if (localeFuzzy) { + return localeFuzzy; } } - if (locales[localeStr] === undefined) { - localeStr = defaultLocale; - } - - if (!localeStr) { - localeStr = fallbackLocale; - } - - return localeStr; + return locale; } export function getSelectOptions({ - locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, + locales, + resetToDefaultText = '', + automaticDetectionText = '', + sort = true, }) { const options: object[] = []; if (resetToDefaultText) { - options.push( - { - value: '', - label: resetToDefaultText, - }, - ); + options.push({ + value: '', + label: resetToDefaultText, + }); } if (automaticDetectionText) { - options.push( - { - value: 'automatic', - label: automaticDetectionText, - }, - ); + options.push({ + value: 'automatic', + label: automaticDetectionText, + }); } options.push({ diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 3d9d2b551..a86a54c6d 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js @@ -17,16 +17,8 @@ import { readJsonSync } from 'fs-extra'; import Store from './lib/Store'; import Request from './lib/Request'; import { CHECK_INTERVAL, DEFAULT_APP_SETTINGS } from '../config'; -import { - isMac, - electronVersion, - osRelease, -} from '../environment'; -import { - ferdiVersion, - userDataPath, - ferdiLocale, -} from '../environment-remote'; +import { isMac, electronVersion, osRelease } from '../environment'; +import { ferdiVersion, userDataPath, ferdiLocale } from '../environment-remote'; import locales from '../i18n/translations'; import { getLocale } from '../helpers/i18n-helpers'; @@ -41,8 +33,6 @@ const debug = require('debug')('Ferdi:AppStore'); const mainWindow = getCurrentWindow(); -const defaultLocale = DEFAULT_APP_SETTINGS.locale; - const executablePath = isMac ? remoteProcess.execPath : process.execPath; const autoLauncher = new AutoLaunch({ name: 'Ferdi', @@ -80,9 +70,9 @@ export default class AppStore extends Store { @observable timeOfflineStart; - @observable updateStatus = null; + @observable updateStatus = ''; - @observable locale = defaultLocale; + @observable locale = ferdiLocale; @observable isSystemMuteOverridden = false; @@ -408,7 +398,7 @@ export default class AppStore extends Store { } @action _resetUpdateStatus() { - this.updateStatus = null; + this.updateStatus = ''; } @action _healthCheck() { @@ -480,23 +470,13 @@ export default class AppStore extends Store { } _setLocale() { - let locale; - if (this.stores.user.isLoggedIn) { - locale = this.stores.user.data.locale; - } - - if ( - locale && - Object.prototype.hasOwnProperty.call(locales, locale) && - locale !== this.locale - ) { - this.locale = locale; - } else if (!locale) { + if (this.stores.user.isLoggedIn && this.stores.user.data.locale) { + this.locale = this.stores.user.data.locale; + } else if (!this.locale) { this.locale = this._getDefaultLocale(); } moment.locale(this.locale); - debug(`Set locale to "${this.locale}"`); } @@ -504,7 +484,6 @@ export default class AppStore extends Store { return getLocale({ locale: ferdiLocale, locales, - defaultLocale, fallbackLocale: DEFAULT_APP_SETTINGS.fallbackLocale, }); } @@ -523,10 +502,12 @@ export default class AppStore extends Store { _handleFullScreen() { const body = document.querySelector('body'); - if (this.isFullScreen) { - body.classList.add('isFullScreen'); - } else { - body.classList.remove('isFullScreen'); + if (body) { + if (this.isFullScreen) { + body.classList.add('isFullScreen'); + } else { + body.classList.remove('isFullScreen'); + } } } -- cgit v1.2.3-54-g00ecf