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

Persian Language support #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static pl.allegro.finance.tradukisto.internal.Container.italianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.kazakhContainer;
import static pl.allegro.finance.tradukisto.internal.Container.latvianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.persianContainer;
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;
Expand All @@ -39,6 +40,7 @@ public enum MoneyConverters {
ITALIAN_BANKING_MONEY_VALUE(italianContainer().getBankingMoneyConverter()),
CROATIAN_BANKING_MONEY_VALUE(croatianContainer().getBankingMoneyConverter()),
CZECH_BANKING_MONEY_VALUE(czechContainer().getBankingMoneyConverter()),
PERSIAN_BANKING_MONEY_VALUE(persianContainer().getBankingMoneyConverter()),
SLOVAK_BANKING_MONEY_VALUE(slovakContainer().getBankingMoneyConverter()),
LATVIAN_BANKING_MONEY_VALUE(latvianContainer().getBankingMoneyConverter()),
KAZAKH_BANKING_MONEY_VALUE(kazakhContainer().getBankingMoneyConverter()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pl.allegro.finance.tradukisto.internal.languages.italian.ItalianValues;
import pl.allegro.finance.tradukisto.internal.languages.kazakh.KazakhValues;
import pl.allegro.finance.tradukisto.internal.languages.latvian.LatvianValues;
import pl.allegro.finance.tradukisto.internal.languages.persian.PersianValues;
import pl.allegro.finance.tradukisto.internal.languages.polish.PolishValues;
import pl.allegro.finance.tradukisto.internal.languages.portuguese.BrazilianPortugueseValues;
import pl.allegro.finance.tradukisto.internal.languages.portuguese.PortugueseIntegerToWordsConverter;
Expand Down Expand Up @@ -216,6 +217,22 @@ public static Container brazilianPortugueseContainer() {

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

public static Container persianContainer()
{
PersianValues persianValues = new PersianValues();
HundredsToWordsConverter hundredsToStringConverter = new HundredsToWordsConverter(persianValues.baseNumbers(),
persianValues.twoDigitsNumberSeparator());

pl.allegro.finance.tradukisto.internal.languages.persian.IntegerToWordsConverter integerConverter =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use import and not the full qualified class name

new pl.allegro.finance.tradukisto.internal.languages.persian.IntegerToWordsConverter(
hundredsToStringConverter, persianValues.pluralForms());

return new Container(integerConverter,
new pl.allegro.finance.tradukisto.internal.languages.persian.BigDecimalToBankingMoneyConverter(integerConverter,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use import and not the full qualified class name

persianValues.currency()));
}


public static Container turkishContainer() {
TurkishValues values = new TurkishValues();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pl.allegro.finance.tradukisto.internal.languages.persian;

import java.math.BigDecimal;

import pl.allegro.finance.tradukisto.internal.BigDecimalToStringConverter;
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;

public class BigDecimalToBankingMoneyConverter implements BigDecimalToStringConverter {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename class to PersianBigDecimalToBankingMoneyConverter


private static final String FORMAT = "%s %s \u0648 %02d \u0635\u062F\u0645";
private static final String FORMAT_INT = "%s %s";
private static final int MAXIMAL_DECIMAL_PLACES_COUNT = 2;

private final IntegerToStringConverter converter;
private final String currencySymbol;

public BigDecimalToBankingMoneyConverter(IntegerToStringConverter converter, String currencySymbol) {
this.converter = converter;
this.currencySymbol = currencySymbol;
}

@Override
public String asWords(BigDecimal value) {
validate(value);

Integer units = value.intValue();
Integer subunits = value.remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).intValue();
if(subunits > 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add space after if, add braces to if and else blocks

return format(FORMAT, converter.asWords(units), currencySymbol, subunits);
else
return format(FORMAT_INT, converter.asWords(units), currencySymbol, subunits);
}

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(valueLessThatIntMax(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 valueLessThatIntMax(BigDecimal value) {
return value.compareTo(new BigDecimal(Integer.MAX_VALUE).add(BigDecimal.ONE)) == -1;
}

private boolean valueGreaterThanOrEqualToZero(BigDecimal value) {
return value.signum() >= 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pl.allegro.finance.tradukisto.internal.languages.persian;

import java.util.Iterator;
import java.util.List;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;

/**
* @author omidp
*
*/
public class IntegerToWordsConverter extends pl.allegro.finance.tradukisto.internal.converters.IntegerToWordsConverter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename class to PersianIntegerToWordsConverter and add import for pl.allegro.finance.tradukisto.internal.converters.IntegerToWordsConverter

{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move opening braces (not just this one) to the line above


private final List<PluralForms> pluralForms;

public IntegerToWordsConverter(GenderAwareIntegerToStringConverter hundredsToWordsConverter, List<PluralForms> pluralForms)
{
super(hundredsToWordsConverter, pluralForms);
this.pluralForms = pluralForms;
}

public IntegerToWordsConverter(IntegerToStringConverter hundredsToWordsConverter, List<PluralForms> pluralForms)
{
super(hundredsToWordsConverter, pluralForms);
this.pluralForms = pluralForms;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's an unnecessary blank line


@Override
protected String joinParts(List<String> result)
{
if (result.size() == 0) {
return hundredsToWordsConverter.asWords(0, pluralForms.get(0).genderType());
}
StringBuilder sb = new StringBuilder();
int counter = 0;
for (String item : result)
{
if(item != null && item.trim().length() > 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after if

{
if(counter > 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after if

{
Iterable<String> split = Splitter.on(" ").split(item);
for (Iterator iterator = split.iterator(); iterator.hasNext();)
{
sb.append(" \u0648 ");
String string = (String) iterator.next();
sb.append(string);
}
}
else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add braces

sb.append(item).append(" ");
}
counter++;
}
// return Joiner.on(" ").join(result).trim();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pl.allegro.finance.tradukisto.internal.languages.persian;

import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;

/**
* @author omidp
*
*/
public class PersianPluralForms implements PluralForms
{

private final String form;

public PersianPluralForms(String form) {
this.form = form;
}

@Override
public String formFor(Integer value) {
return form;
}

@Override
public GenderType genderType() {
return GenderType.NON_APPLICABLE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pl.allegro.finance.tradukisto.internal.languages.persian;

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

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

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

/**
* @author omidp
*
*/
public class PersianValues implements BaseValues
{

@Override
public Map<Integer, GenderForms> baseNumbers()
{
return baseNumbersBuilder().put(0, "\u0635\u0641\u0631").put(1, "\u06CC\u06A9").put(2, "\u062F\u0648").put(3, "\u0633\u0647")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reformat so that each put is on a separate line

.put(4, "\u0686\u0647\u0627\u0631").put(5, "\u067E\u0646\u062C").put(6, "\u0634\u0634").put(7, "\u0647\u0641\u062A")
.put(8, "\u0647\u0634\u062A").put(9, "\u0646\u0647").put(10, "\u062F\u0647").put(11, "\u06CC\u0627\u0632\u062F\u0647")
.put(12, "\u062F\u0648\u0627\u0632\u062F\u0647").put(13, "\u0633\u06CC\u0632\u062F\u0647")
.put(14, "\u0686\u0647\u0627\u0631\u062F\u0647").put(15, "\u067E\u0627\u0646\u0632\u062F\u0647")
.put(16, "\u0634\u0627\u0646\u0632\u062F\u0647").put(17, "\u0647\u0641\u062F\u0647").put(18, "\u0647\u062C\u062F\u0647")
.put(19, "\u0646\u0648\u0632\u062F\u0647").put(20, "\u0628\u06CC\u0633\u062A").put(30, "\u0633\u06CC")
.put(40, "\u0686\u0647\u0644").put(50, "\u067E\u0646\u062C\u0627\u0647").put(60, "\u0634\u0635\u062A")
.put(70, "\u0647\u0641\u062A\u0627\u062F").put(80, "\u0647\u0634\u062A\u0627\u062F").put(90, "\u0646\u0648\u062F")
.put(100, "\u0635\u062F").put(200, "\u062F\u0648\u06CC\u0633\u062A").put(300, "\u0633\u06CC\u0635\u062F")
.put(400, "\u0686\u0647\u0627\u0631\u0635\u062F").put(500, "\u067E\u0627\u0646\u0635\u062F")
.put(600, "\u0634\u0634\u0635\u062F").put(700, "\u0647\u0641\u062A\u0635\u062F").put(800, "\u0647\u0634\u062A\u0635\u062F")
.put(900, "\u0646\u0647\u0635\u062F").build();
}

@Override
public List<PluralForms> pluralForms()
{
return Arrays.asList(new PersianPluralForms(""), new PersianPluralForms("\u0647\u0632\u0627\u0631"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reformat so that each constructor call is on a separate line

new PersianPluralForms("\u0645\u06CC\u0644\u06CC\u0648\u0646"),
new PersianPluralForms("\u0645\u06CC\u0644\u06CC\u0627\u0631\u062F"));
}

@Override
public String currency()
{
return "";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add currency symbol or code

}

@Override
public char twoDigitsNumberSeparator()
{
return ' ';
}


}
Loading