更改了文件结构
This commit is contained in:
86
Src/Unity/Scripts/UX/Translator/UXTranslator.cs
Normal file
86
Src/Unity/Scripts/UX/Translator/UXTranslator.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Linq;
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class UXTranslator : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct Element<T> where T : VisualElement
|
||||
{
|
||||
public string source;
|
||||
public T element;
|
||||
}
|
||||
public UIDocument document;
|
||||
public List<Element<Label>> labels = new();
|
||||
public List<Element<Button>> buttons = new();
|
||||
ITranslator translator;
|
||||
void Start()
|
||||
{
|
||||
var root = document.rootVisualElement;
|
||||
|
||||
labels = root.Query<Label>()
|
||||
.ToList()
|
||||
.Where(x => x.text.Contains("#"))
|
||||
.Select(x => new Element<Label>()
|
||||
{
|
||||
source = x.text,
|
||||
element = x
|
||||
}).ToList();
|
||||
|
||||
buttons = root.Query<Button>()
|
||||
.ToList()
|
||||
.Where(x => x.text.Contains("#"))
|
||||
.Select(x => new Element<Button>()
|
||||
{
|
||||
source = x.text,
|
||||
element = x,
|
||||
}).ToList();
|
||||
|
||||
|
||||
Translate();
|
||||
|
||||
|
||||
|
||||
translator = DI.Get<ITranslator>();
|
||||
|
||||
translator.OnChangeLanguage += Translate;
|
||||
translator.OnCollageAllSourceText += GetAllSourceText;
|
||||
}
|
||||
public void Translate(string langs)
|
||||
{
|
||||
Translate();
|
||||
}
|
||||
[ContextMenu(nameof(Translate))]
|
||||
public void Translate()
|
||||
{
|
||||
var translator = DI.Get<ITranslator>();
|
||||
string newText;
|
||||
foreach (var element in labels)
|
||||
{
|
||||
newText = translator.Translate(element.source);
|
||||
element.element.text = newText;
|
||||
}
|
||||
foreach (var element in buttons)
|
||||
{
|
||||
newText = translator.Translate(element.source);
|
||||
element.element.text = newText;
|
||||
}
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
translator.OnChangeLanguage -= Translate;
|
||||
translator.OnCollageAllSourceText -= GetAllSourceText;
|
||||
}
|
||||
string[] GetAllSourceText()
|
||||
{
|
||||
List<string> list = new();
|
||||
|
||||
list.AddRange(labels.Select(x => x.source));
|
||||
list.AddRange(buttons.Select(x => x.source));
|
||||
return list.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
11
Src/Unity/Scripts/UX/Translator/UXTranslator.cs.meta
Normal file
11
Src/Unity/Scripts/UX/Translator/UXTranslator.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d61884491028dd4389470f1dc31d3bc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user