Skip to content

Commit 6a395ec

Browse files
zacaterasSitko, Michał
and
Sitko, Michał
authored
Added support for nesting formats in LocalizationFormatter (#350)
* Added support for nested formats in LocalizationFormatter (without double parsing) --------- Co-authored-by: Sitko, Michał <[email protected]>
1 parent 2bb777c commit 6a395ec

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

src/SmartFormat.Tests/Extensions/LocalizationFormatterTests.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,17 @@ public void Combine_With_PluralLocalizationFormatter(string format, int count, s
204204
var actual = smart.Format(CultureInfo.GetCultureInfo(cultureName), format, count);
205205
Assert.That(actual, Is.EqualTo(expected));
206206
}
207-
}
207+
208+
[TestCase("{:L:{ProductType}}", "en", "paper", "Paper")]
209+
[TestCase("{:L:{ProductType}}", "de", "paper", "das Papier")]
210+
[TestCase("{:L:{ProductType}}", "fr", "paper", "Papier")]
211+
[TestCase("{:L:{ProductType}}", "en", "pen", "Pen")]
212+
[TestCase("{:L:{ProductType}}", "de", "pen", "der Kugelschreiber")]
213+
[TestCase("{:L:{ProductType}}", "fr", "pen", "Bic")]
214+
public void Combine_With_Nesting(string format, string cultureName, string productType, string expected)
215+
{
216+
var smart = GetFormatterWithRegisteredResource();
217+
var actual = smart.Format(CultureInfo.GetCultureInfo(cultureName), format, new { ProductType = productType });
218+
Assert.That(actual, Is.EqualTo(expected));
219+
}
220+
}

src/SmartFormat.Tests/Localization/LocTest1.de.resx

+6
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,10 @@
132132
<data name="{} items" xml:space="preserve">
133133
<value>{} Elemente</value>
134134
</data>
135+
<data name="paper" xml:space="preserve">
136+
<value>das Papier</value>
137+
</data>
138+
<data name="pen" xml:space="preserve">
139+
<value>der Kugelschreiber</value>
140+
</data>
135141
</root>

src/SmartFormat.Tests/Localization/LocTest1.es.resx

+6
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,10 @@
132132
<data name="{} items" xml:space="preserve">
133133
<value>{} elementos</value>
134134
</data>
135+
<data name="paper" xml:space="preserve">
136+
<value>Papel</value>
137+
</data>
138+
<data name="pen" xml:space="preserve">
139+
<value>Bolígrafo</value>
140+
</data>
135141
</root>

src/SmartFormat.Tests/Localization/LocTest1.fr.resx

+6
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,10 @@
132132
<data name="{} items" xml:space="preserve">
133133
<value>{} éléments</value>
134134
</data>
135+
<data name="paper" xml:space="preserve">
136+
<value>Papier</value>
137+
</data>
138+
<data name="pen" xml:space="preserve">
139+
<value>Bic</value>
140+
</data>
135141
</root>

src/SmartFormat.Tests/Localization/LocTest1.resx

+6
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@
136136
<data name="{} items" xml:space="preserve">
137137
<value>{} items</value>
138138
</data>
139+
<data name="paper" xml:space="preserve">
140+
<value>Paper</value>
141+
</data>
142+
<data name="pen" xml:space="preserve">
143+
<value>Pen</value>
144+
</data>
139145
</root>

src/SmartFormat/Extensions/LocalizationFormatter.cs

+17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Collections.Generic;
77
using System.Globalization;
88
using SmartFormat.Core.Extensions;
9+
using SmartFormat.Core.Output;
910
using SmartFormat.Core.Parsing;
11+
using SmartFormat.Pooling.SmartPools;
1012
using SmartFormat.Utilities;
1113

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

73+
// Try formatting if localized string was not found, but a format has nested items
74+
if (localized is null && formattingInfo.Format!.HasNested)
75+
{
76+
using var zsOutput = new ZStringOutput(ZStringBuilderExtensions.CalcCapacity(formattingInfo.Format));
77+
78+
var localizableFormatDetails = FormatDetailsPool.Instance.Get().Initialize(_formatter!,
79+
formattingInfo.Format, InitializationObject.ObjectList, null, zsOutput);
80+
var localizableFormattingInfo = FormattingInfoPool.Instance.Get().Initialize(localizableFormatDetails,
81+
formattingInfo.Format, formattingInfo.CurrentValue);
82+
83+
_formatter!.Format(localizableFormattingInfo);
84+
85+
localized = LocalizationProvider!.GetString(zsOutput.ToString(), cultureInfo);
86+
}
87+
7188
if (localized is null) throw new LocalizationFormattingException(formattingInfo.Format, $"No localized string found for '{formattingInfo.Format!.RawText}'", formattingInfo.Format.StartIndex);
7289

7390
// Use an existing Format from the cache

0 commit comments

Comments
 (0)