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/helpers/i18n-helpers.ts | 56 +++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'src/helpers') 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({ -- cgit v1.2.3-54-g00ecf