Skip to content

Localization

realonepiecefreak edited this page Oct 5, 2024 · 6 revisions

The framework natively supports localization via LocalizedString and the ILocalizer.

LocalizedString

Every text-based property is a LocalizedString, which represents either a fixed text or a localization ID.
LocalizedString implicitly converts to string. strings implicitly convert to a LocalizedString representing a fixed text.

Usage

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.");

Properties

  • IsEmpty: Determines that the LocalizedString has no fixed text or localization information set.

Methods

  • ToString: Resolves the localization ID and its optional arguments, returns the fixed text, or returns string.Empty otherwise; Equivalent to implicit conversion to string
  • Equals: Determines that the representation of a localized or fixed text matches another

ILocalizer

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.

Usage

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);

Properties

  • CurrentLocale: Returns the currently selected locale to resolve LocalizedString with

Methods

  • 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; Returns false otherwise
  • Localize: Returns the localized string by current or default locale and localization ID; Returns an undefined string when neither the current nor default locale support it
Clone this wiki locally