Skip to content

Commit

Permalink
Merge pull request #55 from harouny/master
Browse files Browse the repository at this point in the history
Add Arabic formatter and Arabic TimeSpan and Date localization for multiple units
  • Loading branch information
MehdiK committed Jan 9, 2014
2 parents 4594508 + 711e270 commit 03d6497
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CasingTests.cs" />
<Compile Include="Localisation\ar\DateHumanizeTests.cs" />
<Compile Include="Localisation\ar\TimeSpanTests.cs" />
<Compile Include="Localisation\DateHumanizeTests.nb-NO.cs" />
<Compile Include="Localisation\es\DateHumanizeTests.cs" />
Expand Down
78 changes: 78 additions & 0 deletions src/Humanizer.Tests/Localisation/ar/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.ar
{
public class DateHumanizeTests : AmbientCulture
{
public DateHumanizeTests() : base("ar") { }


[Theory]
[InlineData(-1, "أمس")]
[InlineData(-2, "منذ يومين")]
[InlineData(-3, "منذ 3 أيام")]
[InlineData(-11, "منذ 11 يوم")]
public void DaysAgo(int days, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddDays(days).Humanize());
}

[Theory]
[InlineData(-2, "منذ ساعتين")]
[InlineData(-1, "منذ ساعة واحدة")]
[InlineData(-3, "منذ 3 ساعات")]
[InlineData(-11, "منذ 11 ساعة")]
public void HoursAgo(int hours, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddHours(hours).Humanize());
}

[Theory]
[InlineData(-2, "منذ دقيقتين")]
[InlineData(-1, "منذ دقيقة واحدة")]
[InlineData(-3, "منذ 3 دقائق")]
[InlineData(-11, "منذ 11 دقيقة")]
public void MinutesAgo(int minutes, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMinutes(minutes).Humanize());
}

[Theory]
[InlineData(-2, "منذ شهرين")]
[InlineData(-1, "منذ شهر واحد")]
[InlineData(-3, "منذ 3 أشهر")]
[InlineData(-11, "منذ 11 شهر")]
public void MonthsAgo(int months, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddMonths(months).Humanize());
}

[Theory]
[InlineData(-2, "منذ ثانيتين")]
[InlineData(-1, "منذ ثانية واحدة")]
[InlineData(-3, "منذ 3 ثوان")]
[InlineData(-11, "منذ 11 ثانية")]
public void SecondsAgo(int seconds, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddSeconds(seconds).Humanize());
}

[Theory]
[InlineData(-2, "منذ عامين")]
[InlineData(-1, "العام السابق")]
[InlineData(-3, "منذ 3 أعوام")]
[InlineData(-11, "منذ 11 عام")]
public void YearsAgo(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
}

[Fact]
public void NotYet()
{
Assert.Equal("ليس بعد", DateTime.UtcNow.AddDays(1).Humanize());
}
}
}
62 changes: 44 additions & 18 deletions src/Humanizer.Tests/Localisation/ar/TimeSpanTests.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
using System;
using Humanizer.Tests;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.ar
{
public class TimeSpanHumanizeExtensionsTests : AmbientCulture
{
public TimeSpanHumanizeExtensionsTests() : base("ar") { }

[Fact]
public void OneWeek()
[Theory]
[InlineData(7, "أسبوع واحد")]
[InlineData(14, "أسبوعين")]
[InlineData(21, "3 أسابيع")]
[InlineData(77, "11 أسبوع")]
public void Weeks(int days, string expected)
{
Assert.Equal("أسبوع واحد", TimeSpan.FromDays(7).Humanize());
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize());
}

[Fact]
public void OneDay()

[Theory]
[InlineData(1, "يوم واحد")]
[InlineData(2, "يومين")]
[InlineData(3, "3 أيام")]
public void Days(int days, string expected)
{
Assert.Equal("يوم واحد", TimeSpan.FromDays(1).Humanize());
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize());
}

[Fact]
public void OneHour()
[Theory]
[InlineData(1, "ساعة واحدة")]
[InlineData(2, "ساعتين")]
[InlineData(3, "3 ساعات")]
[InlineData(11, "11 ساعة")]
public void Hours(int hours, string expected)
{
Assert.Equal("ساعة واحدة", TimeSpan.FromHours(1).Humanize());
Assert.Equal(expected, TimeSpan.FromHours(hours).Humanize());
}

[Fact]
public void OneMinute()
[Theory]
[InlineData(1, "دقيقة واحدة")]
[InlineData(2, "دقيقتين")]
[InlineData(3, "3 دقائق")]
[InlineData(11, "11 دقيقة")]
public void Minutes(int minutes, string expected)
{
Assert.Equal("دقيقة واحدة", TimeSpan.FromMinutes(1).Humanize());
Assert.Equal(expected, TimeSpan.FromMinutes(minutes).Humanize());
}

[Fact]
public void OneSecond()

[Theory]
[InlineData(1, "ثانية واحدة")]
[InlineData(2, "ثانيتين")]
[InlineData(3, "3 ثوان")]
[InlineData(11, "11 ثانية")]
public void Seconds(int seconds, string expected)
{
Assert.Equal("ثانية واحدة", TimeSpan.FromSeconds(1).Humanize());
Assert.Equal(expected, TimeSpan.FromSeconds(seconds).Humanize());
}

[Fact]
public void OneMillisecond()
[Theory]
[InlineData(1, "جزء من الثانية")]
[InlineData(2, "جزئين من الثانية")]
[InlineData(3, "3 أجزاء من الثانية")]
[InlineData(11, "11 جزء من الثانية")]
public void Milliseconds(int milliseconds, string expected)
{
Assert.Equal("جزء من الثانية", TimeSpan.FromMilliseconds(1).Humanize());
Assert.Equal(expected, TimeSpan.FromMilliseconds(milliseconds).Humanize());
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion src/Humanizer/Configuration/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static class Configurator
new Dictionary<string, Func<IFormatter>>(StringComparer.OrdinalIgnoreCase)
{
{ "ro", () => new RomanianFormatter() },
{ "ru", () => new RussianFormatter() }
{ "ru", () => new RussianFormatter() },
{ "ar", () => new ArabicFormatter() },
};

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>On.Days.tt</DependentUpon>
</Compile>
<Compile Include="Localisation\ArabicFormatter.cs" />
<Compile Include="ToQuantityExtensions.cs" />
<Compile Include="Transformer\To.cs" />
<Compile Include="Transformer\IStringTransformer.cs" />
Expand Down
21 changes: 21 additions & 0 deletions src/Humanizer/Localisation/ArabicFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Humanizer.Localisation
{
internal class ArabicFormatter : DefaultFormatter
{
private const string DualPostfix = "_Dual";
private const string PluralPostfix = "_Plural";

protected override string GetResourceKey(string resourceKey, int number)
{
//In Arabic pluralization 2 entities gets a different word.
if (number == 2)
return resourceKey + DualPostfix;

//In Arabic pluralization entities where the count is between 3 and 10 gets a different word.
if (number >= 3 && number <= 10 )
return resourceKey + PluralPostfix;

return resourceKey;
}
}
}
132 changes: 126 additions & 6 deletions src/Humanizer/Properties/Resources.ar.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,47 +126,47 @@
<comment>one second ago</comment>
</data>
<data name="DateHumanize_MultipleSecondsAgo" xml:space="preserve">
<value>منذ {0} ثوان</value>
<value>منذ {0} ثانية</value>
<comment>{0} seconds ago</comment>
</data>
<data name="DateHumanize_SingleMinuteAgo" xml:space="preserve">
<value>منذ دقيقة واحدة</value>
<comment>a minute ago</comment>
</data>
<data name="DateHumanize_MultipleMinutesAgo" xml:space="preserve">
<value>منذ {0} دقائق</value>
<value>منذ {0} دقيقة</value>
<comment>{0} minutes ago</comment>
</data>
<data name="DateHumanize_SingleHourAgo" xml:space="preserve">
<value>منذ ساعة واحدة</value>
<comment>an hour ago</comment>
</data>
<data name="DateHumanize_MultipleHoursAgo" xml:space="preserve">
<value>منذ {0} ساعات</value>
<value>منذ {0} ساعة</value>
<comment>{0} hours ago</comment>
</data>
<data name="DateHumanize_SingleDayAgo" xml:space="preserve">
<value>أمس</value>
<comment>yesterday</comment>
</data>
<data name="DateHumanize_MultipleDaysAgo" xml:space="preserve">
<value>منذ {0} أيام</value>
<value>منذ {0} يوم</value>
<comment>{0} days ago</comment>
</data>
<data name="DateHumanize_SingleMonthAgo" xml:space="preserve">
<value>منذ شهر واحد</value>
<comment>one month ago</comment>
</data>
<data name="DateHumanize_MultipleMonthsAgo" xml:space="preserve">
<value>منذ {0} أشهر</value>
<value>منذ {0} شهر</value>
<comment>{0} months ago</comment>
</data>
<data name="DateHumanize_SingleYearAgo" xml:space="preserve">
<value>العام السابق</value>
<comment>one year ago</comment>
</data>
<data name="DateHumanize_MultipleYearsAgo" xml:space="preserve">
<value>منذ {0} سنوات</value>
<value>منذ {0} عام</value>
<comment>{0} years ago</comment>
</data>
<data name="TimeSpanHumanize_SingleDay" xml:space="preserve">
Expand Down Expand Up @@ -197,4 +197,124 @@
<value>حالاً</value>
<comment>no time</comment>
</data>
<data name="DateHumanize_MultipleDaysAgo_Dual" xml:space="preserve">
<value>منذ يومين</value>
<comment>two days ago</comment>
</data>
<data name="DateHumanize_MultipleHoursAgo_Dual" xml:space="preserve">
<value>منذ ساعتين</value>
<comment>two hours ago</comment>
</data>
<data name="DateHumanize_MultipleMinutesAgo_Dual" xml:space="preserve">
<value>منذ دقيقتين</value>
<comment>two minutes ago</comment>
</data>
<data name="DateHumanize_MultipleMonthsAgo_Dual" xml:space="preserve">
<value>منذ شهرين</value>
<comment>two months ago</comment>
</data>
<data name="DateHumanize_MultipleSecondsAgo_Dual" xml:space="preserve">
<value>منذ ثانيتين</value>
<comment>two seconds ago</comment>
</data>
<data name="DateHumanize_MultipleYearsAgo_Dual" xml:space="preserve">
<value>منذ عامين</value>
<comment>two years ago</comment>
</data>
<data name="TimeSpanHumanize_MultipleWeeks_Dual" xml:space="preserve">
<value>أسبوعين</value>
<comment>two weeks</comment>
</data>
<data name="TimeSpanHumanize_MultipleDays_Dual" xml:space="preserve">
<value>يومين</value>
<comment>two days</comment>
</data>
<data name="TimeSpanHumanize_MultipleHours_Dual" xml:space="preserve">
<value>ساعتين</value>
<comment>two hours</comment>
</data>
<data name="TimeSpanHumanize_MultipleMilliseconds_Dual" xml:space="preserve">
<value>جزئين من الثانية</value>
<comment>two milliseconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleMinutes_Dual" xml:space="preserve">
<value>دقيقتين</value>
<comment>two minutes</comment>
</data>
<data name="TimeSpanHumanize_MultipleSeconds_Dual" xml:space="preserve">
<value>ثانيتين</value>
<comment>two seconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleDays" xml:space="preserve">
<value>{0} يوم</value>
<comment>{0} days</comment>
</data>
<data name="TimeSpanHumanize_MultipleDays_Plural" xml:space="preserve">
<value>{0} أيام</value>
<comment>{0} days</comment>
</data>
<data name="TimeSpanHumanize_MultipleHours" xml:space="preserve">
<value>{0} ساعة</value>
<comment>{0} hours</comment>
</data>
<data name="TimeSpanHumanize_MultipleHours_Plural" xml:space="preserve">
<value>{0} ساعات</value>
<comment>{0} hours</comment>
</data>
<data name="TimeSpanHumanize_MultipleMilliseconds" xml:space="preserve">
<value>{0} جزء من الثانية</value>
<comment>{0} milliseconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleMilliseconds_Plural" xml:space="preserve">
<value>{0} أجزاء من الثانية</value>
<comment>{0} milliseconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleMinutes" xml:space="preserve">
<value>{0} دقيقة</value>
<comment>{0} minutes</comment>
</data>
<data name="TimeSpanHumanize_MultipleMinutes_Plural" xml:space="preserve">
<value>{0} دقائق</value>
<comment>{0} minutes</comment>
</data>
<data name="TimeSpanHumanize_MultipleSeconds" xml:space="preserve">
<value>{0} ثانية</value>
<comment>{0} seconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleSeconds_Plural" xml:space="preserve">
<value>{0} ثوان</value>
<comment>{0} seconds</comment>
</data>
<data name="TimeSpanHumanize_MultipleWeeks" xml:space="preserve">
<value>{0} أسبوع</value>
<comment>{0} weeks</comment>
</data>
<data name="TimeSpanHumanize_MultipleWeeks_Plural" xml:space="preserve">
<value>{0} أسابيع</value>
<comment>{0} weeks</comment>
</data>
<data name="DateHumanize_MultipleDaysAgo_Plural" xml:space="preserve">
<value>منذ {0} أيام</value>
<comment>{0} days ago</comment>
</data>
<data name="DateHumanize_MultipleHoursAgo_Plural" xml:space="preserve">
<value>منذ {0} ساعات</value>
<comment>{0} hours ago</comment>
</data>
<data name="DateHumanize_MultipleMinutesAgo_Plural" xml:space="preserve">
<value>منذ {0} دقائق</value>
<comment>{0} minutes ago</comment>
</data>
<data name="DateHumanize_MultipleMonthsAgo_Plural" xml:space="preserve">
<value>منذ {0} أشهر</value>
<comment>{0} months ago</comment>
</data>
<data name="DateHumanize_MultipleSecondsAgo_Plural" xml:space="preserve">
<value>منذ {0} ثوان</value>
<comment>{0} seconds ago</comment>
</data>
<data name="DateHumanize_MultipleYearsAgo_Plural" xml:space="preserve">
<value>منذ {0} أعوام</value>
<comment>{0} years ago</comment>
</data>
</root>

0 comments on commit 03d6497

Please sign in to comment.