From ea58f86eed031ccb336d9519ef6095fe7a52f405 Mon Sep 17 00:00:00 2001 From: altro3 Date: Mon, 18 Oct 2021 14:30:56 +0700 Subject: [PATCH] 46 Added getValueConverters(Locale locale) and getByLanguageCode(String languageCode) methods (#47) Co-authored-by: altro3 --- .../finance/tradukisto/ValueConverters.java | 78 +++++++-- .../tradukisto/ValueConvertersTest.groovy | 151 +++++++++++++++++- 2 files changed, 204 insertions(+), 25 deletions(-) diff --git a/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java b/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java index c6f20d32..6c3f9dec 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java @@ -1,7 +1,14 @@ package pl.allegro.finance.tradukisto; +import com.google.common.base.Strings; import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +import static com.google.common.base.Verify.verify; import static com.google.common.base.Verify.verifyNotNull; import static pl.allegro.finance.tradukisto.internal.Container.brazilianPortugueseContainer; import static pl.allegro.finance.tradukisto.internal.Container.czechContainer; @@ -13,34 +20,40 @@ import static pl.allegro.finance.tradukisto.internal.Container.latvianContainer; import static pl.allegro.finance.tradukisto.internal.Container.polishContainer; import static pl.allegro.finance.tradukisto.internal.Container.russianContainer; -import static pl.allegro.finance.tradukisto.internal.Container.serbianCyrillicContainer; import static pl.allegro.finance.tradukisto.internal.Container.serbianContainer; +import static pl.allegro.finance.tradukisto.internal.Container.serbianCyrillicContainer; import static pl.allegro.finance.tradukisto.internal.Container.slovakContainer; import static pl.allegro.finance.tradukisto.internal.Container.turkishContainer; import static pl.allegro.finance.tradukisto.internal.Container.ukrainianContainer; public enum ValueConverters { - BRAZILIAN_PORTUGUESE_INTEGER(brazilianPortugueseContainer().getNumbersConverter()), - ENGLISH_INTEGER(englishContainer().getNumbersConverter()), - GERMAN_INTEGER(germanContainer().getNumbersConverter()), - RUSSIAN_INTEGER(russianContainer().getNumbersConverter()), - ITALIAN_INTEGER(italianContainer().getNumbersConverter()), - POLISH_INTEGER(polishContainer().getNumbersConverter()), - CZECH_INTEGER(czechContainer().getNumbersConverter()), - SLOVAK_INTEGER(slovakContainer().getNumbersConverter()), - LATVIAN_INTEGER(latvianContainer().getNumbersConverter()), - KAZAKH_INTEGER(kazakhContainer().getNumbersConverter()), - UKRAINIAN_INTEGER(ukrainianContainer().getNumbersConverter()), - SERBIAN_INTEGER(serbianContainer().getNumbersConverter()), - SERBIAN_CYRILLIC_INTEGER(serbianCyrillicContainer().getNumbersConverter()), - FRENCH_INTEGER(frenchContainer().getNumbersConverter()), - TURKISH_INTEGER(turkishContainer().getNumbersConverter()); + BRAZILIAN_PORTUGUESE_INTEGER(brazilianPortugueseContainer().getNumbersConverter(), Arrays.asList("pt", "pt-br")), + ENGLISH_INTEGER(englishContainer().getNumbersConverter(), "en"), + GERMAN_INTEGER(germanContainer().getNumbersConverter(), "de"), + RUSSIAN_INTEGER(russianContainer().getNumbersConverter(), "ru"), + ITALIAN_INTEGER(italianContainer().getNumbersConverter(), "it"), + POLISH_INTEGER(polishContainer().getNumbersConverter(), "pl"), + CZECH_INTEGER(czechContainer().getNumbersConverter(), "cs"), + SLOVAK_INTEGER(slovakContainer().getNumbersConverter(), "sk"), + LATVIAN_INTEGER(latvianContainer().getNumbersConverter(), "lv"), + KAZAKH_INTEGER(kazakhContainer().getNumbersConverter(), "kk"), + UKRAINIAN_INTEGER(ukrainianContainer().getNumbersConverter(), "uk"), + SERBIAN_INTEGER(serbianContainer().getNumbersConverter(), Arrays.asList("sr", getLanguageCodeFor("sr", "Latn"))), + SERBIAN_CYRILLIC_INTEGER(serbianCyrillicContainer().getNumbersConverter(), getLanguageCodeFor("sr", "Cyrl")), + FRENCH_INTEGER(frenchContainer().getNumbersConverter(), "fr"), + TURKISH_INTEGER(turkishContainer().getNumbersConverter(), "tr"); private final IntegerToStringConverter converter; + private final List languageCodes; + + ValueConverters(IntegerToStringConverter converter, String languageCodes) { + this(converter, Collections.singletonList(languageCodes)); + } - ValueConverters(IntegerToStringConverter converter) { + ValueConverters(IntegerToStringConverter converter, List languageCodes) { this.converter = converter; + this.languageCodes = languageCodes; } public String asWords(Integer value) { @@ -48,4 +61,35 @@ public String asWords(Integer value) { return converter.asWords(value); } + + public static ValueConverters getByLocaleOrDefault(Locale locale, ValueConverters defaultConverter) { + verifyNotNull(locale); + + String languageCode; + if (hasSpecifiedScript(locale)) { + languageCode = getLanguageCodeFor(locale.getLanguage(), locale.getScript()); + } else { + languageCode = locale.getLanguage(); + } + + return getByLanguageCodeOrDefault(languageCode, defaultConverter); + } + + private static boolean hasSpecifiedScript(Locale locale) { + return !Strings.isNullOrEmpty(locale.getScript()); + } + + private static String getLanguageCodeFor(String language, String script) { + return new Locale.Builder().setLanguage(language).setScript(script).build().toString(); + } + + public static ValueConverters getByLanguageCodeOrDefault(String languageCode, ValueConverters defaultConverter) { + verifyNotNull(languageCode); + verify(!languageCode.isEmpty()); + + return Arrays.stream(values()) + .filter(it -> it.languageCodes.contains(languageCode)) + .findFirst() + .orElse(defaultConverter); + } } diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy index 70588dcf..65eca4aa 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy @@ -12,14 +12,14 @@ class ValueConvertersTest extends Specification { BRAZILIAN_PORTUGUESE_INTEGER.asWords(1_234) == "mil duzentos e trinta e quatro" } - def "should convert numbers in German"() { + def "should convert numbers in English"() { expect: - GERMAN_INTEGER.asWords(1_234) == "eintausendzweihundertvierunddreißig" + ENGLISH_INTEGER.asWords(1_234) == "one thousand two hundred thirty-four" } - def "should convert numbers in Italian"() { + def "should convert numbers in German"() { expect: - ITALIAN_INTEGER.asWords(1_234) == "milleduecentotrentaquattro" + GERMAN_INTEGER.asWords(1_234) == "eintausendzweihundertvierunddreißig" } def "should convert numbers in Russian"() { @@ -27,6 +27,11 @@ class ValueConvertersTest extends Specification { RUSSIAN_INTEGER.asWords(1_234) == "одна тысяча двести тридцать четыре" } + def "should convert numbers in Italian"() { + expect: + ITALIAN_INTEGER.asWords(1_234) == "milleduecentotrentaquattro" + } + def "should convert numbers in Polish"() { expect: POLISH_INTEGER.asWords(1_234) == "jeden tysiąc dwieście trzydzieści cztery" @@ -37,9 +42,9 @@ class ValueConvertersTest extends Specification { CZECH_INTEGER.asWords(1_234) == "jeden tisíc dvě stě třicet čtyři" } - def "should convert numbers in English"() { + def "should convert numbers in Slovak"() { expect: - ENGLISH_INTEGER.asWords(1_234) == "one thousand two hundred thirty-four" + SLOVAK_INTEGER.asWords(1_234) == "jeden tisíc dvesto tridsať štyri" } def "should convert numbers in Latvian"() { @@ -52,9 +57,19 @@ class ValueConvertersTest extends Specification { KAZAKH_INTEGER.asWords(1_234) == "бір мың екі жүз отыз төрт" } - def "should convert numbers in Turkish"() { + def "should convert numbers in Ukrainian"() { expect: - TURKISH_INTEGER.asWords(1_234) == "Bin İki Yüz Otuz Dört" + UKRAINIAN_INTEGER.asWords(1_234) == "одна тисяча двісті тридцять чотири" + } + + def "should convert numbers in Serbian"() { + expect: + SERBIAN_INTEGER.asWords(1_234) == "jedna hiljada dvesta trideset četiri" + } + + def "should convert numbers in Serbian Cyrillic"() { + expect: + SERBIAN_CYRILLIC_INTEGER.asWords(1_234) == "једна хиљада двеста тридесет четири" } def "should convert numbers in French"() { @@ -62,6 +77,11 @@ class ValueConvertersTest extends Specification { FRENCH_INTEGER.asWords(1_234) == "mille deux cent trente-quatre" } + def "should convert numbers in Turkish"() { + expect: + TURKISH_INTEGER.asWords(1_234) == "Bin İki Yüz Otuz Dört" + } + def "should throw exception when null given"() { when: POLISH_INTEGER.asWords(null) @@ -69,4 +89,119 @@ class ValueConvertersTest extends Specification { then: thrown(VerifyException) } + + def "getByLocaleOrDefault for Brazilian Portuguese"() { + expect: + getByLocaleOrDefault(new Locale("pt-br"), null).asWords(1_234) == "mil duzentos e trinta e quatro" + } + + def "getByLocaleOrDefault for Portuguese"() { + expect: + getByLocaleOrDefault(new Locale("pt"), null).asWords(1_234) == "mil duzentos e trinta e quatro" + } + + def "getByLocaleOrDefault for English"() { + expect: + getByLocaleOrDefault(Locale.ENGLISH, null).asWords(1_234) == "one thousand two hundred thirty-four" + } + + def "getByLocaleOrDefault for German"() { + expect: + getByLocaleOrDefault(new Locale("de"), null).asWords(1_234) == "eintausendzweihundertvierunddreißig" + } + + def "getByLocaleOrDefault for Russian"() { + expect: + getByLocaleOrDefault(new Locale("ru"), null).asWords(1_234) == "одна тысяча двести тридцать четыре" + } + + def "getByLocaleOrDefault for Italian"() { + expect: + getByLocaleOrDefault(new Locale("it"), null).asWords(1_234) == "milleduecentotrentaquattro" + } + + def "getByLocaleOrDefault for Polish"() { + expect: + getByLocaleOrDefault(new Locale("pl"), null).asWords(1_234) == "jeden tysiąc dwieście trzydzieści cztery" + } + + def "getByLocaleOrDefault for Czech"() { + expect: + getByLocaleOrDefault(new Locale("cs"), null).asWords(1_234) == "jeden tisíc dvě stě třicet čtyři" + } + + def "getByLocaleOrDefault for Slovak"() { + expect: + getByLocaleOrDefault(new Locale("sk"), null).asWords(1_234) == "jeden tisíc dvesto tridsať štyri" + } + + def "getByLocaleOrDefault for Latvian"() { + expect: + getByLocaleOrDefault(new Locale("lv"), null).asWords(1_234) == "viens tūkstotis divi simti trīsdesmit četri" + } + + def "getByLocaleOrDefault for Kazakh"() { + expect: + getByLocaleOrDefault(new Locale("kk"), null).asWords(1_234) == "бір мың екі жүз отыз төрт" + } + + def "getByLocaleOrDefault for Ukrainian"() { + expect: + getByLocaleOrDefault(new Locale("uk"), null).asWords(1_234) == "одна тисяча двісті тридцять чотири" + } + + def "getByLocaleOrDefault for Serbian"() { + expect: + getByLocaleOrDefault(new Locale("sr"), null).asWords(1_234) == "jedna hiljada dvesta trideset četiri" + } + + def "getByLocaleOrDefault for Serbian Latin"() { + expect: + getByLocaleOrDefault(new Locale.Builder().setLanguage("sr").setScript("Latn").build(), null).asWords(1_234) == "jedna hiljada dvesta trideset četiri" + } + + def "getByLocaleOrDefault for Serbian Cyrillic"() { + expect: + getByLocaleOrDefault(new Locale.Builder().setLanguage("sr").setScript("Cyrl").build(), null).asWords(1_234) == "једна хиљада двеста тридесет четири" + } + + def "getByLocaleOrDefault for French"() { + expect: + getByLocaleOrDefault(Locale.FRENCH, null).asWords(1_234) == "mille deux cent trente-quatre" + } + + def "getByLocaleOrDefault for Turkish"() { + expect: + getByLocaleOrDefault(new Locale("tr"), null).asWords(1_234) == "Bin İki Yüz Otuz Dört" + } + + def "getByLocaleOrDefault: should throw exception when null given"() { + when: + getByLocaleOrDefault(null, null).asWords(null) + + then: + thrown(VerifyException) + } + + def "getByLanguageCodeOrDefault: should throw exception when null language code given"() { + when: + getByLanguageCodeOrDefault(null, null).asWords(null) + + then: + thrown(VerifyException) + } + + def "getByLocaleOrDefault: should throw exception when empty language code given"() { + when: + getByLocaleOrDefault(new Locale(""), null).asWords(null) + + then: + thrown(VerifyException) + } + + def "getByLocaleOrDefault: test for default converter"() { + expect: + getByLocaleOrDefault(new Locale("zh"), FRENCH_INTEGER) == FRENCH_INTEGER + } + }