Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity Package UI Localization is available in 2020.2

Discussion in 'Editor Workflows' started by yuji_unity3d, Aug 24, 2020.

  1. yuji_unity3d

    yuji_unity3d

    Unity Technologies

    Joined:
    Oct 30, 2015
    Posts:
    2
    UI of your Package can be localized in 2020.2. This thread introduces how to do that.

    First of all, now we can have 4 languages for this feature, Japanese, Korean, Simplified Chinese and Traditional Chinese. You will need to install language module in the Editor via the Unity Hub beforehand.

    Step 1: Create a PO file as a dictionary.
    PO files are dictionary files defined in GNU gettext. The file names need to be as follows:
    • ja.po for Japanese
    • ko.po for Korean
    • zh_hans.po for Simplified Chinese
    • zh_hant.po for Traditional Chinese
    See GNU gettext for the file format. We'll prepare a sample ja.po file with the following contents.

    msgid ""
    msgstr ""
    "Language: ja\n"

    msgid "Yes"
    msgstr "TranslatedYes"

    Note that the first entry is an empty character. All you need to set the property is the language. The second entry contains a sample "Yes" translation. The file format must be saved in UTF-8.
    When a po file exists under Assets folder, the registration strings can be observed by the inspector.

    Step 2: Create an Assembly Definition File.
    The dictionaries need to be separated so that they don't interfere with other packages or with Unity's own dictionaries. Create an assembly definition file at the location that contains the po file. Typically you make Assembly Definition File for Runtime and Editor each. The po file should be placed at Editor one.

    Step 3: Set LocalizationAsset attribute to the assembly.
    Assets/Editor/Localization/Localization.cs for example would be good, whose content is:
    Code (CSharp):
    1. #if UNITY_2020_2_OR_NEWER
    2. [assembly: UnityEditor.Localization]
    3. #endif
    Step 4: Call L10n.Tr for the translation.

    L10n.Tr("Yes") returns "TranslatedYes" now if users change Language to Japanese at Preference Window. Please use it for the UI. This is the example:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class MyWindow : EditorWindow
    5. {
    6.     private static readonly string YesString = L10n.Tr("Yes");
    7.     [MenuItem("Window/My Custom Editor Window")]
    8.     static void Init()
    9.     {
    10.         var window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
    11.         window.Show();
    12.     }
    13.  
    14.     void OnGUI()
    15.     {
    16.         GUILayout.Label(YesString);
    17.     }
    18. }
    MyWindow.png

    Note:
    • L10n.Tr looks up a dictionary. You should call it outside of your update function like OnGUI.
    • Currently texts in UI Toolkit cannot be localized automatically.
     
    APSchmidt, SugoiDev, Mauri and 4 others like this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,149
    Are there built-in texts that we can use?
     
  3. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    187
    After loading a VisualElement hierarchy, you can use UQuery to go through all TextElements and apply the translation:
    Code (CSharp):
    1. rootVisualElement.Query<TextElement>().ForEach((e) => e.text = L10n.Tr(e.text));
     
    yuji_unity3d and JoNax97 like this.
  4. yuji_unity3d

    yuji_unity3d

    Unity Technologies

    Joined:
    Oct 30, 2015
    Posts:
    2
    If you don't define
    [assembly: UnityEditor.Localization]
    then the dictionary for the Unity Editor will be used. So you can expect simple expressions like "Yes" or "Open" to be translated. But we don't recommend it because you can't control the content of the dictionary. When you want to see the content, Localization/ja.po in the Unity installed folder is the one.
     
    Peter77 likes this.
unityunity