0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(ui): update country code detection logic (#3161)

This commit is contained in:
simeng-li 2023-02-21 10:52:41 +08:00 committed by GitHub
parent f6eb8cf681
commit c93d744041
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -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 () => {

View file

@ -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;
};