-
Notifications
You must be signed in to change notification settings - Fork 1
Localization
The framework natively supports localization via LocalizedString
and the ILocalizer
.
Every text-based property is a LocalizedString
, which represents either a fixed text or a localization ID.
LocalizedString
implicitly converts to string
. string
s implicitly convert to a LocalizedString
representing a fixed text.
To set a localized text to a component or property, that supports it:
// "Label.Caption1" => "This is a label."
var label = new Label(LocalizedString.FromId("Label.Caption1"));
To set a localized text with formattable arguments to a component or property, that supports it:
// "Label.Caption2" => "This is a label with number {0}."
var label = new Label(LocalizedString.FromId("Label.Caption2", () => 2));
To set a fixed text, meaning a text that won't get localized, to a component or property, that supports it:
var label1 = new Label(LocalizedString.FromText("This is also a label."));
// - or -
var label2 = new Label("This is also a label.");
-
IsEmpty
: Determines that theLocalizedString
has no fixed text or localization information set.
-
ToString
: Resolves the localization ID and its optional arguments, returns the fixed text, or returnsstring.Empty
otherwise; Equivalent to implicit conversion tostring
-
Equals
: Determines that the representation of a localized or fixed text matches another
LocalizedString
is resolved by an ILocalizer
, which has to be passed to the constructor of Application
.
If necessary, it can be referenced by calling Application.Instance.Localizer
.
The base class BaseLocalizer
implements ILocalizer
and only requires a deriving class to set some default properties and the intialization methods.
A minimally implemented localizer:
class MyOwnLocalizer : BaseLocalizer
{
protected override string DefaultLocale => "en";
protected override string UndefinedValue => "<undefined>";
protected override IList<LanguageInfo> InitializeLocalizations()
{
return new[] {
new LanguageInfo("en", "English", new Dictionary<string, string> { ["Caption"] = "Show Text" }),
new LanguageInfo("de", "Deutsch", new Dictionary<string, string> { ["Caption"] = "Zeige Text" }),
// Continue with more languages
}
}
protected override string InitializeLocale() => File.ReadAllText("stored_locale.txt");
protected override void SetCurrentLocale(string locale) => File.WriteAllText("stored_locale.txt", locale);
}
To set a localizer for the application:
var localizer = new MyOwnLocalizer();
var applciation = new Application(localizer);
-
CurrentLocale
: Returns the currently selected locale to resolveLocalizedString
with
-
GetLocales
: Returns a list of all loaded locales -
GetLanguageName
: Returns the natural name of a language by locale -
ChangeLocale
: Changes the current language by locale -
TryLocalize
: If a given localization ID is supported by the current or default locale and localizes it; Returnsfalse
otherwise -
Localize
: Returns the localized string by current or default locale and localization ID; Returns anundefined
string when neither the current nor default locale support it