-
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 Farsi support to DateTime resource files and NumberToWords. #121
Changes from all commits
dc82951
0343fb7
9434a53
598ba9e
89ed90f
707479b
2f71596
52aa3f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.fa | ||
{ | ||
public class DateHumanizeTests : AmbientCulture | ||
{ | ||
public DateHumanizeTests() : base("fa") { } | ||
|
||
|
||
[Theory] | ||
[InlineData(1, "فردا")] | ||
[InlineData(13, "13 روز بعد")] | ||
[InlineData(-1, "دیروز")] | ||
[InlineData(-11, "11 روز پیش")] | ||
public void DaysAgo(int days, string expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should either rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This applies to all tests in this class |
||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddDays(days).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک ساعت بعد")] | ||
[InlineData(11, "11 ساعت بعد")] | ||
[InlineData(-1, "یک ساعت پیش")] | ||
[InlineData(-11, "11 ساعت پیش")] | ||
public void HoursAgo(int hours, string expected) | ||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddHours(hours).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک دقیقه بعد")] | ||
[InlineData(13, "13 دقیقه بعد")] | ||
[InlineData(-1, "یک دقیقه پیش")] | ||
[InlineData(-13, "13 دقیقه پیش")] | ||
public void MinutesAgo(int minutes, string expected) | ||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddMinutes(minutes).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک ماه بعد")] | ||
[InlineData(10, "10 ماه بعد")] | ||
[InlineData(-1, "یک ماه پیش")] | ||
[InlineData(-10, "10 ماه پیش")] | ||
public void MonthsAgo(int months, string expected) | ||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddMonths(months).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک ثانیه بعد")] | ||
[InlineData(11, "11 ثانیه بعد")] | ||
[InlineData(-1, "یک ثانیه پیش")] | ||
[InlineData(-11, "11 ثانیه پیش")] | ||
public void SecondsAgo(int seconds, string expected) | ||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddSeconds(seconds).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک سال بعد")] | ||
[InlineData(21, "21 سال بعد")] | ||
[InlineData(-1, "یک سال پیش")] | ||
[InlineData(-21, "21 سال پیش")] | ||
public void YearsAgo(int years, string expected) | ||
{ | ||
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.fa | ||
{ | ||
public class NumberToWordsTests : AmbientCulture | ||
{ | ||
public NumberToWordsTests() : base("fa") { } | ||
|
||
[InlineData(1, "یک")] | ||
[InlineData(10, "ده")] | ||
[InlineData(11, "یازده")] | ||
[InlineData(122, "صد و بیست و دو")] | ||
[InlineData(3501, "سه هزار و پانصد و یک")] | ||
[InlineData(100, "صد")] | ||
[InlineData(1000, "یک هزار")] | ||
[InlineData(100000, "صد هزار")] | ||
[InlineData(1000000, "یک میلیون")] | ||
[InlineData(10000000, "ده میلیون")] | ||
[InlineData(100000000, "صد میلیون")] | ||
[InlineData(1000000000, "یک میلیارد")] | ||
[InlineData(111, "صد و یازده")] | ||
[InlineData(1111, "یک هزار و صد و یازده")] | ||
[InlineData(111111, "صد و یازده هزار و صد و یازده")] | ||
[InlineData(1111111, "یک میلیون و صد و یازده هزار و صد و یازده")] | ||
[InlineData(11111111, "یازده میلیون و صد و یازده هزار و صد و یازده")] | ||
[InlineData(111111111, "صد و یازده میلیون و صد و یازده هزار و صد و یازده")] | ||
[InlineData(1111111111, "یک میلیارد و صد و یازده میلیون و صد و یازده هزار و صد و یازده")] | ||
[InlineData(123, "صد و بیست و سه")] | ||
[InlineData(1234, "یک هزار و دویست و سی و چهار")] | ||
[InlineData(12345, "دوازده هزار و سیصد و چهل و پنج")] | ||
[InlineData(123456, "صد و بیست و سه هزار و چهارصد و پنجاه و شش")] | ||
[InlineData(1234567, "یک میلیون و دویست و سی و چهار هزار و پانصد و شصت و هفت")] | ||
[InlineData(12345678, "دوازده میلیون و سیصد و چهل و پنج هزار و ششصد و هفتاد و هشت")] | ||
[InlineData(123456789, "صد و بیست و سه میلیون و چهارصد و پنجاه و شش هزار و هفتصد و هشتاد و نه")] | ||
[InlineData(1234567890, "یک میلیارد و دویست و سی و چهار میلیون و پانصد و شصت و هفت هزار و هشتصد و نود")] | ||
[Theory] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the |
||
public void ToWordsFarsi(int number, string expected) | ||
{ | ||
Assert.Equal(expected, number.ToWords()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.fa | ||
{ | ||
public class TimeSpanHumanizeTests : AmbientCulture | ||
{ | ||
public TimeSpanHumanizeTests() : base("fa") { } | ||
|
||
[Theory] | ||
[InlineData(7, "یک هفته")] | ||
[InlineData(77, "11 هفته")] | ||
public void Weeks(int days, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize()); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the redundant line breaks. |
||
|
||
[Theory] | ||
[InlineData(1, "یک روز")] | ||
[InlineData(3, "3 روز")] | ||
public void Days(int days, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromDays(days).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک ساعت")] | ||
[InlineData(11, "11 ساعت")] | ||
public void Hours(int hours, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromHours(hours).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک دقیقه")] | ||
[InlineData(11, "11 دقیقه")] | ||
public void Minutes(int minutes, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromMinutes(minutes).Humanize()); | ||
} | ||
|
||
|
||
[Theory] | ||
[InlineData(1, "یک ثانیه")] | ||
[InlineData(11, "11 ثانیه")] | ||
public void Seconds(int seconds, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromSeconds(seconds).Humanize()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, "یک میلی ثانیه")] | ||
[InlineData(11, "11 میلی ثانیه")] | ||
public void Milliseconds(int milliseconds, string expected) | ||
{ | ||
Assert.Equal(expected, TimeSpan.FromMilliseconds(milliseconds).Humanize()); | ||
} | ||
|
||
[Fact] | ||
public void NoTime() | ||
{ | ||
Assert.Equal("الآن", TimeSpan.Zero.Humanize()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.fa | ||
{ | ||
public class ToQuantityTests : AmbientCulture | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the first localisation done on ToQuantity. Exciting times! However, this doesn't fit into this PR. Please create a separate PR for |
||
{ | ||
public ToQuantityTests() | ||
: base("fa") { } | ||
|
||
[Theory] | ||
[InlineData("مرد", 0, "0 مرد")] | ||
[InlineData("مرد", 1, "1 مرد")] | ||
[InlineData("مرد", 5, "5 مرد")] | ||
public void ToQuantity(string word, int quatity, string expected) | ||
{ | ||
Assert.Equal(expected, word.ToQuantity(quatity)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("مرد", 0, "مرد")] | ||
[InlineData("مرد", 1, "مرد")] | ||
[InlineData("مرد", 5, "مرد ها")] | ||
public void ToQuantityWithNoQuantity(string word, int quatity, string expected) | ||
{ | ||
Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.None)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("مرد", 0, "0 مرد")] | ||
[InlineData("مرد", 1, "1 مرد")] | ||
[InlineData("مرد", 5, "5 مرد")] | ||
public void ToQuantityNumeric(string word, int quatity, string expected) | ||
{ | ||
// ReSharper disable once RedundantArgumentDefaultValue | ||
Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.Numeric)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("مرد", 2, "دو مرد")] | ||
[InlineData("مرد", 1, "یک مرد")] | ||
[InlineData("مرد", 1200, "یک هزار و دویست مرد")] | ||
public void ToQuantityWords(string word, int quatity, string expected) | ||
{ | ||
Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.Words)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Humanizer.Localisation.Quantifier | ||
{ | ||
internal class DefaultQuantifier : IQuantifier | ||
{ | ||
public string ToQuantity(string input, int quantity, ShowQuantityAs showQuantityAs) | ||
{ | ||
var transformedInput = TransformInput(input, quantity, showQuantityAs); | ||
|
||
if (showQuantityAs == ShowQuantityAs.None) | ||
return transformedInput; | ||
|
||
if (showQuantityAs == ShowQuantityAs.Numeric) | ||
return string.Format("{0} {1}", quantity, transformedInput); | ||
|
||
return string.Format("{0} {1}", quantity.ToWords(), transformedInput); | ||
} | ||
|
||
protected virtual string TransformInput(string input, int quantity, ShowQuantityAs showQuantityAs) | ||
{ | ||
return quantity == 1 | ||
? input.Singularize(Plurality.CouldBeEither) | ||
: input.Pluralize(Plurality.CouldBeEither); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Humanizer.Localisation.Quantifier | ||
{ | ||
internal class FarsiQuantifier : DefaultQuantifier | ||
{ | ||
protected override string TransformInput(string input, int quantity, ShowQuantityAs showQuantityAs) | ||
{ | ||
//TODO: Use singularize and pluralize for Farsi | ||
string postFix = string.Empty; | ||
|
||
if (showQuantityAs == ShowQuantityAs.None && quantity > 1) | ||
{ | ||
postFix = " ها"; | ||
} | ||
|
||
return string.Format("{0}{1}", input, postFix); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Humanizer.Localisation.Quantifier | ||
{ | ||
interface IQuantifier | ||
{ | ||
string ToQuantity(string input, int quantity, ShowQuantityAs showQuantityAs); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace Humanizer.Localisation.Quantifier | ||
{ | ||
internal class QuantifierFactory | ||
{ | ||
internal static IQuantifier GetQuantifier(CultureInfo culture) | ||
{ | ||
return GetQuantifier(culture.TwoLetterISOLanguageName); | ||
} | ||
|
||
internal static IQuantifier GetQuantifier() | ||
{ | ||
return GetQuantifier(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName); | ||
} | ||
|
||
private static IQuantifier GetQuantifier(string twoLetterISOLanguageName) | ||
{ | ||
switch (twoLetterISOLanguageName) | ||
{ | ||
case "fa": | ||
return new FarsiQuantifier(); | ||
default: | ||
return new DefaultQuantifier(); | ||
} | ||
} | ||
|
||
} | ||
} |
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.
We still don't know what version this is going to or when this is going to be released. So new features should be added under 'In Development'