GetLocalizedString in EditorWindow

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.

2. Following this document:

I got this result. (Bravo!)

3. I tried to show the same text in EditorWindow.
So I used a simple ScriptableObject to hold the LocalizedString

I use the same code for EditorWindow:

public class LocalizedStringEditorExample : EditorWindow
{
    private LocalizedStringSO testSO;
   
    [MenuItem ("Window/My Window")]
    public static void  ShowWindow () {
        EditorWindow.GetWindow(typeof(LocalizedStringEditorExample));
    }

    private void OnEnable()
    {
        testSO = AssetDatabase.LoadAssetAtPath<LocalizedStringSO>("Assets/Scripts/LocalizedStringSOTest.asset");
    }

    void OnGUI()
    {
        GUILayout.Label("TestLabel");

        var localizedText = testSO.labelString.GetLocalizedString();
        if (localizedText.IsDone)
        {
            GUILayout.Label(localizedText.Result);
            Debug.Log(localizedText.Result);
        }
    }
}

But it failed.
6956018--818792--upload_2021-3-20_16-21-43.png

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

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.

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:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Tables;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Localization;
#endif

public static class LocalizationUtils
{
    #if UNITY_EDITOR

    /// <summary>
    /// (Editor only)
    /// Gets a locale to use in edit mode in the editor.
    /// </summary>
    /// <param name="tableCollection">Optional table collection with which to filter the available locales.</param>
    /// <returns>The locale, null if none usable found.</returns>

    static Locale Editor_GetValidLocaleInEditMode(LocalizationTableCollection tableCollection)
    {
        foreach (var locale in LocalizationEditorSettings.GetLocales())
        {
            if (locale != null  &&  (tableCollection == null  ||  tableCollection.GetTable(locale.Identifier) != null))
                return locale;
        }

        return null;
    }

    #endif

    /// <summary>
    /// Gets the localized string from an already loaded table, taking into account whether we are in edit mode, play mode, or a build.
    /// </summary>
    /// <param name="localizedStringReference">The <see cref="LocalizedString"/>.</param>
    /// <returns>The localized string.</returns>

    public static string GetLocalizedStringImmediateSafe(this LocalizedString localizedStringReference)
    {
        // If we are in the editor in edit mode, we need to find a valid locale and get the localized string from it:
        #if UNITY_EDITOR
        {
            if (!EditorApplication.isPlaying)
            {
                string text = null;
                if (!localizedStringReference.IsEmpty)
                {
                    var tableCollection = LocalizationEditorSettings.GetStringTableCollection(localizedStringReference.TableReference);
                    Locale locale = Editor_GetValidLocaleInEditMode(tableCollection);
                    if (locale != null)
                    {
                        StringTable table = (StringTable)tableCollection.GetTable(locale.Identifier);
                        if (table != null)
                            text = table.GetEntryFromReference(localizedStringReference.TableEntryReference).LocalizedValue;
                    }
                }

                return text;
            }
        }
        #endif

        // At runtime (build or editor in play mode), we just get the localized string normally:
        return localizedStringReference.GetLocalizedString().Result;
    }
}
1 Like

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:

System.Exception: SelectedLocale is null
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.

Yes its for Editor preview mode. We only store it in SessionState so it will be reset when you close Unity.

1 Like

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

You could set it with an editor script. Maybe using the initialize on start attribute.