-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds missing resources/tests for the Romanian locale and modifies the…
… RomanianFormatter so as to make it clearer what's happening and remove resource duplication
- Loading branch information
1 parent
abcf79b
commit 9572ee0
Showing
7 changed files
with
269 additions
and
71 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
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
85 changes: 85 additions & 0 deletions
85
src/Humanizer.Tests/Localisation/ro-Ro/TimeSpanHumanizerTests.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,85 @@ | ||
namespace Humanizer.Tests.Localisation.roRo | ||
{ | ||
using System; | ||
|
||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
/// <summary> | ||
/// Test that for values bigger than 19 "de" is added between the numeral | ||
/// and the time unit: http://ebooks.unibuc.ro/filologie/NForascu-DGLR/numerale.htm. | ||
/// There is no test for years since there are only 12 of them in a year. | ||
/// </summary> | ||
public class TimeSpanHumanizerTests : AmbientCulture | ||
{ | ||
public TimeSpanHumanizerTests() : base("ro-RO") | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 milisecundă")] | ||
[InlineData(14, "14 milisecunde")] | ||
[InlineData(21, "21 de milisecunde")] | ||
[InlineData(3000, "3 secunde")] | ||
public void Milliseconds(int millisSeconds, string expected) | ||
{ | ||
var actual = TimeSpan.FromMilliseconds(millisSeconds).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 secundă")] | ||
[InlineData(14, "14 secunde")] | ||
[InlineData(21, "21 de secunde")] | ||
[InlineData(156, "2 minute")] | ||
public void Seconds(int seconds, string expected) | ||
{ | ||
var actual = TimeSpan.FromSeconds(seconds).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 minut")] | ||
[InlineData(14, "14 minute")] | ||
[InlineData(21, "21 de minute")] | ||
[InlineData(156, "2 ore")] | ||
public void Minutes(int minutes, string expected) | ||
{ | ||
var actual = TimeSpan.FromMinutes(minutes).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 oră")] | ||
[InlineData(14, "14 ore")] | ||
[InlineData(21, "21 de ore")] | ||
[InlineData(48, "2 zile")] | ||
public void Hours(int hours, string expected) | ||
{ | ||
var actual = TimeSpan.FromHours(hours).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 zi")] | ||
[InlineData(6, "6 zile")] | ||
[InlineData(7, "1 săptămână")] | ||
[InlineData(14, "2 săptămâni")] | ||
[InlineData(21, "3 săptămâni")] | ||
public void Days(int days, string expected) | ||
{ | ||
var actual = TimeSpan.FromDays(days).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "1 săptămână")] | ||
[InlineData(14, "14 săptămâni")] | ||
[InlineData(21, "21 de săptămâni")] | ||
public void Weeks(int weeks, string expected) | ||
{ | ||
var actual = TimeSpan.FromDays(7 * weeks).Humanize(); | ||
Assert.Equal(expected, actual); | ||
} | ||
} | ||
} |
39 changes: 30 additions & 9 deletions
39
src/Humanizer/Localisation/Formatters/RomanianFormatter.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 |
---|---|---|
@@ -1,24 +1,45 @@ | ||
namespace Humanizer.Localisation.Formatters | ||
{ | ||
using System; | ||
using System.Globalization; | ||
|
||
internal class RomanianFormatter : DefaultFormatter | ||
{ | ||
private const string Above20PostFix = "_Above20"; | ||
private const int PrepositionIndicatingDecimals = 2; | ||
|
||
private const int MaxNumeralWithNoPreposition = 19; | ||
|
||
private const int MinNumeralWithNoPreposition = 1; | ||
|
||
private const string PrepositionResourceKey = "UnitPreposition"; | ||
|
||
private const string RomanianCultureCode = "ro"; | ||
|
||
private static readonly double Divider = Math.Pow(10, PrepositionIndicatingDecimals); | ||
|
||
private readonly CultureInfo romanianCulture; | ||
|
||
public RomanianFormatter() | ||
: base("ro") | ||
: base(RomanianCultureCode) | ||
{ | ||
romanianCulture = new CultureInfo(RomanianCultureCode); | ||
} | ||
|
||
protected override string GetResourceKey(string resourceKey, int number) | ||
protected override string Format(string resourceKey, int number) | ||
{ | ||
var mod100 = number%100; | ||
var format = Resources.GetResource(GetResourceKey(resourceKey, number), romanianCulture); | ||
var preposition = ShouldUsePreposition(number) | ||
? Resources.GetResource(PrepositionResourceKey, romanianCulture) | ||
: string.Empty; | ||
|
||
if (0 < mod100 && mod100 < 20) | ||
{ | ||
return resourceKey; | ||
} | ||
return format.FormatWith(number, preposition); | ||
} | ||
|
||
return resourceKey + Above20PostFix; | ||
private static bool ShouldUsePreposition(int number) | ||
{ | ||
var prepositionIndicatingNumeral = Math.Abs(number % Divider); | ||
return prepositionIndicatingNumeral < MinNumeralWithNoPreposition | ||
|| prepositionIndicatingNumeral > MaxNumeralWithNoPreposition; | ||
} | ||
} | ||
} |
Oops, something went wrong.