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 1 commit
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
Next Next commit
Updated Spanish ToOrdinalWords with gender
  • Loading branch information
thunsaker committed Apr 12, 2014
commit c22665fd2661bf202c09b34542af583c43949a90
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
21 changes: 14 additions & 7 deletions src/Humanizer.Tests/Localisation/es/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.es
{
public class NumberToWordsTests : AmbientCulture
{
namespace Humanizer.Tests.Localisation.es {
public class NumberToWordsTests : AmbientCulture {
public NumberToWordsTests() : base("es-ES") { }

[Theory]
Expand Down Expand Up @@ -43,9 +41,18 @@ public NumberToWordsTests() : base("es-ES") { }
[InlineData(1999, "mil novecientos noventa y nueve")]
[InlineData(2014, "dos mil catorce")]
[InlineData(2048, "dos mil cuarenta y ocho")]
public void ToWordsSpanish(int number, string expected)
{
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
@@ -1,16 +1,27 @@
using System;
using System.Collections.Generic;

namespace Humanizer.Localisation.NumberToWords
{
internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter
{
namespace Humanizer.Localisation.NumberToWords {
internal class SpanishNumberToWordsConverter : DefaultNumberToWordsConverter {
private static readonly string[] HundredsMap = { "cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos", "ochocientos", "novecientos" };
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" };

public override string Convert(int number)
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)
return "cero";

Expand All @@ -19,53 +30,45 @@ public override string Convert(int number)

var parts = new List<string>();

if ((number / 1000000000) > 0)
{
parts.Add(number/1000000000 == 1
if ((number / 1000000000) > 0) {
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
if ((number / 1000000) > 0) {
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
if ((number / 1000) > 0) {
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)]);
if ((number / 100) > 0) {
parts.Add(number == 100 ? string.Format("cien") : HundredsMap[(number / 100)]);
number %= 100;
}

if (number > 0)
{
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]);

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

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

return towords;
}
}
}