Skip to content

Commit

Permalink
Merge pull request #1633 from hapifhir/do-20240522-i18n-locale-infer
Browse files Browse the repository at this point in the history
Add utility to infer locale from region code
  • Loading branch information
grahamegrieve authored May 23, 2024
2 parents dd608c5 + f1fdd51 commit 8b5fbbb
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.hl7.fhir.utilities.i18n;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* Infers an <a href="http://terminology.hl7.org/5.5.0/CodeSystem-v3-ietf3066.html">IETF BCP 47</a> locale from a <a href="http://terminology.hl7.org/5.5.0/CodeSystem-ISO3166Part1.html">ISO3166</a> country code.
*/
public class RegionToLocaleMapper {

private RegionToLocaleMapper() {
throw new IllegalStateException("Utility class");
}

static final Map<String, Locale> codeToLocale = new HashMap<>();
static {
addLocaleForRegions(new Locale("es", "AR"), "AR", "ARG");
addLocaleForRegions(new Locale("en", "AU"), "AU", "AUS");
addLocaleForRegions(new Locale("de", "AT"), "AT", "AUT");
addLocaleForRegions(new Locale("pt", "BR"), "BR", "BRA");
addLocaleForRegions(new Locale("es", "CL"), "CL", "CHL");
addLocaleForRegions(new Locale("zh", "CN"), "CN", "CHN");
addLocaleForRegions(new Locale("es", "CO"), "CO", "COL");
addLocaleForRegions(new Locale("hr", "HR"), "HR", "HRV");
addLocaleForRegions(new Locale("cz", "CZ"), "CZ", "CZE");
addLocaleForRegions(new Locale("da", "DK"), "DK", "DNK");
addLocaleForRegions(new Locale("fi", "FI"), "FI", "FIN");
addLocaleForRegions(new Locale("fr", "FR"), "FR", "FRA");
addLocaleForRegions(new Locale("de", "DE"), "DE", "DEU");
addLocaleForRegions(new Locale("el", "GR"), "GR", "GRC");
addLocaleForRegions(new Locale("zh", "HK"), "HK", "HKG");
addLocaleForRegions(new Locale("hi", "IN"), "IN", "IND");
addLocaleForRegions(new Locale("it", "IT"), "IT", "ITA");
addLocaleForRegions(new Locale("ja", "JP"), "JP", "JPN");
addLocaleForRegions(new Locale("ko", "KR"), "KR", "KOR");
addLocaleForRegions(new Locale("es", "MX"), "MX", "MEX");
addLocaleForRegions(new Locale("nl", "NL"), "NL", "NLD");
addLocaleForRegions(new Locale("en", "NZ"), "NZ", "NZL");
addLocaleForRegions(new Locale("no", "NO"), "NO", "NOR");
addLocaleForRegions(new Locale("es", "PE"), "PE", "PER");
addLocaleForRegions(new Locale("es", "PH"), "PH", "PHL");
addLocaleForRegions(new Locale("pl", "PL"), "PL", "POL");
addLocaleForRegions(new Locale("pt", "PT"), "PT", "PRT");
addLocaleForRegions(new Locale("hu", "RO"), "RO", "ROU");
addLocaleForRegions(new Locale("ru", "RU"), "RU", "RUS");
addLocaleForRegions(new Locale("en", "SG"), "SG", "SGP");
addLocaleForRegions(new Locale("sk", "SK"), "SK", "SVK");
addLocaleForRegions(new Locale("sl", "SI"), "SI", "SVN");
addLocaleForRegions(new Locale("es", "ES"), "ES", "ESP");
addLocaleForRegions(new Locale("sv", "SE"), "SE", "SWE");
addLocaleForRegions(new Locale("zh", "TW"), "TW", "TWN");
addLocaleForRegions(new Locale("ar", "AE"), "AE", "ARE");
addLocaleForRegions(new Locale("en", "GB"), "GB", "GBR");
addLocaleForRegions(new Locale("uk", "UA"), "UA", "UKR");
}

private static void addLocaleForRegions(Locale locale, String... regions) {
for (String region : regions) {
codeToLocale.put(region, locale);
}
}
public static Locale getLocaleFromRegion(String countryCode){
return codeToLocale.get(countryCode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.hl7.fhir.utilities.i18n;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.Locale;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

class RegionToLocaleMapperTest {
@ParameterizedTest
@CsvSource({
"AR, es-AR",
"ARG, es-AR",
"AU, en-AU",
"AUS, en-AU",
"AT, de-AT",
"AUT, de-AT",
"BR, pt-BR",
"BRA, pt-BR",
"CL, es-CL",
"CHL, es-CL",
"CN, zh-CN",
"CHN, zh-CN",
"CO, es-CO",
"COL, es-CO",
"HR, hr-HR",
"HRV, hr-HR",
"CZ, cz-CZ",
"CZE, cz-CZ",
"DK, da-DK",
"DNK, da-DK",
"FI, fi-FI",
"FIN, fi-FI",
"FR, fr-FR",
"FRA, fr-FR",
"DE, de-DE",
"DEU, de-DE",
"GR, el-GR",
"GRC, el-GR",
"HK, zh-HK",
"HKG, zh-HK",
"IN, hi-IN",
"IND, hi-IN",
"IT, it-IT",
"ITA, it-IT",
"JP, ja-JP",
"JPN, ja-JP",
"KR, ko-KR",
"KOR, ko-KR",
"MX, es-MX",
"MEX, es-MX",
"NL, nl-NL",
"NLD, nl-NL",
"NZ, en-NZ",
"NZL, en-NZ",
"NO, no-NO",
"NOR, no-NO",
"PE, es-PE",
"PER, es-PE",
"PH, es-PH",
"PHL, es-PH",
"PL, pl-PL",
"POL, pl-PL",
"PT, pt-PT",
"PRT, pt-PT",
"RO, hu-RO",
"ROU, hu-RO",
"RU, ru-RU",
"RUS, ru-RU",
"SG, en-SG",
"SGP, en-SG",
"SK, sk-SK",
"SVK, sk-SK",
"SI, sl-SI",
"SVN, sl-SI",
"ES, es-ES",
"ESP, es-ES",
"SE, sv-SE",
"SWE, sv-SE",
"TW, zh-TW",
"TWN, zh-TW",
"AE, ar-AE",
"ARE, ar-AE",
"GB, en-GB",
"GBR, en-GB",
"UA, uk-UA",
"UKR, uk-UA"
})
void testOurRegionsToLocales(String countryCode, String locale) {
assertEquals(Locale.forLanguageTag(locale), RegionToLocaleMapper.getLocaleFromRegion(countryCode));
}

@Test
void testUnmappedRegion() {
// Sorry Tuvalu, you are a tiny, isolated nation.
Assertions.assertNull(RegionToLocaleMapper.getLocaleFromRegion("TUV"));
}

@Test
void testGarbageRegion() {
Assertions.assertNull(RegionToLocaleMapper.getLocaleFromRegion("LIMARIA"));
}
}

0 comments on commit 8b5fbbb

Please sign in to comment.