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

Added Spanish ToOrdinalWords translations #188

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###In Development
- [#188](https://github.com/Mehdik/Humanizer/pull/188): Added Spanish ToOrdinalWords translations
- [#166](https://github.com/MehdiK/Humanizer/pull/166): Added Dutch (NL) Number to words and ordinals

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.21.15...master)
Expand Down
13 changes: 12 additions & 1 deletion src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@ public void ToWordsSpanish(int number, string expected)
{
Assert.Equal(expected, number.ToWords());
}

[Theory]
[InlineData(1, "primero", null)]
[InlineData(2, "segundo", GrammaticalGender.Masculine)]
[InlineData(2, "segunda", GrammaticalGender.Feminine)]
[InlineData(2, "segundo", GrammaticalGender.Neuter)]
[InlineData(11, "once", null)]
public void ToOrdinalWordsSpanish(int number, string words, GrammaticalGender gender)
{
Assert.Equal(words, number.ToOrdinalWords(gender));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter
private static readonly string[] UnitsMap = { "cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve" };
private static readonly string[] TensMap = { "cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };

private static readonly Dictionary<int, string> Ordinals = new Dictionary<int, string>
{
{1, "primero"},
Copy link
Member

Choose a reason for hiding this comment

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

I got my answer here.

{2, "segundo"},
{3, "tercero"},
{4, "quarto"},
{5, "quinto"},
{6, "sexto"},
{7, "séptimo"},
{8, "octavo"},
{9, "noveno"},
{10, "décimo"}
};

public override string Convert(int number)
{
if (number == 0)
Expand All @@ -21,43 +35,42 @@ public override string Convert(int number)

if ((number / 1000000000) > 0)
{
parts.Add(number/1000000000 == 1
parts.Add(number / 1000000000 == 1
? string.Format("mil millones")
: string.Format("{0} mil millones", Convert(number/1000000000)));
: string.Format("{0} mil millones", Convert(number / 1000000000)));

number %= 1000000000;
}

if ((number / 1000000) > 0)
{
parts.Add(number/1000000 == 1
parts.Add(number / 1000000 == 1
? string.Format("millón")
: string.Format("{0} millones", Convert(number/1000000)));
: string.Format("{0} millones", Convert(number / 1000000)));

number %= 1000000;
}

if ((number / 1000) > 0)
{
parts.Add(number/1000 == 1
parts.Add(number / 1000 == 1
? string.Format("mil")
: string.Format("{0} mil", Convert(number/1000)));
: string.Format("{0} mil", Convert(number / 1000)));

number %= 1000;
}

if ((number / 100) > 0)
{
parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number/100)]);
parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number / 100)]);
number %= 100;
}

if (number > 0)
{
if (number < 20)
parts.Add(UnitsMap[number]);
else if (number > 20 && number < 30)
{
else if (number > 20 && number < 30) {
var lastPart = TensMap[number / 10];
if ((number % 10) > 0)
lastPart += string.Format(" {0}", UnitsMap[number % 10]);
Expand All @@ -77,9 +90,15 @@ public override string Convert(int number)
return string.Join(" ", parts.ToArray());
}

public override string ConvertToOrdinal(int number)
public override string ConvertToOrdinal(int number, GrammaticalGender gender = GrammaticalGender.Masculine)
{
throw new NotImplementedException();
string towords;
if (!Ordinals.TryGetValue(number, out towords))
towords = Convert(number);
if (gender == GrammaticalGender.Feminine)
towords = towords.TrimEnd('o') + "a";

return towords;
}
}
}