Localization

Most widgets have localization support, exceptions are:

  • AutocompleteString

  • ComboboxString

  • ListViewString

Integration with custom localization system can done with UIWidgets.l10n.Localization class.

Example for the I2 Localization:

protected virtual void Start()
{
   Localization.GetTranslation = I2Translation;
   Localization.GetCountryCode = I2CountryCode; // used by Calendar and similar widgets
   I2.Loc.LocalizationManager.OnLocalizeEvent += Localization.LocaleChanged;
}

public static string I2Translation(string input)
{
   var result = I2.Loc.LocalizationManager.GetTranslation(input);
   if (result == null)
   {
      return input;
   }

   return result;
}

public static string I2CountryCode()
{
   return I2.Loc.LocalizationManager.CurrentLanguageCode;
}

Dialog, Popup Localization

Dialog and Popup widgets requires enabled LocalizationSupport in DialogInfoBase component.

Formatted strings can be used with the SetInfo method:

public void Dialog()
{
   var actions = new DialogButton[]
   {
      new DialogButton("OK", DialogClose),
      new DialogButton("Cancel", DialogClose),
   };

   var instance = DialogTemplate.Clone();
   instance.DialogInfo.LocalizationSupport = true;
   instance.Show(
      buttons: actions,
      focusButton: "Close",
      modal: false,
      onCancel: DialogClose);
   instance.SetInfo("Welcome, {0}", new object[] { "username", }, "Value 1: {0}\nValue 2: {1}", new object[] { "argument 1", "argument 2" });
}

bool DialogClose(int buttonIndex)
{
   return true;
}

Notify Localization

Notify widget requires enabled LocalizationSupport in NotifyInfoBase component.

Formatted strings can be used with the SetMessage method:

public void NotificationFormatted()
{
   var instance = NotificationTemplate.Clone();

   instance.NotifyInfo.LocalizationSupport = true;
   instance.Show(customHideDelay: 0f);
   instance.SetMessage("Welcome, {0} {1}", "FirstName", "LastName");
}

Generated Widgets

The easiest way to add localization support is to implement property returning a localized string in the data class. Widgets are automatically updated on locale changes.

public class Item
{
   public string LocalizedName
   {
      get
      {
         return I2.Loc.LocalizationManager.GetTranslation(Name);
      }
   }

   public string Name;
}