更改文件架构

This commit is contained in:
CortexCore
2023-06-07 18:38:07 +08:00
parent 93292b1a59
commit ed84166723
720 changed files with 297 additions and 65 deletions

View File

@@ -0,0 +1,22 @@
{
"name": "BITKit.Translator",
"rootNamespace": "",
"references": [
"GUID:a209c53514018594f9f482516f2a6781",
"GUID:66d2ae14764cc7d49aad4b16930747c0",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:be17a8778dbfe454890ed8279279e153",
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b9b77474d0790ea43b6bead6c07d306a
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,111 @@
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 Excute()
{
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.Excute;
var contrastButton = root.Create<Button>();
contrastButton.text = "获取未翻译文本";
contrastButton.clicked += agent.GetDifference;
return root;
}
}
#endif
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aa1a4a302a76aed46b76a508f57a2886
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RotaryHeart.Lib.SerializableDictionary;
using Newtonsoft.Json;
using System.Linq;
namespace BITKit
{
public class UnityTranslator : MonoBehaviour, ITranslator
{
public string langs = "zh-cns";
public SerializableDictionaryBase<string, TranslateSO> dictionary = new();
public event Action<string> OnChangeLanguage;
public event Func<string[]> OnCollageAllSourceText;
public string Translate(string rawString)
{
if (dictionary.TryGetValue(langs, out var so))
{
return so.Get(rawString, false);
}
return rawString;
}
public void Set(string value)
{
if (dictionary.ContainsKey(value))
{
langs = value;
BIT4Log.Log<UnityTranslator>($"已设置语言为:{value}");
OnChangeLanguage?.Invoke(value);
}
}
void Awake()
{
DI.Register<ITranslator>(this);
//GameObject.DontDestroyOnLoad(gameObject);
}
public string[] GetAllSourceText()
{
var value = OnCollageAllSourceText.GetInvocationList()
.SelectMany(x => x.Method.Invoke(x.Target, null) as string[])
.ToArray();
return value;
}
public void LogAllSourceText()
{
BITAppForUnity.ThrowIfNotPlaying();
var json = JsonConvert.SerializeObject(GetAllSourceText(),Formatting.Indented);
Debug.Log(json);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2d7876c854d62484f990e85fa65960b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: