UI Toolbox logoUI Toolbox
Support

Localization

The toolbox controls show some texts on their own, for example OK, Cancel, Next, Today, None and "No results". ToolboxText (UIToolbox) holds them. Each text has a key, such as ToolboxText.CANCEL ("tb.cancel"), and an English default in ToolboxText.Defaults.

Three overlapping windows, a world map, quests and a chat, with the quests in frontThree overlapping windows, a world map, quests and a chat, with the quests in front

Member Use
Lookup A Func<string, string?> that returns the translation of a key. Null or an empty string gives the English default.
Refresh() Call it after a language change. The controls on a panel read their texts again, and the controls that format numbers and dates use CultureInfo.CurrentCulture again.
Get(key) The translation, else the English default, else the key.
Track(element, apply) Runs apply now and after each Refresh while the element is on a panel. Use it for your own texts.
SetText(element, key), SetTooltip(element, key) Track for one text or tooltip.

The window chrome buttons have the tooltips ToolboxText.MINIMIZE, MAXIMIZE and CLOSE. When a click restores the window, the minimize or maximize button has the tooltip ToolboxText.RESTORE. The tab close button has the tooltip ToolboxText.CLOSE_TAB. A button with only an icon or a glyph has a tooltip from a key too, such as OPEN on the arrow of a ComboBox, INCREASE and DECREASE on the buttons of a NumericUpDown, MORE_OPTIONS on the arrow of a SplitButton and REMOVE on the "×" of a chip. The chrome sets these tooltips, so a tooltip in a custom chrome template does not stay.

Call Lookup, Refresh and Get on the main thread. Set the culture yourself: Refresh does not change CultureInfo.CurrentCulture.

ToolboxText.Lookup = key =>
    myTranslations.TryGetValue(key, out var text) ? text : null;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
ToolboxText.Refresh();

The status texts of a SearchDropdown are the keys ToolboxText.NO_RESULTS, ToolboxText.SEARCHING and ToolboxText.SEARCH_FAILED. Read them with ToolboxText.Get, and translate them like any other key.

Shipped translations

The package ships the translations of every ToolboxText key. They are JSON files in Assets/UIToolbox/Runtime/Resources/UIToolbox/Strings, one per language, so the controls speak nine languages without the examples: English (en-US), French (fr-FR), German (de-DE), Spanish (es-ES), Japanese (ja-JP), Chinese (zh-CN), Korean (ko-KR), Portuguese (pt-BR) and Russian (ru-RU). ToolboxLocales lists them, from the file Resources/UIToolbox/locales.json. That file is the one list of languages: the string files, the examples, the documentation build and the tools in Tools/i18n-scan all read it.

ToolboxStrings.Install();          // the system language, else English
ToolboxStrings.Select("de-DE");    // a language change at run time

ToolboxStrings.Install sets ToolboxText.Lookup to read the shipped files. Select sets CultureInfo.CurrentCulture and CurrentUICulture, and calls ToolboxText.Refresh(). A code such as fr-CA picks fr-FR, the first language with the same two letters. A game with its own texts sets ToolboxText.Lookup itself and calls ToolboxStrings.Get(key) for the keys that it does not translate.

Member Use
ToolboxLocales.All The languages of locales.json: code, English name, native name, script, font and documentation folder.
ToolboxLocales.Match(culture) The language that fits a culture, by code and then by the two letters.
ToolboxStrings.Table(code) The texts of one language by key, or null when it has no file.
ToolboxStrings.Missing An event with the language and the key, raised once when a language lacks a key that a control asked for. The control shows the English default.
ToolboxStrings.MissingKeys(table) The keys of ToolboxText.Defaults that a table lacks.
ToolboxStrings.MissingReport() One line for each language that lacks keys, for a log.
ToolboxStrings.Missing += (locale, key) =>
    Debug.LogWarning($"{locale} has no text for {key}");

Plural rules and number formats

PluralRules holds the plural rules of the Unicode CLDR for the nine languages, and for Italian, Polish, Czech and Arabic. PluralRules.Category(culture, number) gives One, Few, Many, Other (and Zero and Two for Arabic). Portuguese reads 0 and 1 as one, like French. A language that the class does not know follows English. RelativeTimeText and the Localization example use this class. The form {0:plural:one|few|many} has two forms (one|other), three forms (one|few|many, as in Russian) or six forms (zero|one|two|few|many|other, as in Arabic).

ToolboxCulture.SpaceBeforePercent(culture) tells whether a language writes a space before the percent sign ("50 %" in German and French). A StatDelta uses it.

Test your screens with a pseudo language

PseudoLocale.Convert(text) makes a made-up language from an English text: accented letters, about 40 percent longer, in brackets. "Cancel" becomes [Ĉàñçéľ ~~], and {0} and tags stay as they are. German and Russian texts are 30 to 40 percent longer than English, so a text that clips in the pseudo language will clip in them too. A text that stays plain English in the pseudo language has no key, so no translation can reach it.

ToolboxText.Lookup = PseudoLocale.Lookup;
ToolboxText.Refresh();

Add a language

To add a language, for example Italian (it-IT):

  1. Run node Tools/i18n-scan/add-locale.mjs it-IT --name Italian --native "Italiano (Italia)". It adds the language to locales.json, and makes it-IT.json in the runtime folder (the texts of the controls) and in Examples/Resources/UIToolboxSamples/Strings (the texts of the examples) as copies of the English files, with their .meta files.
  2. Translate the values of both files. Keep the keys and the {0} placeholders. A text with a plural form keeps the form: {0:plural:{} articolo|{} articoli}.
  3. Run node Tools/i18n-scan/check-locales.mjs. It lists a missing file, a missing, unknown or empty key, and how many texts still equal the English text. check-locales.mjs --list it-IT lists them.
  4. Run node Tools/i18n-scan/scan.mjs --check. It checks that each example text is in the table and that the placeholders of each translation match the English text.
  5. Add the plural rule to PluralRules when the language is not one of the thirteen above and does not follow the English rule.
  6. A language with a script that the default font lacks needs a font. Pass --script cjk --font NotoSansXX to add-locale.mjs, add the font asset to the fallback list of the text settings, and run Tools/subset-cjk-fonts.mjs for the example fonts.
  7. The documentation is optional. Pass --docs it, add the language to LANGUAGES in Tools/docs/languages.mjs (the page texts and the group names), and translate the pages in Documentation/translations/it. Without --docs the documentation tools skip the language.

The EditMode tests check that each language of locales.json has a runtime file with each key of ToolboxText.Defaults.

The Localization example

The Localization example (Assets/UIToolbox/Examples/Localization) adds the texts of the examples to the shipped translations. Its JSON string files, one per language, are in Examples/Resources/UIToolboxSamples/Strings; SampleLocalization reads them together with the files of ToolboxStrings. It needs no package, so it works in each Unity 6 version. The file en-US.json is the English text of each key and the fallback. A file <locale>.json has one text per key:

{ "tb.next": "Suivant", "tb.back": "Précédent" }

The example loads English and the picked language only, and keeps what it has loaded. A locale whose file is missing is not in the language list, and nothing reports an error. A text that a language lacks shows English and is reported once through ToolboxStrings.Missing.

The example has one window. Its Preview card shows a part of a game for each tab: Shop (a DataGrid with C2 prices, a plural cart line and a MessageBox), Dates (a MonthCalendar and a DatePicker) and Setup (a Wizard with the toolbox texts Next, Back, Cancel and Finish). The language dropdown under the preview applies to every tab. The window title and the tab titles come from the table too. Each tab shows the code that makes its part: the entries of a Strings file, the UXML and the C#.

To use the Unity Localization package (com.unity.localization) instead, set ToolboxText.Lookup to a function that reads your string table, and call ToolboxText.Refresh() in the SelectedLocaleChanged event.

An open MessageBox keeps its message in the old language after a locale change. Only its buttons change.

Chinese, Japanese and Korean

ToolboxText takes any string, so translations in Chinese, Japanese and Korean (CJK) need no special code. The text needs a font that has its characters.

When no font of the panel has a character, Unity uses a font of the operating system. This OS font fallback works on desktop platforms only. It does not work on WebGL and on some consoles: there, the text shows boxes. Chinese, Japanese and Korean draw many of the same Han characters in different shapes. The OS fallback takes the first OS font that has the glyph, so the shapes can be wrong for the language.

For a shipped game, add a full CJK font asset for each language. Use one of these methods:

.localization.loc-lang-ja {
    -unity-font-definition: url("Fonts/NotoSansJP-Subset.ttf");
}
var language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
foreach (var code in new[] { "ja", "zh", "ko" })
    root.EnableInClassList("loc-lang-" + code, code == language);

Some texts are not in the root, for example the list of a DropdownField, popups and tooltips. The example Panel Settings, which all example scenes and the demo menu share, use Localization Text Settings. These text settings have the emoji of Emoji Text Settings and the three fonts of the example as fallbacks, so these texts also show the characters. A fallback font loads its glyphs only when a text needs them.

The example fonts are Noto Sans JP, Noto Sans SC and Noto Sans KR (SIL Open Font License 1.1, in Fonts/OFL.txt). They are subsets: they have only the characters of the example, so the package stays small. The script Tools/subset-cjk-fonts.mjs made them. Do not use the subsets in your game, because they do not have the characters of your texts. Use the full fonts.