API reference
Locale
DevCore.UiEngine.Localization.Locale is a static class: one set of locales for every app and view. It does not depend on Unity. The guide chapter is Locales.
The current locale
| Member | Meaning |
|---|---|
static string Current | The locale's code. Reactive: assigning it updates every text and reader this frame |
static string Fallback | The locale a missing text comes from |
static bool IsRightToLeft | Whether the current locale is written right to left |
Available | The registered locales, as LocaleInfo: Code, Name, IsRightToLeft |
static bool Has(string code) | Whether a locale is registered |
static bool HasChoice | Whether a locale was chosen by code; when not, the system's language is used at start |
static event Action Changed | Raised when the current locale or its texts change |
using DevCore.UiEngine.Localization;
Locale.Current = "tr";
Locale.Changed += () => SaveLanguage(Locale.Current);
Reading texts
| Member | Meaning |
|---|---|
static string T(string key) | The text of a key. Missing: the fallback's text, then the key itself |
static string T(LocaleKey key) | The same by a prepared key: an array read |
static LocaleKey Key(string key) | Prepares a key once, for texts read often |
static string T(string key, int | long count) | The plural form for the count, with the count filled in |
static string T(string key, double number) | The plural form for a number |
static string T(string key, object arg0), (key, arg0, arg1), (key, params object[] args) | The text with its {0}, {1}… filled in, formatted with the locale's culture. A first argument that is a number picks the plural form |
static string Plural(string key, long count) | The plural form's text, not filled in. Allocates nothing |
static PluralCategory PluralOf(long count) | The form the current language uses for a count: Zero, One, Two, Few, Many, Other |
static bool HasText(string key) | Whether the current locale or the fallback has the text |
static readonly LocaleKey Play = Locale.Key("menu.play");
string a = Locale.T(Play); // no lookup by string
string b = Locale.T("hud.gold", gold); // "250 gold"
string c = Locale.T("items", count); // "1 item" or "5 items"
A text that asks for an argument you did not give, or has an open brace, is shown as it is instead of throwing.
Formats
| Member | Meaning |
|---|---|
static CultureInfo Culture | The .NET culture of the current locale's code, then of its language, else invariant. Reactive |
static NumberFormatInfo NumberFormat | Culture.NumberFormat |
static string Format(IFormattable value, string format = null) | Formats a number or a date for the locale: Locale.Format(1234.5, "N1") |
static void SetCulture(string code, CultureInfo culture) | Gives a locale another culture |
Adding and changing texts
Locale files are compiled and registered for you. These members are for texts that come from elsewhere: downloaded content, mods, tools.
| Member | Meaning |
|---|---|
static void Add(LocaleTable table) | Registers a locale, or merges into one that exists |
static void Add(string code, string name, bool rightToLeft, Func<LocaleTable> texts) | Registers a locale whose table is built on first use |
static void Replace(LocaleTable table) | Replaces a locale's texts |
static void SetText(string code, string key, string text) | Sets one text |
static bool Remove(string code), static void Clear() | Removes a locale; removes all |
static LocaleTable Table(string code) | The table of a locale |
LocaleTable has Set(key, text), With("key1", "text1", …), TryGet and Merge.
Plural rules
PluralRules.Select(locale, n) returns the plural category of a number in a language, and CategoriesOf(locale) the categories that language has. The rules are CLDR's for 216 languages and match the browser's Intl.PluralRules.