So, I created a simple example to demonstrate the error.
Link: https://drive.google.com/drive/folders/1w9M4Y4QfFCFdleUKMVpBu85GJKA7APYs?usp=drive_link
There is a build and source code (re-import packages before using). I also shared two videos. The first video demonstrates how it works in the editor, the second video shows errors in the browser.
Code:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
using UnityEngine.UIElements;
[RequireComponent(typeof(UIDocument))]
public class UI : MonoBehaviour
{
private const string c_Key = "ex";
private const string c_Table = "UITable";
private VisualElement m_PrevButton;
private VisualElement m_NextButton;
private Label m_LocalizedText;
private int m_CurrentLocaleIndex;
private IList<Locale> m_Locales;
private StringTable m_StringTable;
private LocalizedStringTable m_LocalizedStringTable = new LocalizedStringTable { TableReference = c_Table };
private void Awake()
{
var doc = GetComponent<UIDocument>();
var uxml = doc.rootVisualElement;
m_PrevButton = uxml.Q<VisualElement>(name: "PrevButton");
m_NextButton = uxml.Q<VisualElement>(name: "NextButton");
m_LocalizedText = uxml.Q<Label>(name: "LocalizedText");
m_PrevButton.RegisterCallback<ClickEvent>(_ => PreviousLocale());
m_NextButton.RegisterCallback<ClickEvent>(_ => NextLocale());
}
private IEnumerator Start()
{
yield return LocalizationSettings.InitializationOperation;
Debug.Log("LocalizationSettings.InitializationOperation: " + LocalizationSettings.InitializationOperation.Status);
m_Locales = LocalizationSettings.Instance.GetAvailableLocales().Locales;
SetLocale(1);
LocalizationSettings.SelectedLocaleChanged += DebugInfo;
var op = m_LocalizedStringTable.GetTableAsync();
yield return op.Task;
m_LocalizedStringTable.TableChanged += OnStringTableChanged;
DebugInfo();
}
private async void OnStringTableChanged(StringTable table)
{
await LocalizationSettings.InitializationOperation.Task;
await m_LocalizedStringTable.GetTableAsync().Task;
var entry = table.GetEntry(c_Key);
Text = entry.GetLocalizedString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void NextLocale()
{
SetLocale(m_CurrentLocaleIndex < m_Locales.Count - 1 ? m_CurrentLocaleIndex + 1 : 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void PreviousLocale()
{
SetLocale(m_CurrentLocaleIndex > 0 ? m_CurrentLocaleIndex - 1 : m_Locales.Count - 1);
}
private string Text
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => m_LocalizedText.text = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetLocale(int index)
{
m_CurrentLocaleIndex = index;
LocalizationSettings.SelectedLocale = m_Locales[index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DebugInfo()
{
DebugInfo(LocalizationSettings.SelectedLocale);
Debug.Log("Available locales: " + string.Join(", ", m_Locales));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DebugInfo(Locale locale)
{
Debug.Log("Current Locale: " + locale.Identifier);
}
}
Errors in Browser Console: