Alphabet localization
Localization can be supplied to multiple alphabet variations. Culture string can indicate alphabet type in the middle part of the string "language[-alphabet][-region]". [Demo]
Resources/sr/Localization.Example.l.yaml
TemplateFormat: Brace
Serbian:
- Culture: sr
Items:
- Key: Localization.Example.HelloWorld
Items:
- Text: "Zdravo Svete"
- Text: "Здраво Свете"
Culture: sr-Cyrl
If the preferred default alphabet is cyrillic, then the special case texts should be supplied to "-Latn" culture.
TemplateFormat: Brace
Serbian:
- Culture: sr
Items:
- Key: Localization.Example.HelloWorld
Items:
- Text: "Здраво Свете"
- Text: "Zdravo Svete"
Culture: sr-Latn
Text localizes to the requested alphabet type.
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create localizable text for "HelloWorld"
ILocalizableText hello = localization.LocalizableTextCached["Localization.Example.HelloWorld"];
// Print for "en" which fallbacks to invariant ""
WriteLine(hello.Print(CultureInfo.GetCultureInfo("en"), null)); // "Hello World"
// Print for "sr"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr"), null)); // "Zdravo Svete"
// Print for "sr-Latn-RS" which fallbacks to "sr"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr-Latn-RS"), null)); // "Zdravo Svete"
// Print for "sr-Cyrl-RS" which fallbacks to "sr-Cyrl"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr-Cyrl-RS"), null)); // "Здраво Свете"
Take a look at the culture fallback path. For example "sr-Cyrl-RS" the fallbacks are "sr-Cyrl", "sr" and then "".
// Get fallback cultures for "sr-Cyrl-RS"
string[] fallbackCultures = FallbackCultureProvider.Default["sr-Cyrl-RS"];
// Print
WriteLine($"\"{String.Join("\" → \"", fallbackCultures)}\""); // "sr-Cyrl-RS" → "sr-Cyrl" → "sr" → ""
Similarly "sr-Latn-RS" the fallbacks are "sr-Latn", "sr" and then "".
// Get fallback cultures for "sr-Latn-RS"
string[] fallbackCultures = FallbackCultureProvider.Default["sr-Latn-RS"];
// Print
WriteLine($"\"{String.Join("\" → \"", fallbackCultures)}\""); // "sr-Latn-RS" → "sr-Latn" → "sr" → ""
The culture, where fallback process lands to, can be read from .Culture property.
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create localizable text for "HelloWorld"
ILocalizableText hello = localization.LocalizableTextCached["Localization.Example.HelloWorld"];
// Localize to cyrillic
ILocalizedText hello_cyrl = hello.Localize("sr-Cyrl-RS")?.Value!;
// Get the culture where fallback landed to
string hello_cyrl_culture = hello_cyrl.Culture;
// Print culture
WriteLine(hello_cyrl_culture); // "sr-Cyrl"
Resources/Localization.Example.l.yaml
TemplateFormat: Brace
Invariant:
- Culture: ""
Items:
- Key: Localization.Example.HelloWorld
Text: "Hello World"
Full Example
Full example
using System.Globalization;
using Avalanche.Localization;
using static System.Console;
class alphabet
{
public static void Run()
{
{
// <01>
// Get fallback cultures for "sr-Cyrl-RS"
string[] fallbackCultures = FallbackCultureProvider.Default["sr-Cyrl-RS"];
// Print
WriteLine($"\"{String.Join("\" → \"", fallbackCultures)}\""); // "sr-Cyrl-RS" → "sr-Cyrl" → "sr" → ""
// </01>
}
{
// <02>
// Get fallback cultures for "sr-Latn-RS"
string[] fallbackCultures = FallbackCultureProvider.Default["sr-Latn-RS"];
// Print
WriteLine($"\"{String.Join("\" → \"", fallbackCultures)}\""); // "sr-Latn-RS" → "sr-Latn" → "sr" → ""
// </02>
}
{
// <03>
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create localizable text for "HelloWorld"
ILocalizableText hello = localization.LocalizableTextCached["Localization.Example.HelloWorld"];
// Print for "en" which fallbacks to invariant ""
WriteLine(hello.Print(CultureInfo.GetCultureInfo("en"), null)); // "Hello World"
// Print for "sr"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr"), null)); // "Zdravo Svete"
// Print for "sr-Latn-RS" which fallbacks to "sr"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr-Latn-RS"), null)); // "Zdravo Svete"
// Print for "sr-Cyrl-RS" which fallbacks to "sr-Cyrl"
WriteLine(hello.Print(CultureInfo.GetCultureInfo("sr-Cyrl-RS"), null)); // "Здраво Свете"
// </03>
}
{
// <04>
// Create localization
ILocalization localization = Localization.CreateDefault();
// Create localizable text for "HelloWorld"
ILocalizableText hello = localization.LocalizableTextCached["Localization.Example.HelloWorld"];
// Localize to cyrillic
ILocalizedText hello_cyrl = hello.Localize("sr-Cyrl-RS")?.Value!;
// Get the culture where fallback landed to
string hello_cyrl_culture = hello_cyrl.Culture;
// Print culture
WriteLine(hello_cyrl_culture); // "sr-Cyrl"
// </04>
}
}
}