67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using Studio.Command;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Localization;
|
|
using UnityEngine.Localization.Components;
|
|
using UnityEngine.Localization.Settings;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
namespace Studio
|
|
{
|
|
public class CommandLocalizationHelper : LocalizeStringEvent
|
|
{
|
|
protected override void OnEnable()
|
|
{
|
|
if (!Application.isPlaying)
|
|
return;
|
|
|
|
base.OnEnable();
|
|
}
|
|
|
|
public void Localization(string id, Action<string> onCompleted)
|
|
{
|
|
var localizedString = new LocalizedString()
|
|
{
|
|
TableReference = "Command",
|
|
TableEntryReference = id
|
|
};
|
|
|
|
AsyncOperationHandle<string> stringOperation = localizedString.GetLocalizedStringAsync();
|
|
stringOperation.Completed += (op) =>
|
|
{
|
|
if (op.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
onCompleted?.Invoke(op.Result);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Not exist in table");
|
|
onCompleted?.Invoke(id);
|
|
}
|
|
};
|
|
}
|
|
|
|
public async Task<List<string>> GetLocalizedAllLocales(string id)
|
|
{
|
|
var result = new List<string>();
|
|
var locales = LocalizationSettings.AvailableLocales.Locales;
|
|
|
|
foreach (var locale in locales)
|
|
{
|
|
var handle = LocalizationSettings.StringDatabase.GetTableEntryAsync("Command", id, locale);
|
|
await handle.Task;
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result.Entry != null)
|
|
{
|
|
result.Add(handle.Result.Entry.GetLocalizedString());
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|