111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using BITKit.HttpNet;
|
|
using Cysharp.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using RotaryHeart.Lib.SerializableDictionary;
|
|
using System;
|
|
using UnityEngine.UIElements;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
namespace BITKit
|
|
{
|
|
public class GoogleSheetTranslator : MonoBehaviour, IAction
|
|
{
|
|
[SerializeReference, SubclassSelector]
|
|
public References url;
|
|
[SerializeReference, SubclassSelector]
|
|
public IWebProvider webProvider;
|
|
public SerializableDictionaryBase<string, TranslateSO> dictionary;
|
|
public async void Execute()
|
|
{
|
|
var csv = await webProvider.GetAsync(url);
|
|
|
|
var lines = csv.Split("\n").ToList();
|
|
var e = lines.GetEnumerator();
|
|
e.MoveNext();
|
|
var langs = e.Current.Split(",").Skip(1).Where(x => x is not "\"\"");
|
|
foreach (var lang in langs)
|
|
{
|
|
var _langs = lang;
|
|
_langs = _langs.Substring(1);
|
|
_langs = _langs.Substring(0, _langs.Length - 1);
|
|
_langs = _langs.Replace("#", string.Empty);
|
|
Debug.Log($"已获取语言{_langs}");
|
|
}
|
|
while (e.MoveNext())
|
|
{
|
|
var words = e.Current
|
|
.Split(",", langs.Count() + 1)
|
|
.Take(langs.Count())
|
|
.ToArray();
|
|
var source = words[0].Substring(1);
|
|
source = source.Substring(0, source.Length - 1);
|
|
|
|
words = e.Current.Split(",", langs.Count() + 1).Skip(1).ToArray();
|
|
//Debug.Log($"已获取到语言:{string.Join(",", words)}");
|
|
int index = 0;
|
|
foreach (var word in words)
|
|
{
|
|
if (word is "\"\"")
|
|
{
|
|
continue;
|
|
}
|
|
string currentLangs = langs
|
|
.ElementAt(index++)
|
|
.Replace("#", string.Empty)
|
|
.Substring(1);
|
|
currentLangs = currentLangs.Substring(0, currentLangs.Length - 1);
|
|
if (dictionary.TryGetValue(currentLangs, out var so))
|
|
{
|
|
string s1 = "\"";
|
|
//结束字符串
|
|
string s2 = "\"";
|
|
Regex rg = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
|
|
string _word = rg.Match(word).Value;
|
|
|
|
so.dictionary.Insert(source, _word);
|
|
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorUtility.SetDirty(so);
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void GetDifference()
|
|
{
|
|
BITAppForUnity.ThrowIfNotPlaying();
|
|
|
|
var sources = DI.Get<ITranslator>().GetAllSourceText();
|
|
var result = MathE.Subtract(sources.Distinct(), dictionary.First().Value.dictionary.Keys.Distinct());
|
|
var key = string.Join("\n", result);
|
|
Debug.Log($"已获取到未翻译文本数量:{result.Count()}");
|
|
Debug.Log(key);
|
|
}
|
|
}
|
|
#if UNITY_EDITOR
|
|
[UnityEditor.CustomEditor(typeof(GoogleSheetTranslator))]
|
|
public class GoogleSheetTranslatorInspector : BITInspector<GoogleSheetTranslator>
|
|
{
|
|
public override VisualElement CreateInspectorGUI()
|
|
{
|
|
FillDefaultInspector(root, serializedObject, true);
|
|
|
|
var excuteButton = root.Create<Button>();
|
|
excuteButton.text = "更新本地化语言";
|
|
excuteButton.clicked += agent.Execute;
|
|
|
|
var contrastButton = root.Create<Button>();
|
|
contrastButton.text = "获取未翻译文本";
|
|
contrastButton.clicked += agent.GetDifference;
|
|
|
|
|
|
return root;
|
|
}
|
|
}
|
|
#endif
|
|
} |