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

adds missing resources/tests for the Romanian locale #356

Merged
merged 1 commit into from
Dec 11, 2014
Merged
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
- [#356](https://github.com/MehdiK/Humanizer/pull/356): Added missing values for ro and changed the RomanianFormatter implementation so as to avoid duplicate resources
- [#350](https://github.com/MehdiK/Humanizer/pull/350): Added missing values for nl

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.31.0...master)
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Localisation\pl\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\NumberToWordsTests.cs" />
<Compile Include="Localisation\pt-BR\OrdinalizeTests.cs" />
<Compile Include="Localisation\ro-Ro\TimeSpanHumanizerTests.cs" />
<Compile Include="Localisation\ru-RU\NumberToWordsTests.cs" />
<Compile Include="Localisation\ru-RU\OrdinalizeTests.cs" />
<Compile Include="Localisation\ru-RU\TimeSpanHumanizeTests.cs" />
Expand Down
8 changes: 4 additions & 4 deletions src/Humanizer.Tests/Localisation/ResourcesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public void CanGetCultureSpecificTranslationsWithImplicitCulture()
{
using (new AmbientCulture("ro"))
{
var format = Resources.GetResource("DateHumanize_MultipleYearsAgo_Above20");
Assert.Equal("acum {0} de ani", format);
var format = Resources.GetResource("DateHumanize_MultipleYearsAgo");
Assert.Equal("acum {0}{1} ani", format);
}
}

[Fact]
public void CanGetCultureSpecificTranslationsWithExplicitCulture()
{
var format = Resources.GetResource("DateHumanize_MultipleYearsAgo_Above20", new CultureInfo("ro"));
Assert.Equal("acum {0} de ani", format);
var format = Resources.GetResource("DateHumanize_MultipleYearsAgo", new CultureInfo("ro"));
Assert.Equal("acum {0}{1} ani", format);
}
}
}
108 changes: 66 additions & 42 deletions src/Humanizer.Tests/Localisation/ro-Ro/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Humanizer.Localisation;
using Xunit;

namespace Humanizer.Tests.Localisation.roRo
{
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.
Expand All @@ -14,70 +15,93 @@ public DateHumanizeTests() : base("ro-RO")
{
}

[Fact]
public void RomanianTranslationIsCorrectForThreeHoursAgo()
[Theory]
[InlineData(3, "acum 3 ore")]
[InlineData(20, "acum 20 de ore")]
public void HoursAgo(int hours, string expected)
{
DateHumanize.Verify("acum 3 ore", 3, TimeUnit.Hour, Tense.Past);
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor20HoursAgo()
[Theory]
[InlineData(19, "acum 19 minute")]
[InlineData(60, "acum o oră")]
[InlineData(44, "acum 44 de minute")]
public void MinutesAgo(int minutes, string expected)
{
DateHumanize.Verify("acum 20 de ore", 20, TimeUnit.Hour, Tense.Past);
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor19MinutesAgo()

[Theory]
[InlineData(2, "acum 2 secunde")]
[InlineData(59, "acum 59 de secunde")]
public void SecondsAgo(int seconds, string expected)
{
DateHumanize.Verify("acum 19 minute", 19, TimeUnit.Minute, Tense.Past);
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor60MinutesAgo()

[Theory]
[InlineData(10, "acum 10 zile")]
[InlineData(23, "acum 23 de zile")]
public void DaysAgo(int seconds, string expected)
{
DateHumanize.Verify("acum o oră", 60, TimeUnit.Minute, Tense.Past);
DateHumanize.Verify(expected, seconds, TimeUnit.Day, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor44MinutesAgo()

[Theory]
[InlineData(119, "acum 119 ani")]
[InlineData(100, "acum 100 de ani")]
public void YearsAgo(int seconds, string expected)
{
DateHumanize.Verify("acum 44 de minute", 44, TimeUnit.Minute, Tense.Past);
DateHumanize.Verify(expected, seconds, TimeUnit.Year, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor2SecondsAgo()

[Theory]
[InlineData(1, "acum")]
[InlineData(22, "acum")]
public void MillisecondsAgo(int milliseconds, string expected)
{
DateHumanize.Verify("acum 2 secunde", 2, TimeUnit.Second, Tense.Past);
DateHumanize.Verify(expected, milliseconds, TimeUnit.Millisecond, Tense.Past);
}

[Fact]
public void RomanianTranslationIsCorrectFor59SecondsAgo()

[Theory]
[InlineData(19, "peste 19 secunde")]
[InlineData(21, "peste 21 de secunde")]
public void SecondsFromNow(int seconds, string expected)
{
DateHumanize.Verify("acum 59 de secunde", 59, TimeUnit.Second, Tense.Past);
DateHumanize.Verify(expected, seconds, TimeUnit.Second, Tense.Future);
}

[Fact]
public void RomanianTranslationIsCorrectFor10DaysAgo()

[Theory]
[InlineData(19, "peste 19 minute")]
[InlineData(22, "peste 22 de minute")]
public void MinutesFromNow(int minutes, string expected)
{
DateHumanize.Verify("acum 10 zile", 10, TimeUnit.Day, Tense.Past);
DateHumanize.Verify(expected, minutes, TimeUnit.Minute, Tense.Future);
}

[Fact]
public void RomanianTranslationIsCorrectFor23DaysAgo()

[Theory]
[InlineData(3, "peste 3 ore")]
[InlineData(23, "peste 23 de ore")]
public void HoursFromNow(int hours, string expected)
{
DateHumanize.Verify("acum 23 de zile", 23, TimeUnit.Day, Tense.Past);
DateHumanize.Verify(expected, hours, TimeUnit.Hour, Tense.Future);
}

[Fact]
public void RomanianTranslationIsCorrectFor119YearsAgo()
[Theory]
[InlineData(5, "peste 5 zile")]
[InlineData(23, "peste 23 de zile")]
public void DaysFromNow(int days, string expected)
{
DateHumanize.Verify("acum 119 ani", 119, TimeUnit.Year, Tense.Past);
DateHumanize.Verify(expected, days, TimeUnit.Day, Tense.Future);
}

[Fact]
public void RomanianTranslationIsCorrectFor100YearsAgo()

[Theory]
[InlineData(5, "peste 5 ani")]
[InlineData(21, "peste 21 de ani")]
public void YearsFromNow(int years, string expected)
{
DateHumanize.Verify("acum 100 de ani", 100, TimeUnit.Year, Tense.Past);
DateHumanize.Verify(expected, years, TimeUnit.Year, Tense.Future);
}
}
}
85 changes: 85 additions & 0 deletions src/Humanizer.Tests/Localisation/ro-Ro/TimeSpanHumanizerTests.cs
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);
}
}
}
35 changes: 26 additions & 9 deletions src/Humanizer/Localisation/Formatters/RomanianFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
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;
}
}
}
Loading