Skip to content

Commit

Permalink
Adds HasAttrMessage, and obsoletes TryGetAttrMsg and TryGetMsg. (#26
Browse files Browse the repository at this point in the history
)

- Adds `LinguiniBundle.HasAttrMessage` method
- Obsoletes `LinguiniBundle.TryGetAttrMsg` for `LinguiniBundle.TryGetAttrMessage`
- Obsoletes `LinguiniBundle.TryGetMsg` for `LinguiniBundle.TryGetMessage`
  • Loading branch information
Ygg01 authored Nov 19, 2022
1 parent 602bd6b commit 038a3c3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
32 changes: 25 additions & 7 deletions Linguini.Bundle.Test/Unit/BundleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public void TestReplaceMessage()
.SetUseIsolating(false);

var bundle = bundler.UncheckedBuild();
Assert.IsTrue(bundle.TryGetAttrMsg("term1", null, out _, out var termMsg));
Assert.IsTrue(bundle.TryGetAttrMessage("term1", null, out _, out var termMsg));
Assert.AreEqual("val1", termMsg);
Assert.IsTrue(bundle.TryGetAttrMsg("term2", null, out _, out var termMsg2));
Assert.IsTrue(bundle.TryGetAttrMessage("term2", null, out _, out var termMsg2));
Assert.AreEqual("val2", termMsg2);

bundle.AddResourceOverriding(_replace2);
Assert.IsTrue(bundle.TryGetAttrMsg("term2", null, out _, out _));
Assert.IsTrue(bundle.TryGetAttrMsg("term1", null, out _, out termMsg));
Assert.IsTrue(bundle.TryGetAttrMessage("term2", null, out _, out _));
Assert.IsTrue(bundle.TryGetAttrMessage("term1", null, out _, out termMsg));
Assert.AreEqual("xxx", termMsg);
Assert.IsTrue(bundle.TryGetAttrMsg("new1.attr", null, out _, out var newMsg));
Assert.IsTrue(bundle.TryGetAttrMessage("new1.attr", null, out _, out var newMsg));
Assert.AreEqual("6", newMsg);
}

Expand Down Expand Up @@ -150,7 +150,7 @@ public void TestConcurrencyBundler()

Parallel.For(0, 10, i => bundler.AddResource($"term-1 = {i}", out _));
Parallel.For(0, 10, i => bundler.AddResource($"term-2= {i}", out _));
Parallel.For(0, 10, i => bundler.TryGetAttrMsg("term-1", null, out _, out _));
Parallel.For(0, 10, i => bundler.TryGetAttrMessage("term-1", null, out _, out _));
Parallel.For(0, 10, i => bundler.AddResourceOverriding($"term-2= {i + 1}"));
Assert.True(bundler.HasMessage("term-1"));
}
Expand All @@ -166,7 +166,7 @@ public void TestConcurrencyOption()
var optBundle = FluentBundle.MakeUnchecked(bundleOpt);
Parallel.For(0, 10, i => optBundle.AddResource($"term-1 = {i}", out _));
Parallel.For(0, 10, i => optBundle.AddResource($"term-2= {i}", out _));
Parallel.For(0, 10, i => optBundle.TryGetAttrMsg("term-1", null, out _, out _));
Parallel.For(0, 10, i => optBundle.TryGetAttrMessage("term-1", null, out _, out _));
Parallel.For(0, 10, i => optBundle.AddResourceOverriding($"term-2= {i + 1}"));
Assert.True(optBundle.HasMessage("term-1"));
}
Expand All @@ -190,6 +190,24 @@ public void TestExample()
Assert.AreEqual("Hello, Test!", message);
}

[Test]
[Parallelizable]
[TestCase("new1.attr", true)]
[TestCase("new1", true)]
[TestCase("new1.", false)]
[TestCase("new2", false)]
public void TestHasAttrMessage(string idWithAttr, bool found)
{
var bundle = LinguiniBuilder.Builder()
.CultureInfo(new CultureInfo("en"))
.AddResource(_replace2)
.SetUseIsolating(false)
.UncheckedBuild();

Assert.AreEqual(bundle.TryGetAttrMessage(idWithAttr, null, out _, out _),
bundle.HasAttrMessage(idWithAttr));
}

public static IEnumerable<TestCaseData> TestBundleErrors
{
get
Expand Down
57 changes: 47 additions & 10 deletions Linguini.Bundle/FluentBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using Linguini.Bundle.Builder;
using Linguini.Bundle.Errors;
using Linguini.Bundle.Resolver;
Expand Down Expand Up @@ -173,16 +174,35 @@ public void AddResourceOverriding(Resource res)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasMessage(string identifier)
{
var id = (identifier, EntryKind.Message);
return _entries.ContainsKey(id)
&& _entries[id] is AstMessage;
}

public bool HasAttrMessage(string idWithAttr)
{
var attributes = idWithAttr.IndexOf('.');
if (attributes < 0)
{
return HasMessage(idWithAttr);
}

var id = idWithAttr.AsSpan(0, attributes).ToString();
var attr = idWithAttr.AsSpan(attributes + 1).ToString();
if (TryGetAstMessage(id, out var astMessage))
{
return astMessage.GetAttribute(attr) != null;
}

return false;
}

public string? GetAttrMessage(string msgWithAttr, FluentArgs? args = null)
{
TryGetAttrMsg(msgWithAttr, args, out var errors, out var message);
TryGetAttrMessage(msgWithAttr, args, out var errors, out var message);
if (errors.Count > 0)
{
throw new LinguiniException(errors);
Expand All @@ -191,25 +211,23 @@ public bool HasMessage(string identifier)
return message;
}

public bool TryGetAttrMsg(string msgWithAttr, FluentArgs? args,
public bool TryGetAttrMessage(string msgWithAttr, FluentArgs? args,
out IList<FluentError> errors, out string? message)
{
if (msgWithAttr.Contains("."))
{
var split = msgWithAttr.Split('.');
return TryGetMsg(split[0], split[1], args, out errors, out message);
return TryGetMessage(split[0], split[1], args, out errors, out message);
}

return TryGetMsg(msgWithAttr, args, out errors, out message);
return TryGetMessage(msgWithAttr, null, args, out errors, out message);
}

public bool TryGetMsg(string id, FluentArgs? args,
public bool TryGetMessage(string id, FluentArgs? args,
out IList<FluentError> errors, [NotNullWhen(true)] out string? message)
{
return TryGetMsg(id, null, args, out errors, out message);
}
=> TryGetMessage(id, null, args, out errors, out message);

public bool TryGetMsg(string id, string? attribute, FluentArgs? args,
public bool TryGetMessage(string id, string? attribute, FluentArgs? args,
out IList<FluentError> errors, [NotNullWhen(true)] out string? message)
{
string? value = null;
Expand Down Expand Up @@ -325,5 +343,24 @@ public FluentBundle DeepClone()
UseIsolating = UseIsolating,
};
}

#region Obsolete

[Obsolete("Use TryGetAttrMessage(string, FluentArgs?, out IList<FluentError>, out string?)")]
public bool TryGetAttrMsg(string msgWithAttr, FluentArgs? args,
out IList<FluentError> errors, out string? message) =>
TryGetAttrMessage(msgWithAttr, args, out errors, out message);

[Obsolete("Use TryGetMessage(string, FluentArgs?, out IList<FluentError>, out string?")]
public bool TryGetMsg(string id, FluentArgs? args,
out IList<FluentError> errors, [NotNullWhen(true)] out string? message)
=> TryGetMessage(id, args, out errors, out message);

[Obsolete("Use TryGetMessage")]
public bool TryGetMsg(string id, string? attribute, FluentArgs? args,
out IList<FluentError> errors, [NotNullWhen(true)] out string? message)
=> TryGetMessage(id, attribute, args, out errors, out message);

#endregion
}
}
}
2 changes: 1 addition & 1 deletion Linguini.Bundle/Linguini.Bundle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ It provides easy to use and extend system for describing translations.</Descript
<Win32Resource />
<PackageProjectUrl>https://github.com/Ygg01/Linguini</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<PackageVersion>0.3.1</PackageVersion>
<PackageVersion>0.3.2</PackageVersion>
<TargetFrameworks>net461;net5.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

Expand Down
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ version 0.3.0
version 0.3.1
========

- Fixed error in `Linguini.Bundle` that prevented term and scope arguments from coexisting. @adcdefg30
- Fixed error in `Linguini.Bundle` that prevented term and scope arguments from coexisting. @adcdefg30

version 0.3.2
========

- Adds `LinguiniBundle.HasAttrMessage` method
- Obsoletes `LinguiniBundle.TryGetAttrMsg` for `LinguiniBundle.TryGetAttrMessage`
- Obsoletes `LinguiniBundle.TryGetMsg` for `LinguiniBundle.TryGetMessage`

0 comments on commit 038a3c3

Please sign in to comment.