From 5f8e33c6ad61a5507980b3a90a8c5b6b24fa6901 Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Fri, 3 Jun 2022 08:00:20 -0400 Subject: [PATCH] Improve system locale selection (#2271) * improve system language selection * fix lint * add comments --- src/renderer/store/modules/settings.js | 29 +++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index 693813d1..bc6d84e3 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -271,12 +271,31 @@ const stateWithSideEffects = { 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('-', '_')) + const systemLocaleName = (await dispatch('getSystemLocale')).replace('-', '_') // ex: en_US + const systemLocaleLang = systemLocaleName.split('_')[0] // ex: en + const targetLocaleOptions = Object.keys(i18n.messages).filter((locale) => { // filter out other languages + const localeLang = locale.replace('-', '_').split('_')[0] + return localeLang.includes(systemLocaleLang) + }).sort((a, b) => { + const aLocaleName = a.replace('-', '_') + const bLocaleName = b.replace('-', '_') + const aLocale = aLocaleName.split('_') // ex: [en, US] + const bLocale = bLocaleName.split('_') + if (aLocale.includes(systemLocaleName)) { // country & language match, prefer a + return -1 + } else if (bLocale.includes(systemLocaleName)) { // country & language match, prefer b + return 1 + } else if (aLocale.length === 1) { // no country code for a, prefer a + return -1 + } else if (bLocale.length === 1) { // no country code for b, prefer b + return 1 + } else { // a & b have different country code from system, sort alphabetically + return aLocaleName.localeCompare(bLocaleName) + } }) + if (targetLocaleOptions.length > 0) { + targetLocale = targetLocaleOptions[0] + } // Go back to default value if locale is unavailable if (!targetLocale) {