LocalText Class

    At the core of string localization is LocalText class.

    Its constructor takes a key parameter, which defines the local text key that it will contain. Some of sample keys are:

    • Enums.Month.December
    • Db.Northwind.Customer.CustomerName
    • Dialogs.YesButton

    At runtime, through ToString() function, the local text key is translated to its representation in the active language (which is CultureInfo.CurrentUICulture).

    1. Console.WriteLine(text.ToString());
    1. > Yes
    1. var text = new LocalText("Unknown.Local.Text.Key");

    Gets the local text key that LocalText instance contains.

    Implicit Conversions From String

    LocalText has implicit conversion from String type.

    1. LocalText someText = "Dialogs.YesButton";

    Here someText variable references a new LocalText instance with the key Dialogs.YesButton. So it is just a shortcut to LocalText constructor.

    1. var lt = new LocalText("Dialogs.NoButton");
    2. string text = lt;
    1. > No

    LocalText.Get Static Method

    To access the translation for a local text key without creating a LocalText instance, use Get method:

    1. > Yes

    Unlike Get method which returns the local text key if no translation is found, TryGet returns null. Thus, coalesce operator can be used along with TryGet where required:

    1. var translation = LocalText.TryGet("Looking.For.This.Key") ?? "Default Text";
    1. > Default Text

    LocalText.Empty Field

    Similar to String.Empty, LocalText contains an empty local text object with empty key.

    We will talk about language identifiers in the following section.