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.
| 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. 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();
SearchDropdown no longer has the NO_RESULTS_TEXT, SEARCHING_TEXT and SEARCH_FAILED_TEXT constants. Use ToolboxText.NO_RESULTS, ToolboxText.SEARCHING and ToolboxText.SEARCH_FAILED with ToolboxText.Get.
The Localization example
The Localization example (Assets/UIToolbox/Examples/Localization) connects ToolboxText to a JSON string table, Strings.json. It needs no package, so it works in each Unity 6 version. Strings.json has a list of locale codes and, for each key, one text per locale:
{ "locales": ["en-US", "fr-FR"], "entries": [ { "key": "tb.next", "values": ["Next", "Suivant"] } ] }
SampleLocalization.Install(table)reads the table, setsToolboxText.Lookupand selects the locale of the system language, or the first locale.SampleLocalization.Select(index)orSelect("fr-FR")setsCultureInfo.CurrentCultureandCurrentUICultureto the locale and callsToolboxText.Refresh().SampleLocalization.Format(key, args)formats a text in the current culture. The form{0:plural:one item|{} items}picks the first text for 1 and the second text for other numbers.{}is the number.SampleLocalization.Uninstall()goes back to English and to the culture from beforeInstall.- The table has a row for each key in
ToolboxText.Defaults, in en-US, fr-FR, de-DE and es-ES. LocalizationControllersets the texts of the example from the same table after each change, and fills the languageDropdownField.
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.