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:
parent
f6eb8cf681
commit
c93d744041
2 changed files with 13 additions and 1 deletions
|
@ -35,6 +35,9 @@ describe('country-code', () => {
|
||||||
|
|
||||||
await i18next.changeLanguage('en-CA');
|
await i18next.changeLanguage('en-CA');
|
||||||
expect(getDefaultCountryCode()).toEqual('CA');
|
expect(getDefaultCountryCode()).toEqual('CA');
|
||||||
|
|
||||||
|
await i18next.changeLanguage('ru');
|
||||||
|
expect(getDefaultCountryCode()).toEqual('RU');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('getDefaultCountryCallingCode', async () => {
|
it('getDefaultCountryCallingCode', async () => {
|
||||||
|
@ -55,6 +58,9 @@ describe('country-code', () => {
|
||||||
|
|
||||||
await i18next.changeLanguage('en-CA');
|
await i18next.changeLanguage('en-CA');
|
||||||
expect(getDefaultCountryCallingCode()).toEqual('1');
|
expect(getDefaultCountryCallingCode()).toEqual('1');
|
||||||
|
|
||||||
|
await i18next.changeLanguage('ru');
|
||||||
|
expect(getDefaultCountryCallingCode()).toEqual('7');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('getCountryList should sort properly', async () => {
|
it('getCountryList should sort properly', async () => {
|
||||||
|
|
|
@ -29,12 +29,18 @@ export const getDefaultCountryCode = (): CountryCode => {
|
||||||
const { language } = i18next;
|
const { language } = i18next;
|
||||||
|
|
||||||
// Extract the country code from language tag suffix
|
// Extract the country code from language tag suffix
|
||||||
const [, countryCode] = language.split('-');
|
const [languageCode, countryCode] = language.split('-');
|
||||||
|
|
||||||
if (countryCode && isValidCountryCode(countryCode)) {
|
if (countryCode && isValidCountryCode(countryCode)) {
|
||||||
return countryCode;
|
return countryCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const upperCaseLanguageCode = languageCode?.toUpperCase();
|
||||||
|
|
||||||
|
if (upperCaseLanguageCode && isValidCountryCode(upperCaseLanguageCode)) {
|
||||||
|
return upperCaseLanguageCode;
|
||||||
|
}
|
||||||
|
|
||||||
return countryCallingCodeMap[language] ?? fallbackCountryCode;
|
return countryCallingCodeMap[language] ?? fallbackCountryCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue