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

Add Slovene support 🇸🇮 #134

Merged
merged 10 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ With Tradukisto, you can now transform numbers into their word equivalents easy
* 🇵🇱 Polish
* 🇷🇺 Russian
* 🇸🇰 Slovak
* 🇸🇮 Slovene / Slovenian
* 🇺🇦 Ukrainian
* 🇷🇸 Serbian (Latin)
* 🇷🇸 Serbian (Cyrillic)
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.turkishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.ukrainianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.bulgarianContainer;
Expand All @@ -43,6 +44,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,7 +30,8 @@ public enum ValueConverters {
FRENCH_INTEGER(Container.frenchContainer().getIntegerConverter(), "fr"),
TURKISH_INTEGER(Container.turkishContainer().getIntegerConverter(), "tr"),
DUTCH_INTEGER(Container.dutchContainer().getIntegerConverter(), "nl"),
HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi");
HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi"),
SLOVENE_INTEGER(Container.sloveneContainer().getIntegerConverter(), "sl");

private final IntegerToStringConverter converter;
private final List<String> languageCodes;
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.turkish.TurkishBigDecimalToBankingMoneyConverter;
import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishSmallNumbersToWordsConverter;
Expand Down Expand Up @@ -87,6 +89,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
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 (useSingular(value)) {
return singularForm;
}
if (useDual(value)) {
return dualForm;
}
if (usePluralForm(value)) {
return pluralForm;
}
return genitivePluralForm;
}

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

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

private boolean usePluralForm(Integer value) {
return Range.closed(3, 4).contains(value % 100);
}
jglaszka marked this conversation as resolved.
Show resolved Hide resolved

@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, genderType);
}
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, GenderType genderType) {
Integer units = value % 10;
Integer tens = value - units;
return format("%sin%s", asWords(units, genderType), asWords(tens, genderType));
}

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, "ena")
.put(2, genderForms("dva", "dve", "dva", "dva"))
.put(3, "tri")
.put(4, "š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.NEUTER),
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 @@ -19,6 +19,7 @@ import static pl.allegro.finance.tradukisto.MoneyConverters.RUSSIAN_BANKING_MONE
import static pl.allegro.finance.tradukisto.MoneyConverters.SERBIAN_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.SERBIAN_CYRILLIC_BANKING_MONEY_VALUE
import static pl.allegro.finance.tradukisto.MoneyConverters.SLOVAK_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 @@ -47,6 +48,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"
"Russian" | RUSSIAN_BANKING_MONEY_VALUE || "одна тысяча двести тридцать четыре руб. 56/100"
"Turkish" | TURKISH_BANKING_MONEY_VALUE || "BinİkiYüzOtuzDörtTL,ElliAltıKr."
"Ukrainian" | UKRAINIAN_BANKING_MONEY_VALUE || "одна тисяча двісті тридцять чотири ₴ 56/100"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import static pl.allegro.finance.tradukisto.ValueConverters.RUSSIAN_INTEGER
import static pl.allegro.finance.tradukisto.ValueConverters.SERBIAN_CYRILLIC_INTEGER
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.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 @@ -47,6 +48,7 @@ class ValueConvertersTest extends Specification {
"Serbian Cyrillic" | SERBIAN_CYRILLIC_INTEGER || "једна хиљада двеста тридесет четири"
"Serbian Latin" | SERBIAN_INTEGER || "jedna hiljada dvesta trideset četiri"
"Slovak" | SLOVAK_INTEGER || "jeden tisíc dvesto tridsať štyri"
"Slovene" | SLOVENE_INTEGER || "ena tisoč dvesto štiriintrideset"
"Russian" | RUSSIAN_INTEGER || "одна тысяча двести тридцать четыре"
"Turkish" | TURKISH_INTEGER || "Bin İki Yüz Otuz Dört"
"Ukrainian" | UKRAINIAN_INTEGER || "одна тисяча двісті тридцять чотири"
Expand Down Expand Up @@ -90,6 +92,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("tr") || TURKISH_INTEGER
new Locale("uk") || UKRAINIAN_INTEGER
new Locale("nl") || DUTCH_INTEGER
Expand Down Expand Up @@ -137,6 +140,7 @@ class ValueConvertersTest extends Specification {
"sr" || SERBIAN_INTEGER
"sr__#Latn" || SERBIAN_INTEGER
"sk" || SLOVAK_INTEGER
"sl" || SLOVENE_INTEGER
"tr" || TURKISH_INTEGER
"uk" || UKRAINIAN_INTEGER
"hi" || HINDI_INTEGER
Expand Down
Loading