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

Various fixes/improvements. #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 1 addition & 14 deletions Fluent.Net.SimpleExample/TranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,6 @@ public string GetString(string id, IDictionary<string, object> args = null,
return "";
}

public string PreferredLocale => _contexts.First().Locales.First();

CultureInfo _culture;
public CultureInfo Culture
{
get
{
if (_culture == null)
{
_culture = new CultureInfo(PreferredLocale);
}
return _culture;
}
}
public CultureInfo Culture => _contexts.First().Culture;
}
}
2 changes: 1 addition & 1 deletion Fluent.Net.Test/IsolatingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void IsolatesInterpolatedDateTypedVariables()
var args = new Dictionary<string, object> { { "arg", dt } };
var msg = ctx.GetMessage("baz");
var val = ctx.Format(msg, args, errors);
var dtf = dt.ToString(new CultureInfo(ctx.Locales.First()));
var dtf = dt.ToString(ctx.Culture);
val.Should().Be($"{FSI}{dtf}{PDI} Baz");
errors.Count.Should().Be(0);
}
Expand Down
3 changes: 2 additions & 1 deletion Fluent.Net.Test/MessageContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Fluent.Net.Test
{
Expand Down Expand Up @@ -203,7 +204,7 @@ public void TestFluentNumber()
public void TestFluentDateTime()
{
var dt = new FluentDateTime(new DateTime(2009, 01, 02));
var ctx = new MessageContext("en-US");
var ctx = new MessageContext(new CultureInfo("en-US", useUserOverride: false));
dt.Format(ctx).Should().Be("1/2/2009 12:00:00 AM");
dt.Match(ctx, new FluentDateTime(new DateTime(2009, 01, 02))).Should().BeTrue();
dt.Match(ctx, new FluentDateTime(new DateTime(2009, 01, 03))).Should().BeFalse();
Expand Down
4 changes: 2 additions & 2 deletions Fluent.Net.Test/MessageContextTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using FluentAssertions;
using System.Collections.Generic;
using System.Globalization;

namespace Fluent.Net.Test
{
public class MessageContextTestBase : FtlTestBase
{
protected static MessageContext CreateContext(string ftl, bool useIsolating = false)
{
var locales = new string[] { "en-US", "en" };
var ctx = new MessageContext(locales, new MessageContextOptions()
var ctx = new MessageContext(new CultureInfo("en-US", useUserOverride: false), new MessageContextOptions()
{ UseIsolating = useIsolating });
var errors = ctx.AddMessages(Ftl(ftl));
errors.Should().BeEquivalentTo(new List<ParseException>());
Expand Down
62 changes: 28 additions & 34 deletions Fluent.Net.Test/Plural/LocaleRulesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,35 @@ namespace Fluent.Net.Test.Plural
public class LocaleRulesTest
{
[Test]
public void CheckEnRules()
[TestCase("en", "1", ExpectedResult = "one")]
[TestCase("en-US", "1", ExpectedResult = "one")]
[TestCase("en", "1.6", ExpectedResult = "other")]
[TestCase("en", "1.0", ExpectedResult = "other")]
[TestCase("en", "9", ExpectedResult = "other")]
// for lt:
// <pluralRule count="one">n % 10 = 1 and n % 100 != 11..19 @integer 1, 21, 31, 41, 51, 61, 71, 81, 101, 1001, … @decimal 1.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 101.0, 1001.0, …</pluralRule>
// <pluralRule count="few">n % 10 = 2..9 and n % 100 != 11..19 @integer 2~9, 22~29, 102, 1002, … @decimal 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 22.0, 102.0, 1002.0, …</pluralRule>
// <pluralRule count="many">f != 0 @decimal 0.1~0.9, 1.1~1.7, 10.1, 100.1, 1000.1, …</pluralRule>
// <pluralRule count="other"> @integer 0, 10~20, 30, 40, 50, 60, 100, 1000, 10000, 100000, 1000000, … @decimal 0.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …</pluralRule>
[TestCase("lt", "1", ExpectedResult = "one")]
[TestCase("lt", "11", ExpectedResult = "other")]
[TestCase("lt", "21", ExpectedResult = "one")]
[TestCase("lt", "91", ExpectedResult = "one")]
[TestCase("lt", "101", ExpectedResult = "one")]
[TestCase("lt", "2", ExpectedResult = "few")]
[TestCase("lt", "37", ExpectedResult = "few")]
[TestCase("lt", "9", ExpectedResult = "few")]
[TestCase("lt", "10", ExpectedResult = "other")]
[TestCase("lt", "23", ExpectedResult = "few")]
[TestCase("lt", "0.6", ExpectedResult = "many")]
[TestCase("lt", "1.1", ExpectedResult = "many")]
[TestCase("lt", "2.0", ExpectedResult = "few")]
[TestCase("lt", "1234.456", ExpectedResult = "many")]
[TestCase("lt", "14", ExpectedResult = "other")]
[TestCase("lt", "40", ExpectedResult = "other")]
public string TestPluralSelect(string locale, string num)
{
LocaleRules.Select("en", "1").Should().Be("one");
LocaleRules.Select("en", "1.6").Should().Be("other");
LocaleRules.Select("en", "1.0").Should().Be("other");
LocaleRules.Select("en", "9").Should().Be("other");
}

[Test]
public void CheckLtRules()
{
// <pluralRule count="one">n % 10 = 1 and n % 100 != 11..19 @integer 1, 21, 31, 41, 51, 61, 71, 81, 101, 1001, … @decimal 1.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 101.0, 1001.0, …</pluralRule>
// <pluralRule count="few">n % 10 = 2..9 and n % 100 != 11..19 @integer 2~9, 22~29, 102, 1002, … @decimal 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 22.0, 102.0, 1002.0, …</pluralRule>
// <pluralRule count="many">f != 0 @decimal 0.1~0.9, 1.1~1.7, 10.1, 100.1, 1000.1, …</pluralRule>
// <pluralRule count="other"> @integer 0, 10~20, 30, 40, 50, 60, 100, 1000, 10000, 100000, 1000000, … @decimal 0.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …</pluralRule>

LocaleRules.Select("lt", "1").Should().Be("one");
LocaleRules.Select("lt", "11").Should().Be("other");
LocaleRules.Select("lt", "21").Should().Be("one");
LocaleRules.Select("lt", "91").Should().Be("one");
LocaleRules.Select("lt", "101").Should().Be("one");

LocaleRules.Select("lt", "2").Should().Be("few");
LocaleRules.Select("lt", "37").Should().Be("few");
LocaleRules.Select("lt", "9").Should().Be("few");
LocaleRules.Select("lt", "10").Should().Be("other");
LocaleRules.Select("lt", "23").Should().Be("few");

LocaleRules.Select("lt", "0.6").Should().Be("many");
LocaleRules.Select("lt", "1.1").Should().Be("many");
LocaleRules.Select("lt", "2.0").Should().Be("few");
LocaleRules.Select("lt", "1234.456").Should().Be("many");

LocaleRules.Select("lt", "14").Should().Be("other");
LocaleRules.Select("lt", "40").Should().Be("other");
return LocaleRules.Select(locale, num);
}
}
}
35 changes: 35 additions & 0 deletions Fluent.Net/BuiltIns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace Fluent.Net
{
internal static class BuiltIns
{
public static FluentType Number(IList<object> args, IDictionary<string, object> options)
{
// TODO: add to errors? what we doin here?
if (args.Count != 1)
{
throw new Exception("Too many arguments to NUMBER() function");
}
if (args[0].GetType() != typeof(FluentNumber))
{
throw new Exception("NUMBER() expected an argument of type FluentNumber");
}
return (FluentNumber)args[0];
}

public static FluentType DateTime(IList<object> args, IDictionary<string, object> options)
{
if (args.Count != 1)
{
throw new Exception("Too many arguments to DATETIME() function");
}
if (args[0].GetType() != typeof(FluentDateTime))
{
throw new Exception("DATETIME() expected an argument of type FluentDateTime");
}
return (FluentDateTime)args[0];
}
}
}
36 changes: 36 additions & 0 deletions Fluent.Net/FluentError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Fluent.Net
{
public abstract class FluentError
{
public string Message { get; set; }

public FluentError(string message)
{
Message = message;
}
}

class RangeError : FluentError
{
public RangeError(string message) :
base(message)
{
}
}

class TypeError : FluentError
{
public TypeError(string message) :
base(message)
{
}
}

class ReferenceError : FluentError
{
public ReferenceError(string message) :
base(message)
{
}
}
}
30 changes: 30 additions & 0 deletions Fluent.Net/FluentResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.IO;
using Fluent.Net.RuntimeAst;

namespace Fluent.Net
{
/// <summary>
/// Fluent Resource is a structure storing a map
/// of localization entries.
/// </summary>
public class FluentResource
{
public IDictionary<string, Message> Entries { get; }
public IList<ParseException> Errors { get; }

public FluentResource(IDictionary<string, Message> entries,
IList<ParseException> errors)
{
Entries = entries;
Errors = errors;
}

public static FluentResource FromReader(TextReader reader)
{
var parser = new RuntimeParser();
var resource = parser.GetResource(reader);
return new FluentResource(resource.Entries, resource.Errors);
}
}
}
Loading