Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TM crash issue when language switched to Nederlands #6426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading