Skip to content

Commit

Permalink
Add Slovene support 🇸🇮 (#134)
Browse files Browse the repository at this point in the history
* added slovene support

* update README.md

* updated long values

* updated long values

* removed unused imports

* updated test cases

* corrected tests

* renamed methods in SlovenePluralForms'
  • Loading branch information
jglaszka authored Nov 2, 2023
1 parent fd31e93 commit c368d45
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ With Tradukisto, you can now transform numbers into their word equivalents easy
* 🇷🇸 Serbian (Cyrillic)
* 🇷🇸 Serbian (Latin)
* 🇸🇰 Slovak
* 🇸🇮 Slovene / Slovenian
* 🇪🇸 Spanish
* 🇸🇪 Swedish
* 🇹🇷 Turkish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.slovakContainer;
import static pl.allegro.finance.tradukisto.internal.Container.sloveneContainer;
import static pl.allegro.finance.tradukisto.internal.Container.spanishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.turkishContainer;
Expand All @@ -46,6 +47,7 @@ public enum MoneyConverters {
TURKISH_BANKING_MONEY_VALUE(turkishContainer().getBankingMoneyConverter()),
SERBIAN_BANKING_MONEY_VALUE(serbianContainer().getBankingMoneyConverter()),
SERBIAN_CYRILLIC_BANKING_MONEY_VALUE(serbianCyrillicContainer().getBankingMoneyConverter()),
SLOVENE_BANKING_MONEY_VALUE(sloveneContainer().getBankingMoneyConverter()),
FRENCH_BANKING_MONEY_VALUE(frenchContainer().getBankingMoneyConverter()),
BULGARIAN_BANKING_MONEY_VALUE(bulgarianContainer().getBankingMoneyConverter()),
DUTCH_BANKING_MONEY_VALUE(dutchContainer().getBankingMoneyConverter()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum ValueConverters {
FRENCH_INTEGER(Container.frenchContainer().getIntegerConverter(), "fr"),
TURKISH_INTEGER(Container.turkishContainer().getIntegerConverter(), "tr"),
DUTCH_INTEGER(Container.dutchContainer().getIntegerConverter(), "nl"),
SLOVENE_INTEGER(Container.sloveneContainer().getIntegerConverter(), "sl"),
SPANISH_INTEGER(Container.spanishContainer().getIntegerConverter(), "es"),
HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi"),
SWEDISH_INTEGER(Container.swedishContainer().getIntegerConverter(), "sv");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import pl.allegro.finance.tradukisto.internal.languages.serbian.SerbianValues;
import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValues;
import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValuesForSmallNumbers;
import pl.allegro.finance.tradukisto.internal.languages.slovene.SloveneThousandToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.slovene.SloveneValues;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishIntegerToWordsConverterAdapter;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishThousandToWordsConverter;
Expand Down Expand Up @@ -93,6 +95,20 @@ public static Container croatianContainer() {
return new Container(new CroatianValues());
}

public static Container sloveneContainer() {
SloveneValues values = new SloveneValues();

SloveneThousandToWordsConverter sloveneThousandToWordsConverter = new SloveneThousandToWordsConverter(
values.baseNumbers());

IntegerToStringConverter converter = new NumberToWordsConverter(sloveneThousandToWordsConverter, values.pluralForms());

BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new BigDecimalToBankingMoneyConverter(
converter, values.currency());

return new Container(converter, null, bigDecimalBankingMoneyValueConverter);
}

public static Container czechContainer() {
CzechValues czechValues = new CzechValues();
Container containerForBigNumbers = new Container(czechValues);
Expand Down Expand Up @@ -250,7 +266,7 @@ public static Container hindiContainer() {

return new Container(integerToStringConverter, longValueConverters, bigDecimalConverter);
}

public static Container spanishContainer() {
SpanishValues values = new SpanishValues();

Expand All @@ -266,7 +282,7 @@ public static Container spanishContainer() {

return new Container(converter, null, bigDecimalBankingMoneyValueConverter);
}

public static Container swedishContainer() {
SwedishValues swedishBaseValues = new SwedishValues();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pl.allegro.finance.tradukisto.internal.languages;

import pl.allegro.finance.tradukisto.internal.support.Range;

public class SlovenePluralForms implements PluralForms {
private final String singularForm;
private final String dualForm;
private final String pluralForm;
private final String genitivePluralForm;

private final GenderType genderType;

public SlovenePluralForms(String singularForm, String dualForm, String pluralForm, String genitivePluralForm, GenderType genderType) {
this.singularForm = singularForm;
this.dualForm = dualForm;
this.pluralForm = pluralForm;
this.genitivePluralForm = genitivePluralForm;
this.genderType = genderType;
}

@Override
public String formFor(Integer value) {
if (shouldUseSingularForm(value)) {
return singularForm;
}
if (shouldUseDualForm(value)) {
return dualForm;
}
if (shouldUsePluralForm(value)) {
return pluralForm;
}
return genitivePluralForm;
}

private boolean shouldUseSingularForm(Integer value) {
return value == 1 || value % 100 == 1;
}

private boolean shouldUseDualForm(Integer value) {
return value == 2 || value % 100 == 2;
}

private boolean shouldUsePluralForm(Integer value) {
return Range.closed(3, 4).contains(value % 100);
}

@Override
public GenderType genderType() {
return genderType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pl.allegro.finance.tradukisto.internal.languages.slovene;

import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.languages.GenderForms;
import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.support.Range;

import java.util.Map;

import static java.lang.String.format;

public class SloveneThousandToWordsConverter implements GenderAwareIntegerToStringConverter {

private final Map<Integer, GenderForms> baseValues;

public SloveneThousandToWordsConverter(Map<Integer, GenderForms> baseValues) {
this.baseValues = baseValues;
}

@Override
public String asWords(Integer value, GenderType genderType) {
if (baseValues.containsKey(value)) {
return baseValues.get(value).formFor(genderType);
}
if (Range.closed(21, 99).contains(value)) {
return twoDigitsNumberAsString(value);
}
if (Range.closed(101, 999).contains(value)) {
return threeDigitsNumberAsString(value, genderType);
}
throw new IllegalArgumentException(format("Can't convert %d", value));
}

private String twoDigitsNumberAsString(Integer value) {
Integer units = value % 10;
Integer tens = value - units;
return format("%sin%s", asWords(units, GenderType.FEMININE), asWords(tens, GenderType.FEMININE));
}

private String threeDigitsNumberAsString(Integer value, GenderType genderType) {
Integer tensWithUnits = value % 100;
Integer hundreds = value - tensWithUnits;
return format("%s %s", asWords(hundreds, genderType), asWords(tensWithUnits, genderType));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pl.allegro.finance.tradukisto.internal.languages.slovene;

import pl.allegro.finance.tradukisto.internal.BaseValues;
import pl.allegro.finance.tradukisto.internal.languages.GenderForms;
import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;
import pl.allegro.finance.tradukisto.internal.languages.SlovenePluralForms;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms;
import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder;

public class SloveneValues implements BaseValues {
@Override
public Map<Integer, GenderForms> baseNumbers() {
return baseNumbersBuilder()
.put(0, "nič")
.put(1, genderForms("en", "ena", "ena", "ena"))
.put(2, genderForms("dva", "dve", "dve", "dva"))
.put(3, genderForms("trije", "tri", "tri", "tri"))
.put(4, genderForms("štirije", "štiri", "štiri", "štiri"))
.put(5, "pet")
.put(6, "šest")
.put(7, "sedem")
.put(8, "osem")
.put(9, "devet")
.put(10, "deset")
.put(11, "enajst")
.put(12, "dvanajst")
.put(13, "trinajst")
.put(14, "štirinajst")
.put(15, "petnajst")
.put(16, "šestnajst")
.put(17, "sedemnajst")
.put(18, "osemnajst")
.put(19, "devetnajst")
.put(20, "dvajset")
.put(30, "trideset")
.put(40, "štirideset")
.put(50, "petdeset")
.put(60, "šestdeset")
.put(70, "sedemdeset")
.put(80, "osemdeset")
.put(90, "devetdeset")
.put(100, "sto")
.put(200, "dvesto")
.put(300, "tristo")
.put(400, "štiristo")
.put(500, "petsto")
.put(600, "šeststo")
.put(700, "sedemsto")
.put(800, "osemsto")
.put(900, "devetsto")
.build();
}

@Override
public List<PluralForms> pluralForms() {
return Arrays.asList(
new SlovenePluralForms("", "", "", "", GenderType.FEMININE),
new SlovenePluralForms("tisoč", "tisoč", "tisoč", "tisoč", GenderType.NEUTER),
new SlovenePluralForms("milijon", "milijona", "milijone", "milijonov", GenderType.MASCULINE),
new SlovenePluralForms("milijarda", "milijardi", "milijarde", "milijard", GenderType.FEMININE));
}

@Override
public String currency() {
return "€";
}

@Override
public char twoDigitsNumberSeparator() {
return ' ';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import static pl.allegro.finance.tradukisto.MoneyConverters.SERBIAN_CYRILLIC_BAN
import static pl.allegro.finance.tradukisto.MoneyConverters.SLOVAK_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.SPANISH_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.SWEDISH_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.SLOVENE_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.TURKISH_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.UKRAINIAN_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.DUTCH_BANKING_MONEY_VALUE
Expand Down Expand Up @@ -49,6 +50,7 @@ class MoneyConvertersTest extends Specification {
"Serbian Cyrillic" | SERBIAN_CYRILLIC_BANKING_MONEY_VALUE || "једна хиљада двеста тридесет четири РСД 56/100"
"Serbian Latin" | SERBIAN_BANKING_MONEY_VALUE || "jedna hiljada dvesta trideset četiri RSD 56/100"
"Slovak" | SLOVAK_BANKING_MONEY_VALUE || "jeden tisíc dvesto tridsať štyri € 56/100"
"Slovene" | SLOVENE_BANKING_MONEY_VALUE || "ena tisoč dvesto štiriintrideset € 56/100"
"Spanish" | SPANISH_BANKING_MONEY_VALUE || "mil doscientos treinta y cuatro € 56/100"
"Russian" | RUSSIAN_BANKING_MONEY_VALUE || "одна тысяча двести тридцать четыре руб. 56/100"
"Swedish" | SWEDISH_BANKING_MONEY_VALUE || "ett tusen två hundra och trettiofyra kr 56/100"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import static pl.allegro.finance.tradukisto.ValueConverters.SERBIAN_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.SLOVAK_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.SPANISH_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.SWEDISH_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.SLOVENE_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.TURKISH_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.UKRAINIAN_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.DUTCH_INTEGER
Expand Down Expand Up @@ -50,6 +51,7 @@ class ValueConvertersTest extends Specification {
"Serbian Latin" | SERBIAN_INTEGER || "jedna hiljada dvesta trideset četiri"
"Slovak" | SLOVAK_INTEGER || "jeden tisíc dvesto tridsať štyri"
"Spanish" | SPANISH_INTEGER || "mil doscientos treinta y cuatro"
"Slovene" | SLOVENE_INTEGER || "ena tisoč dvesto štiriintrideset"
"Russian" | RUSSIAN_INTEGER || "одна тысяча двести тридцать четыре"
"Swedish" | SWEDISH_INTEGER || "ett tusen två hundra och trettiofyra"
"Turkish" | TURKISH_INTEGER || "Bin İki Yüz Otuz Dört"
Expand Down Expand Up @@ -94,6 +96,7 @@ class ValueConvertersTest extends Specification {
new Locale("sr") || SERBIAN_INTEGER
new Locale.Builder().setLanguage("sr").setScript("Latn").build() || SERBIAN_INTEGER
new Locale("sk") || SLOVAK_INTEGER
new Locale("sl") || SLOVENE_INTEGER
new Locale("es") || SPANISH_INTEGER
new Locale("tr") || TURKISH_INTEGER
new Locale("uk") || UKRAINIAN_INTEGER
Expand Down Expand Up @@ -143,6 +146,7 @@ class ValueConvertersTest extends Specification {
"sr" || SERBIAN_INTEGER
"sr__#Latn" || SERBIAN_INTEGER
"sk" || SLOVAK_INTEGER
"sl" || SLOVENE_INTEGER
"es" || SPANISH_INTEGER
"tr" || TURKISH_INTEGER
"uk" || UKRAINIAN_INTEGER
Expand Down
Loading

0 comments on commit c368d45

Please sign in to comment.