From c93d7440419e27f24d16dfa3c907b9abe8b48e8f Mon Sep 17 00:00:00 2001 From: simeng-li Date: Tue, 21 Feb 2023 10:52:41 +0800 Subject: [PATCH] fix(ui): update country code detection logic (#3161) --- packages/ui/src/utils/country-code.test.ts | 6 ++++++ packages/ui/src/utils/country-code.ts | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/utils/country-code.test.ts b/packages/ui/src/utils/country-code.test.ts index f3b770e7d..7fbef8a27 100644 --- a/packages/ui/src/utils/country-code.test.ts +++ b/packages/ui/src/utils/country-code.test.ts @@ -35,6 +35,9 @@ describe('country-code', () => { await i18next.changeLanguage('en-CA'); expect(getDefaultCountryCode()).toEqual('CA'); + + await i18next.changeLanguage('ru'); + expect(getDefaultCountryCode()).toEqual('RU'); }); it('getDefaultCountryCallingCode', async () => { @@ -55,6 +58,9 @@ describe('country-code', () => { await i18next.changeLanguage('en-CA'); expect(getDefaultCountryCallingCode()).toEqual('1'); + + await i18next.changeLanguage('ru'); + expect(getDefaultCountryCallingCode()).toEqual('7'); }); it('getCountryList should sort properly', async () => { diff --git a/packages/ui/src/utils/country-code.ts b/packages/ui/src/utils/country-code.ts index 3e49c9f0c..294c1b24c 100644 --- a/packages/ui/src/utils/country-code.ts +++ b/packages/ui/src/utils/country-code.ts @@ -29,12 +29,18 @@ export const getDefaultCountryCode = (): CountryCode => { const { language } = i18next; // Extract the country code from language tag suffix - const [, countryCode] = language.split('-'); + const [languageCode, countryCode] = language.split('-'); if (countryCode && isValidCountryCode(countryCode)) { return countryCode; } + const upperCaseLanguageCode = languageCode?.toUpperCase(); + + if (upperCaseLanguageCode && isValidCountryCode(upperCaseLanguageCode)) { + return upperCaseLanguageCode; + } + return countryCallingCodeMap[language] ?? fallbackCountryCode; };