From f1fdd51342ca0572248170b3c08a970b0c446950 Mon Sep 17 00:00:00 2001 From: dotasek Date: Wed, 22 May 2024 18:21:07 -0400 Subject: [PATCH] Add utility to infer locale from region code --- .../utilities/i18n/RegionToLocaleMapper.java | 67 +++++++++++ .../i18n/RegionToLocaleMapperTest.java | 107 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapper.java create mode 100644 org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapperTest.java diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapper.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapper.java new file mode 100644 index 0000000000..f6e4e5c8ae --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapper.java @@ -0,0 +1,67 @@ +package org.hl7.fhir.utilities.i18n; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * Infers an IETF BCP 47 locale from a ISO3166 country code. + */ +public class RegionToLocaleMapper { + + private RegionToLocaleMapper() { + throw new IllegalStateException("Utility class"); + } + + static final Map 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); + } + +} diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapperTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapperTest.java new file mode 100644 index 0000000000..fd083c7c46 --- /dev/null +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/RegionToLocaleMapperTest.java @@ -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")); + } +}