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).
Console.WriteLine(text.ToString());
> Yes
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.
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.
var lt = new LocalText("Dialogs.NoButton");
string text = lt;
> No
LocalText.Get Static Method
To access the translation for a local text key without creating a LocalText instance, use Get method:
> 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:
var translation = LocalText.TryGet("Looking.For.This.Key") ?? "Default Text";
> 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.