Search Unity

GetLocalizedString in EditorWindow

Discussion in 'Localization Tools' started by BrettHuang, Mar 20, 2021.

  1. BrettHuang

    BrettHuang

    Joined:
    Oct 21, 2020
    Posts:
    18
    Hello,

    I am trying to get the localized string and show it in the Editor Window.
    Here is what I did in my test project:

    1. Create Localization String Table like this:
    One Table, One Entry, Two locales, nothing special.
    upload_2021-3-20_16-22-34.png

    2. Following this document:
    upload_2021-3-20_16-22-55.png

    I got this result. (Bravo!)
    upload_2021-3-20_16-23-4.png

    3. I tried to show the same text in EditorWindow.
    So I used a simple ScriptableObject to hold the LocalizedString
    upload_2021-3-20_16-23-14.png

    I use the same code for EditorWindow:
    Code (CSharp):
    1. public class LocalizedStringEditorExample : EditorWindow
    2. {
    3.     private LocalizedStringSO testSO;
    4.    
    5.     [MenuItem ("Window/My Window")]
    6.     public static void  ShowWindow () {
    7.         EditorWindow.GetWindow(typeof(LocalizedStringEditorExample));
    8.     }
    9.  
    10.     private void OnEnable()
    11.     {
    12.         testSO = AssetDatabase.LoadAssetAtPath<LocalizedStringSO>("Assets/Scripts/LocalizedStringSOTest.asset");
    13.     }
    14.  
    15.     void OnGUI()
    16.     {
    17.         GUILayout.Label("TestLabel");
    18.  
    19.         var localizedText = testSO.labelString.GetLocalizedString();
    20.         if (localizedText.IsDone)
    21.         {
    22.             GUILayout.Label(localizedText.Result);
    23.             Debug.Log(localizedText.Result);
    24.         }
    25.     }
    26. }
    But it failed.
    upload_2021-3-20_16-21-43.png

    Did I do something wrong?
    How can I get the string for EditorWindow?
    Thank you!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    We don't have support for getting a string the same way when not running in play mode, that's coming in 0.11.0.
    However if you want to get values in an Editor script then we have an Editor API for this under LocalizationEditorSettings.
    You can get the StringTableCollection, then the table and finally the entry using this.
     
    Last edited: Mar 20, 2021
  3. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    245
    For reference, I use this extension method (GetLocalizedStringImmediateSafe) to take care of the checks and always use it from anywhere. It's not been thoroughly tested, but seems to work fine, though note that it assumes the table has been already loaded if using it at runtime/play mode:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Localization;
    6. using UnityEngine.Localization.Tables;
    7.  
    8. #if UNITY_EDITOR
    9. using UnityEditor;
    10. using UnityEditor.Localization;
    11. #endif
    12.  
    13. public static class LocalizationUtils
    14. {
    15.     #if UNITY_EDITOR
    16.  
    17.     /// <summary>
    18.     /// (Editor only)
    19.     /// Gets a locale to use in edit mode in the editor.
    20.     /// </summary>
    21.     /// <param name="tableCollection">Optional table collection with which to filter the available locales.</param>
    22.     /// <returns>The locale, null if none usable found.</returns>
    23.  
    24.     static Locale Editor_GetValidLocaleInEditMode(LocalizationTableCollection tableCollection)
    25.     {
    26.         foreach (var locale in LocalizationEditorSettings.GetLocales())
    27.         {
    28.             if (locale != null  &&  (tableCollection == null  ||  tableCollection.GetTable(locale.Identifier) != null))
    29.                 return locale;
    30.         }
    31.  
    32.         return null;
    33.     }
    34.  
    35.     #endif
    36.  
    37.     /// <summary>
    38.     /// Gets the localized string from an already loaded table, taking into account whether we are in edit mode, play mode, or a build.
    39.     /// </summary>
    40.     /// <param name="localizedStringReference">The <see cref="LocalizedString"/>.</param>
    41.     /// <returns>The localized string.</returns>
    42.  
    43.     public static string GetLocalizedStringImmediateSafe(this LocalizedString localizedStringReference)
    44.     {
    45.         // If we are in the editor in edit mode, we need to find a valid locale and get the localized string from it:
    46.         #if UNITY_EDITOR
    47.         {
    48.             if (!EditorApplication.isPlaying)
    49.             {
    50.                 string text = null;
    51.                 if (!localizedStringReference.IsEmpty)
    52.                 {
    53.                     var tableCollection = LocalizationEditorSettings.GetStringTableCollection(localizedStringReference.TableReference);
    54.                     Locale locale = Editor_GetValidLocaleInEditMode(tableCollection);
    55.                     if (locale != null)
    56.                     {
    57.                         StringTable table = (StringTable)tableCollection.GetTable(locale.Identifier);
    58.                         if (table != null)
    59.                             text = table.GetEntryFromReference(localizedStringReference.TableEntryReference).LocalizedValue;
    60.                     }
    61.                 }
    62.  
    63.                 return text;
    64.             }
    65.         }
    66.         #endif
    67.  
    68.         // At runtime (build or editor in play mode), we just get the localized string normally:
    69.         return localizedStringReference.GetLocalizedString().Result;
    70.     }
    71. }
     
    karl_jones likes this.
  4. PlaycorpStudios

    PlaycorpStudios

    Joined:
    Aug 2, 2016
    Posts:
    48
    Hi Karl,

    Is this now supported in the latest version of Localizaiton package?

    Can I get the LocalizedString same way as I get it on runtime?

    I'm getting this error at the moment:

    Code (CSharp):
    1. System.Exception: SelectedLocale is null
    2. UnityEngine.Localization.LocalizedString:GetLocalizedString ()
    [Update]

    I have noticed if I set the LocalizaitonSceneControl window to English locale then it works fine. But that doesn't save anywhere, so i need to keep doing it everytime Unity gets opened.
     
    Last edited: Jul 21, 2021
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Yes its for Editor preview mode. We only store it in SessionState so it will be reset when you close Unity.
     
    PlaycorpStudios likes this.
  6. Liderangel

    Liderangel

    Joined:
    Jul 8, 2018
    Posts:
    101

    Any way on forcing this to always start with a language selected?
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    You could set it with an editor script. Maybe using the initialize on start attribute.