Skip to content

Commit

Permalink
Added support for nesting formats in LocalizationFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sitko, Michał committed Aug 29, 2023
1 parent 2bb777c commit 1e43acf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
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>
8 changes: 8 additions & 0 deletions src/SmartFormat/Extensions/LocalizationFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ 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)
{
var formatted = _formatter!.Format(formattingInfo.Format!.RawText, formattingInfo.CurrentValue);

localized = LocalizationProvider!.GetString(formatted, 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

0 comments on commit 1e43acf

Please sign in to comment.