Skip to content

Display localized text

Last updated

Get strings in the current display language from your registered source by key. When the language changes, listen for the event and redraw your UI.

Call Get(sourceId, key, fallback) to get a string. In your tool, a small helper method that passes your SourceId is convenient.

public static string Get(string key, string fallback = null)
{
return EditorLocalization.Service.Get(SampleToolLocalization.SourceId, key, fallback);
}

Get looks for the key in this order and returns the first match.

  1. The current language
  2. The source’s DefaultLanguageCode
  3. en-US
  4. The fallback argument, if given
  5. The key string itself

If the source is not registered or its translation folder cannot be found, Get also returns the key string.

When the language changes, the LanguageChanged event fires with the new language code. Subscribe only while your window is open, and redraw it when the event fires.

private void OnEnable()
{
EditorLocalization.Service.LanguageChanged += OnLanguageChanged;
}
private void OnDisable()
{
EditorLocalization.Service.LanguageChanged -= OnLanguageChanged;
}
private void OnLanguageChanged(string languageCode)
{
Repaint();
}

Log messages start with [AMARI-LOC]. Warnings for undefined keys are off by default.

Set the EditorPrefs key com.amari-noa.unity-editor-localization-core.log-missing-keys to true to log a warning whenever an undefined key is requested without a fallback.