-
-
Notifications
You must be signed in to change notification settings - Fork 106
Localization _ LocalizationFormatter
axunonb edited this page Mar 25, 2022
·
4 revisions
The LocalizationFormatter localizes literals and placeholders.
{ :L ( lang ) : literal }
| Any value | :formatter-name | (lang) | literal |
|---|---|---|---|
| Any value | "L" | language code | the literal to localize |
{ Any value : L (lang) : { placeholder } }
| Any value | :formatter-name | (lang) | placeholder |
|---|---|---|---|
| Any value | "L" | language code | the nested placeholder |
ILocalizationProvider? LocalizationProvider: default isnull.
Can be set to the standardLocalizationProvider, which handlesresxresource files.
- The standard
LocalizationProvider, handlesresxresource files. A fallback culture can be set. It will be used, in case no item for a certain culture could be found in any of the resources.LocalizationProvidercan search an unlimited number of resoures. - All
IFormatters can also make use of localization, if needed. - Issues with localization throw a
LocalizationFormattingException, which is derived fromFormattingException.
The culture-specific results shown here are included in embedded resource files, which are omitted for brevity. Full source code: see LocalizationFormatterTests.
// "WeTranslateText" and its translations
// are entries in the resource file
// culture supplied as a format option
Smart.Format(culture, "{:L(es):WeTranslateText}");
// culture supplied as an argument to the formatter
var culture = CultureInfo.GetCultureInfo("es");
Smart.Format(culture, "{:L:WeTranslateText}");
// outputs for both: "Traducimos el texto"// "has {:#,#} inhabitants}" and its translations
// are entries in the resource file
Smart.Format("{0} {1:L(en):has {:#,#} inhabitants}", "X-City", 8900000);
// outputs: "X-City has 8,900,000 inhabitants"
Smart.Format("{0} {1:L(es):has {:#,#} inhabitants}", "X-City", 8900000);
// outputs: "X-City tiene 8.900.000 habitantes"By example of pluralization
// "{} item", "{} items" and its translations
// are entries in the resource file
Smart.Format("{0:plural:{:L(en):{} item}|{:L(en):{} items}}", 0);
// outputs for English: 0 items
Smart.Format("{0:plural:{:L(fr):{} item}|{:L(fr):{} items}}", 0);
// outputs for French: 0 élément
Smart.Format("{0:plural:{:L(fr):{} item}|{:L(fr):{} items}}", 200);
// outputs for French: 200 élémentsCreate your custom ILocalizationProvider implementation:
public class DictLocalizationProvider : ILocalizationProvider
{
private readonly Dictionary<string, Dictionary<string, string>> _translations;
public DictLocalizationProvider()
{
_translations = new Dictionary<string, Dictionary<string, string>> {
{ "en", new Dictionary<string, string> { { "COUNTRY", "country" } } },
{ "fr", new Dictionary<string, string> { { "COUNTRY", "pays" } } },
{ "es", new Dictionary<string, string> { { "COUNTRY", "país" } } }
};
}
public string? GetString(string name)
{
return GetTranslation(name, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
}
public string? GetString(string name, string cultureName)
{
return GetTranslation(name, cultureName);
}
public string? GetString(string name, CultureInfo cultureInfo)
{
return GetTranslation(name, cultureInfo.TwoLetterISOLanguageName);
}
private string? GetTranslation(string name, string cultureName)
{
if (!_translations.TryGetValue(cultureName, out var entry)) return null;
return entry.TryGetValue(name, out var localized) ? localized : null;
}
}Use it:
// Change settings for your provider
Smart.Default.Settings.Localization.LocalizationProvider = new DictLocalizationProvider();
// Add the formatter
Smart.Default.AddExtensions(new LocalizationFormatter());
Smart.Format("{:L(en):COUNTRY} * {:L(fr):COUNTRY} * {:L(es):COUNTRY}");
// Output: "country * pays * país"- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License
3.6