-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from dilaverdemirel/master
Add Turkish Language support
- Loading branch information
Showing
15 changed files
with
551 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ Supported languages | |
* Slovak | ||
* Ukrainian | ||
* Serbian (latin) | ||
* Turkish | ||
|
||
Usage | ||
----- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...nance/tradukisto/internal/languages/turkish/TurkishBigDecimalToBankingMoneyConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.turkish; | ||
|
||
import pl.allegro.finance.tradukisto.internal.BigDecimalToStringConverter; | ||
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static java.lang.String.format; | ||
|
||
/** | ||
* @author Dilaver Demirel | ||
*/ | ||
public class TurkishBigDecimalToBankingMoneyConverter implements BigDecimalToStringConverter { | ||
private static final String SUBUNIT_SEPARATOR = ","; | ||
private static final String FORMAT = "%s%s%s%s"; | ||
private static final int MAXIMAL_DECIMAL_PLACES_COUNT = 2; | ||
|
||
private final IntegerToStringConverter converter; | ||
private final TurkishValues turkishValues; | ||
|
||
public TurkishBigDecimalToBankingMoneyConverter(IntegerToStringConverter converter, TurkishValues turkishValues) { | ||
this.converter = converter; | ||
this.turkishValues = turkishValues; | ||
} | ||
|
||
@Override | ||
public String asWords(BigDecimal value) { | ||
validate(value); | ||
|
||
Integer units = value.intValue(); | ||
int subunits = value.remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).intValue(); | ||
|
||
String tempSubunitSymbol = turkishValues.subunitSymbol(); | ||
String tempSubUnitWords = SUBUNIT_SEPARATOR + converter.asWords(subunits); | ||
if (subunits <= 0) { | ||
tempSubunitSymbol = ""; | ||
tempSubUnitWords = ""; | ||
} | ||
|
||
String formattedValue = format(FORMAT, converter.asWords(units), turkishValues.currency(), tempSubUnitWords, tempSubunitSymbol); | ||
String convertedToMoneyTypeValue = formattedValue.replace(Character.toString(turkishValues.twoDigitsNumberSeparator()), ""); | ||
return convertedToMoneyTypeValue; | ||
} | ||
|
||
private void validate(BigDecimal value) { | ||
checkArgument(value.scale() <= MAXIMAL_DECIMAL_PLACES_COUNT, | ||
"can't transform more than %s decimal places for value %s", MAXIMAL_DECIMAL_PLACES_COUNT, value); | ||
|
||
checkArgument(valueLessThanIntMax(value), | ||
"can't transform numbers greater than Integer.MAX_VALUE for value %s", value); | ||
|
||
checkArgument(valueGreaterThanOrEqualToZero(value), | ||
"can't transform negative numbers for value %s", value); | ||
} | ||
|
||
private boolean valueLessThanIntMax(BigDecimal value) { | ||
return value.compareTo(new BigDecimal(Integer.MAX_VALUE).add(BigDecimal.ONE)) == -1; | ||
} | ||
|
||
private boolean valueGreaterThanOrEqualToZero(BigDecimal value) { | ||
return value.signum() >= 0; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...allegro/finance/tradukisto/internal/languages/turkish/TurkishIntegerToWordsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.turkish; | ||
|
||
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; | ||
import pl.allegro.finance.tradukisto.internal.NumberProcessor; | ||
|
||
/** | ||
* @author Dilaver Demirel | ||
*/ | ||
public class TurkishIntegerToWordsConverter implements IntegerToStringConverter { | ||
|
||
private final IntegerToStringConverter bigNumbersConverter; | ||
private final TurkishSmallNumbersToWordsConverter smallNumbersConverter; | ||
|
||
public TurkishIntegerToWordsConverter(IntegerToStringConverter bigNumbersConverter, | ||
TurkishSmallNumbersToWordsConverter smallNumbersConverter) { | ||
this.bigNumbersConverter = bigNumbersConverter; | ||
this.smallNumbersConverter = smallNumbersConverter; | ||
} | ||
|
||
@Override | ||
public String asWords(Integer value) { | ||
Integer bigNumber = value / 1000000; | ||
Integer smallNumber = value % 1000000; | ||
|
||
return new NumberProcessor(bigNumbersConverter, smallNumbersConverter).process(bigNumber, smallNumber); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...ain/java/pl/allegro/finance/tradukisto/internal/languages/turkish/TurkishPluralForms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.turkish; | ||
|
||
import pl.allegro.finance.tradukisto.internal.languages.GenderType; | ||
import pl.allegro.finance.tradukisto.internal.languages.PluralForms; | ||
|
||
/** | ||
* @author Dilaver Demirel | ||
*/ | ||
public class TurkishPluralForms implements PluralForms { | ||
|
||
private final String form; | ||
|
||
public TurkishPluralForms(String form) { | ||
this.form = form; | ||
} | ||
|
||
@Override | ||
public String formFor(Integer value) { | ||
return form; | ||
} | ||
|
||
@Override | ||
public GenderType genderType() { | ||
return GenderType.NON_APPLICABLE; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...ro/finance/tradukisto/internal/languages/turkish/TurkishSmallNumbersToWordsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.turkish; | ||
|
||
import com.google.common.collect.Range; | ||
import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; | ||
import pl.allegro.finance.tradukisto.internal.languages.GenderType; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* @author Dilaver Demirel | ||
* <p> | ||
* Small numbers are between 0 and 999.999 | ||
* Big numbers are greater than 999.999 | ||
*/ | ||
public class TurkishSmallNumbersToWordsConverter implements GenderAwareIntegerToStringConverter { | ||
|
||
private final TurkishValues turkishValues; | ||
private final char separator; | ||
|
||
public TurkishSmallNumbersToWordsConverter(TurkishValues turkishValues) { | ||
this.turkishValues = turkishValues; | ||
separator = turkishValues.twoDigitsNumberSeparator(); | ||
} | ||
|
||
@Override | ||
public String asWords(Integer value, GenderType genderType) { | ||
if (turkishValues.baseNumbers().containsKey(value)) { | ||
return turkishValues.baseNumbers().get(value).formFor(genderType); | ||
} else if (Range.closed(21, 99).contains(value)) { | ||
return twoDigitsNumberAsString(value, genderType); | ||
} else if (Range.closed(101, 999).contains(value)) { | ||
return threeDigitsNumberAsString(value, genderType); | ||
} else if (value == 1000) { | ||
return "Bin"; | ||
} else if (Range.closed(1001, 1999).contains(value)) { | ||
return oneThousandsAsString(value, genderType); | ||
} else if (Range.closed(2000, 999999).contains(value)) { | ||
return greaterThanOneThousandsAsString(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("%s" + separator + "%s", asWords(tens, genderType), asWords(units, genderType)); | ||
} | ||
|
||
private String threeDigitsNumberAsString(Integer value, GenderType genderType) { | ||
Integer tensWithUnits = value % 100; | ||
Integer hundreds = value - tensWithUnits; | ||
return format("%s" + separator + "%s", asWords(hundreds, genderType), asWords(tensWithUnits, genderType)); | ||
} | ||
|
||
private String greaterThanOneThousandsAsString(Integer value, GenderType genderType) { | ||
Integer thousands = value / 1000; | ||
Integer other = value % 1000; | ||
|
||
if (other == 0) { | ||
return format("%s" + separator + "Bin", asWords(thousands, genderType)); | ||
} | ||
return format("%s" + separator + "Bin" + separator + "%s", asWords(thousands, genderType), | ||
asWords(other, genderType)); | ||
} | ||
|
||
private String oneThousandsAsString(Integer value, GenderType genderType) { | ||
Integer other = value % 1000; | ||
return format("Bin" + separator + "%s", asWords(other, genderType)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/pl/allegro/finance/tradukisto/internal/languages/turkish/TurkishValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.turkish; | ||
|
||
import pl.allegro.finance.tradukisto.internal.BaseValues; | ||
import pl.allegro.finance.tradukisto.internal.languages.GenderForms; | ||
import pl.allegro.finance.tradukisto.internal.languages.PluralForms; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder; | ||
|
||
public class TurkishValues implements BaseValues { | ||
|
||
public Map<Integer, GenderForms> baseNumbers() { | ||
return baseNumbersBuilder() | ||
.put(0, "Sıfır") | ||
.put(1, "Bir") | ||
.put(2, "İki") | ||
.put(3, "Üç") | ||
.put(4, "Dört") | ||
.put(5, "Beş") | ||
.put(6, "Altı") | ||
.put(7, "Yedi") | ||
.put(8, "Sekiz") | ||
.put(9, "Dokuz") | ||
.put(10, "On") | ||
.put(11, "On Bir") | ||
.put(12, "On İki") | ||
.put(13, "On Üç") | ||
.put(14, "On Dört") | ||
.put(15, "On Beş") | ||
.put(16, "On Altı") | ||
.put(17, "On Yedi") | ||
.put(18, "On Sekiz") | ||
.put(19, "On Dokuz") | ||
.put(20, "Yirmi") | ||
.put(30, "Otuz") | ||
.put(40, "Kırk") | ||
.put(50, "Elli") | ||
.put(60, "Altmış") | ||
.put(70, "Yetmiş") | ||
.put(80, "Seksen") | ||
.put(90, "Doksan") | ||
.put(100, "Yüz") | ||
.put(200, "İki Yüz") | ||
.put(300, "Üç Yüz") | ||
.put(400, "Dört Yüz") | ||
.put(500, "Beş Yüz") | ||
.put(600, "Altı Yüz") | ||
.put(700, "Yedi Yüz") | ||
.put(800, "Sekiz Yüz") | ||
.put(900, "Dokuz Yüz") | ||
.build(); | ||
} | ||
|
||
public List<PluralForms> pluralForms() { | ||
return Arrays.asList( | ||
new TurkishPluralForms("Milyon"), | ||
new TurkishPluralForms("Milyar")); | ||
} | ||
|
||
public String currency() { | ||
return "TL"; | ||
} | ||
|
||
@Override | ||
public char twoDigitsNumberSeparator() { | ||
return ' '; | ||
} | ||
|
||
public String subunitSymbol() { | ||
return "Kr."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.