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

Added support for nesting formats in LocalizationFormatter #350

Merged
merged 2 commits into from
Sep 9, 2023
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
15 changes: 14 additions & 1 deletion src/SmartFormat.Tests/Extensions/LocalizationFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,17 @@ public void Combine_With_PluralLocalizationFormatter(string format, int count, s
var actual = smart.Format(CultureInfo.GetCultureInfo(cultureName), format, count);
Assert.That(actual, Is.EqualTo(expected));
}
}

[TestCase("{:L:{ProductType}}", "en", "paper", "Paper")]
[TestCase("{:L:{ProductType}}", "de", "paper", "das Papier")]
[TestCase("{:L:{ProductType}}", "fr", "paper", "Papier")]
[TestCase("{:L:{ProductType}}", "en", "pen", "Pen")]
[TestCase("{:L:{ProductType}}", "de", "pen", "der Kugelschreiber")]
[TestCase("{:L:{ProductType}}", "fr", "pen", "Bic")]
public void Combine_With_Nesting(string format, string cultureName, string productType, string expected)
{
var smart = GetFormatterWithRegisteredResource();
var actual = smart.Format(CultureInfo.GetCultureInfo(cultureName), format, new { ProductType = productType });
Assert.That(actual, Is.EqualTo(expected));
}
}
6 changes: 6 additions & 0 deletions src/SmartFormat.Tests/Localization/LocTest1.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@
<data name="{} items" xml:space="preserve">
<value>{} Elemente</value>
</data>
<data name="paper" xml:space="preserve">
<value>das Papier</value>
</data>
<data name="pen" xml:space="preserve">
<value>der Kugelschreiber</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/SmartFormat.Tests/Localization/LocTest1.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@
<data name="{} items" xml:space="preserve">
<value>{} elementos</value>
</data>
<data name="paper" xml:space="preserve">
<value>Papel</value>
</data>
<data name="pen" xml:space="preserve">
<value>Bolígrafo</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/SmartFormat.Tests/Localization/LocTest1.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@
<data name="{} items" xml:space="preserve">
<value>{} éléments</value>
</data>
<data name="paper" xml:space="preserve">
<value>Papier</value>
</data>
<data name="pen" xml:space="preserve">
<value>Bic</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/SmartFormat.Tests/Localization/LocTest1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@
<data name="{} items" xml:space="preserve">
<value>{} items</value>
</data>
<data name="paper" xml:space="preserve">
<value>Paper</value>
</data>
<data name="pen" xml:space="preserve">
<value>Pen</value>
</data>
</root>
17 changes: 17 additions & 0 deletions src/SmartFormat/Extensions/LocalizationFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Collections.Generic;
using System.Globalization;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Output;
using SmartFormat.Core.Parsing;
using SmartFormat.Pooling.SmartPools;
using SmartFormat.Utilities;

namespace SmartFormat.Extensions;
Expand Down Expand Up @@ -68,6 +70,21 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
// Get the localized string
var localized = LocalizationProvider!.GetString(formattingInfo.Format!.RawText, cultureInfo);

// Try formatting if localized string was not found, but a format has nested items
if (localized is null && formattingInfo.Format!.HasNested)
{
using var zsOutput = new ZStringOutput(ZStringBuilderExtensions.CalcCapacity(formattingInfo.Format));

var localizableFormatDetails = FormatDetailsPool.Instance.Get().Initialize(_formatter!,
formattingInfo.Format, InitializationObject.ObjectList, null, zsOutput);
var localizableFormattingInfo = FormattingInfoPool.Instance.Get().Initialize(localizableFormatDetails,
formattingInfo.Format, formattingInfo.CurrentValue);

_formatter!.Format(localizableFormattingInfo);

localized = LocalizationProvider!.GetString(zsOutput.ToString(), cultureInfo);
}

if (localized is null) throw new LocalizationFormattingException(formattingInfo.Format, $"No localized string found for '{formattingInfo.Format!.RawText}'", formattingInfo.Format.StartIndex);

// Use an existing Format from the cache
Expand Down