Skip to content

Commit

Permalink
Merge pull request #6426 from hotosm/fix/6425-crash-when-language-swi…
Browse files Browse the repository at this point in the history
…tched-to-nederlands
  • Loading branch information
ramyaragupathy authored May 14, 2024
2 parents 9b3fe34 + 0f4a545 commit 2b5767a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions frontend/src/utils/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCountries, getCountry, getSupportedLangs } from '@hotosm/iso-countri

export function translateCountry(name, locale) {
const code = getCountryCode(name);
if (code && isLangSupported(locale)) return getCountry(locale.split('-')[0], code);
if (code && isLangSupported(locale)) return getCountry(locale.split(/[-_]/)[0], code);
return name;
}

Expand All @@ -20,13 +20,14 @@ export function getCountryCode(name) {
}

export function isLangSupported(code) {
if (getSupportedLangs().includes(code.split('-')[0])) return true;
// split with `-` or `_` to get the language initials
if (getSupportedLangs().includes(code.split(/[-_]/)[0])) return true;
return false;
}

export function formatCountryList(locale) {
if (locale && isLangSupported(locale)) {
const countries = getCountries(locale.split('-')[0]);
const countries = getCountries(locale.split(/[-_]/)[0]);
return Object.keys(countries).map((key) => ({ label: countries[key], value: key }));
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/internationalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const supportedLocales = [
{ value: 'ko', label: '한국어' },
// { value: 'mg', label: 'Malagasy' },
// { value: 'ml', label: 'Malayalam' },
{ value: 'nl', label: 'Nederlands' },
{ value: 'nl_NL', label: 'Nederlands' },
{ value: 'pt', label: 'Português' },
{ value: 'pt-BR', label: 'Português (Brasil)' },
// { value: 'ru', label: 'Русский язык' },
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/tests/internationalization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('getSupportedLocale', () => {
it('returns a generic supported locale if the variation is not supported', () => {
expect(getSupportedLocale('en-gb')).toEqual({ label: 'English', value: 'en' });
expect(getSupportedLocale('es-AR')).toEqual({ label: 'Español', value: 'es' });
expect(getSupportedLocale('nl-NL')).toEqual({ label: 'Nederlands', value: 'nl' });
expect(getSupportedLocale('nl_NL')).toEqual({ label: 'Nederlands', value: 'nl_NL' });
});
it('returns the default locale if the code is not supported and does not have a variation', () => {
expect(getSupportedLocale('xt')).toEqual({ label: 'English', value: 'en' });
Expand Down

0 comments on commit 2b5767a

Please sign in to comment.