-
Notifications
You must be signed in to change notification settings - Fork 966
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 pt-BR NumberToWords localisation #194
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d4035b
Changed the NumberToWords Converter to find the right converter by th…
akamud cb02f86
Updated the test to show the Converter did not get the default Number…
akamud ec3da20
Changed the NumberToWords Converter to find the right converter by th…
akamud 865a774
release notes for Pull Request #174
akamud d3e17d5
Updated the test to show the Converter did not get the default Number…
akamud cc2ab95
Added an extra test to show it can correctly resolve pt-BR and pt-PT …
akamud 933d183
some tidyup
MehdiK 6a4e191
Added pt-BR NumberToWords localisation
akamud d345af4
release notes added for PR #194
akamud 574a531
removed closed PR #174 from release notes
akamud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
46 changes: 46 additions & 0 deletions
46
src/Humanizer.Tests/Localisation/NumberToWordsFactoryTests.cs
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,46 @@ | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation | ||
{ | ||
public class NumberToWordsFactoryTests | ||
{ | ||
[Fact] | ||
public void CanGetTwoLetterIsoLanguageSpecificFactory() | ||
{ | ||
using (new AmbientCulture("ar")) | ||
{ | ||
string result = 1000000000.ToWords(); | ||
Assert.NotEqual("1000000000", result); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanGetRfcStandardLanguageSpecificFactory() | ||
{ | ||
using (new AmbientCulture("pt-BR")) | ||
{ | ||
string result = 1000000000.ToWords(); | ||
Assert.NotEqual("1000000000", result); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanGetCorrectRfcStandardLanguageSpecificFactory() | ||
{ | ||
string resultPtBr; | ||
const int number = 1000000000; | ||
using (new AmbientCulture("pt-BR")) | ||
{ | ||
resultPtBr = number.ToWords(); | ||
} | ||
|
||
string resultPtPt; | ||
using (new AmbientCulture("pt-PT")) | ||
{ | ||
resultPtPt = number.ToWords(); | ||
} | ||
|
||
Assert.NotEqual(resultPtBr, resultPtPt); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Humanizer.Tests/Localisation/pt-BR/NumberToWordsTests.cs
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,47 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.ptBR | ||
{ | ||
public class NumberToWordsTests : AmbientCulture | ||
{ | ||
public NumberToWordsTests() : base("pt-BR") { } | ||
|
||
[Theory] | ||
[InlineData(1, "um")] | ||
[InlineData(10, "dez")] | ||
[InlineData(11, "onze")] | ||
[InlineData(122, "cento e vinte e dois")] | ||
[InlineData(3501, "três mil quinhentos e um")] | ||
[InlineData(100, "cem")] | ||
[InlineData(1000, "mil")] | ||
[InlineData(100000, "cem mil")] | ||
[InlineData(1000000, "um milhão")] | ||
[InlineData(10000000, "dez milhões")] | ||
[InlineData(100000000, "cem milhões")] | ||
[InlineData(1000000000, "um bilhão")] | ||
[InlineData(111, "cento e onze")] | ||
[InlineData(1111, "mil cento e onze")] | ||
[InlineData(111111, "cento e onze mil cento e onze")] | ||
[InlineData(1111111, "um milhão cento e onze mil cento e onze")] | ||
[InlineData(11111111, "onze milhões cento e onze mil cento e onze")] | ||
[InlineData(111111111, "cento e onze milhões cento e onze mil cento e onze")] | ||
[InlineData(1111111111, "um bilhão cento e onze milhões cento e onze mil cento e onze")] | ||
[InlineData(123, "cento e vinte e três")] | ||
[InlineData(1234, "mil duzentos e trinta e quatro")] | ||
[InlineData(8100, "oito mil e cem")] | ||
[InlineData(12345, "doze mil trezentos e quarenta e cinco")] | ||
[InlineData(123456, "cento e vinte e três mil quatrocentos e cinquenta e seis")] | ||
[InlineData(1234567, "um milhão duzentos e trinta e quatro mil quinhentos e sessenta e sete")] | ||
[InlineData(12345678, "doze milhões trezentos e quarenta e cinco mil seiscentos e setenta e oito")] | ||
[InlineData(123456789, "cento e vinte e três milhões quatrocentos e cinquenta e seis mil setecentos e oitenta e nove")] | ||
[InlineData(1234567890, "um bilhão duzentos e trinta e quatro milhões quinhentos e sessenta e sete mil oitocentos e noventa")] | ||
[InlineData(1999, "mil novecentos e noventa e nove")] | ||
[InlineData(2014, "dois mil e quatorze")] | ||
[InlineData(2048, "dois mil e quarenta e oito")] | ||
public void ToWordsPortuguese(int number, string expected) | ||
{ | ||
Assert.Equal(expected, number.ToWords()); | ||
} | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
src/Humanizer/Localisation/NumberToWords/BrazilianPortugueseNumberToWordsConverter.cs
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,100 @@ | ||
using Humanizer.Localisation.NumberToWords; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Humanizer.Localisation.NumberToWords | ||
{ | ||
internal class BrazilianPortugueseNumberToWordsConverter : DefaultNumberToWordsConverter | ||
{ | ||
private static readonly string[] portugueseUnitsMap = { "zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove" }; | ||
private static readonly string[] portugueseTensMap = { "zero", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa" }; | ||
private static readonly string[] portugueseHundredsMap = { "zero", "cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos" }; | ||
|
||
public override string Convert(int number) | ||
{ | ||
if (number == 0) | ||
return "zero"; | ||
|
||
if (number < 0) | ||
return string.Format("menos {0}", Convert(Math.Abs(number))); | ||
|
||
var parts = new List<string>(); | ||
|
||
if ((number / 1000000000) > 0) | ||
{ | ||
if (number / 1000000000 > 2) | ||
{ | ||
parts.Add(string.Format("{0} bilhões", Convert(number / 1000000000))); | ||
} | ||
else | ||
{ | ||
parts.Add(string.Format("{0} bilhão", Convert(number / 1000000000))); | ||
} | ||
number %= 1000000000; | ||
} | ||
|
||
if ((number / 1000000) > 0) | ||
{ | ||
if (number / 1000000 > 2) | ||
{ | ||
parts.Add(string.Format("{0} milhões", Convert(number / 1000000))); | ||
} | ||
else | ||
{ | ||
parts.Add(string.Format("{0} milhão", Convert(number / 1000000))); | ||
} | ||
number %= 1000000; | ||
} | ||
|
||
if ((number / 1000) > 0) | ||
{ | ||
if (number / 1000 == 1) | ||
parts.Add("mil"); | ||
else | ||
parts.Add(string.Format("{0} mil", Convert(number / 1000))); | ||
|
||
number %= 1000; | ||
} | ||
|
||
if ((number / 100) > 0) | ||
{ | ||
if (number == 100) | ||
{ | ||
if (parts.Count > 0) | ||
{ | ||
parts.Add("e cem"); | ||
} | ||
else | ||
{ | ||
parts.Add("cem"); | ||
} | ||
} | ||
else | ||
{ | ||
parts.Add(portugueseHundredsMap[(number / 100)]); | ||
} | ||
|
||
number %= 100; | ||
} | ||
|
||
if (number > 0) | ||
{ | ||
if (parts.Count != 0) | ||
parts.Add("e"); | ||
|
||
if (number < 20) | ||
parts.Add(portugueseUnitsMap[number]); | ||
else | ||
{ | ||
var lastPart = portugueseTensMap[number / 10]; | ||
if ((number % 10) > 0) | ||
lastPart += string.Format(" e {0}", portugueseUnitsMap[number % 10]); | ||
|
||
parts.Add(lastPart); | ||
} | ||
} | ||
|
||
return string.Join(" ", parts.ToArray()); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this class provides much benefit as everything asserted here is already being tested much more thoroughly elsewhere. So removing it.