JJBB/Assets/Editor/Scripts/Multilingual/Multilingual_ComplexFont.cs
2024-08-23 15:49:34 +08:00

1074 lines
41 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using System.IO;
public class Multilingual_ComplexFont : MonoBehaviour
{
/// <summary>
/// 语言信息
/// </summary>
public class GualInfo
{
/// <summary>
/// 中文文本
/// </summary>
public string ChineseText = "";
/// <summary>
/// 翻译文本
/// </summary>
public string TranslateText = "-1";
public string ToLine()
{
return string.Format("{0}\t{1}", ChineseText, TranslateText);
}
}
/// <summary>
/// 所有文本路径
/// </summary>
private static string AllTEXTPATH = Application.dataPath.Replace("Assets", "Multilingual\\");
/// <summary>
/// 预设文本路径
/// </summary>
private static string PREFABPATH = Application.dataPath+ "\\Project3D\\BundleData\\UI\\Prefab";
/// <summary>
/// 表格文本路径
/// </summary>
private static string TABFILEPATH = Application.dataPath + "\\Project3D\\BundleData\\Tables3D";
/// <summary>
/// LUA表格文本路径
/// </summary>
private static string TABLUAFILEPATH = Application.dataPath + "\\Project\\Script\\LuaScripts\\GameTables\\Lua";
/// <summary>
/// 还原所有UI文本
/// </summary>
[MenuItem("ProTool/Multilingual/繁体/还原/ReturnAllUIText 还原到汉语简体")]
public static void ReturnAllUIText1()
{
SetAllUIText("繁体//", 0);
}
/// <summary>
/// 还原所有表格文本
/// </summary>
[MenuItem("ProTool/Multilingual/繁体/还原/ReturnAllTabText 还原到汉语简体")]
public static void ReturnAllTabText1()
{
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
//所有表格文本路径
string allTabtextPath = AllTEXTPATH + "//AllTabText.txt";
//获取文件信息
FileInfo fi = new FileInfo(allTabtextPath);
//信息字典
Dictionary<string, List<string>> fiLineDics = new Dictionary<string, List<string>>();
//信息存在
if (fi.Exists)
{
//使用Unicode编码从字节流中读取文件
StreamReader sw1 = new StreamReader(allTabtextPath, System.Text.Encoding.Unicode);
//读取文件
string line = sw1.ReadLine();
//不为空循环
while (line!=null)
{
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;//结束本次循环开始下次循环
string[] lines = line.Split('\t');
if (lines.Length < 2)
continue;
//存入字典
fiLineDics[lines[0]] = new List<string>(lines);
}
//关闭读取
sw1.Close();
}
else
{
//显示一个模态对话框。
EditorUtility.DisplayDialog("缺少文件", string.Format("找不到 {0} 文件", allTabtextPath), "yes");
return;
}
//创建目录文件夹
DirectoryInfo directoryInfo = Directory.CreateDirectory(TABFILEPATH);
//获取以 .txt 后缀文件目录信息
FileInfo[] fileInfos = directoryInfo.GetFiles("*.txt", SearchOption.AllDirectories);
for (int n = 0; n < fileInfos.Length; n++)
{
//是否包含 StrDictionary 文件
if (fileInfos[n].Name.Contains("StrDictionary"))
continue;
//使用utf8编码从字节流中读取文件
StreamReader sw1 = new StreamReader(fileInfos[n].FullName, utf8WithoutBom);
//流 列表
List<string> lines = new List<string>();
//读取
string line = sw1.ReadLine();
//存入列表
lines.Add(line);
//不是null
while (line!=null)
{
//读取
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] strs = line.Split('\t');
if (strs.Length < 2)
continue;
///存入列表
lines.Add(line);
}
//关闭
sw1.Close();
if (lines.Count <= 2)
continue;
//分离
string[] column = lines[0].Split('\t');
for (int i = 2; i < lines.Count; i++)
{
//不是空 不包含 "#"和"\t"
if (!string.IsNullOrEmpty(lines[i]) && !lines[i][0].Equals('#') && !lines[i][0].Equals('\t'))
{
//分离
string[] segments = lines[i].Split('\t');
if (segments.Length != column.Length)
{
Debug.Log("error : " + lines[i]);
return;
}
for (int index = 2; index < column.Length; index++)
{
//新格式字符串 名字 列 段
string key = string.Format("{0}#{1}#{2}", fileInfos[n].Name, column[index], segments[0]);
// fiLineDics字典包含 key
if (fiLineDics.ContainsKey(key))
{
//取值
segments[index] = fiLineDics[key][1];
//连接
lines[i] = string.Join("\t", segments);
}
}
//显示进度条 标题 信息 进度
EditorUtility.DisplayProgressBar("Find All Text", string.Format("Files : {0}/{1} {2}", n, fileInfos.Length, fileInfos[n].FullName), n * 1.0f / fileInfos.Length);
}
}
// utf8编码格式写入 如果存在则改写 不存在侧创建
StreamWriter sw = new StreamWriter(fileInfos[n].FullName, false, utf8WithoutBom);
for (int i = 0; i < lines.Count; i++)
{
//输出
sw.WriteLine(lines[i]);
}
//关闭
sw.Close();
}
//关闭进度条
EditorUtility.ClearProgressBar();
//将所有未保存的资产更改写入磁盘
AssetDatabase.SaveAssets();
}
#region //翻译
/// <summary>
/// 翻译所有UI文本
/// </summary>
[MenuItem("ProTool/Multilingual/繁体/翻译/Set/SetAllUIText")]
public static void SetAllUIText1()
{
SetAllUIText("繁体//",1);
Debug.Log("SetAllUIText success!");
}
/// <summary>
/// 翻译所有表格文本
/// </summary>
[MenuItem("ProTool/Multilingual/繁体/翻译/Set/SetAllTabText")]
public static void SetAllTabText1()
{
SetAllTabText("繁体", TABFILEPATH, "AllTabText.txt");
SetAllLuaTabText("繁体", TABLUAFILEPATH, "AllTabText.txt");
Debug.Log("SetAllTabText success!");
}
/// <summary>
/// 翻译StrDictionaty
/// </summary>
[MenuItem("ProTool/Multilingual/繁体/翻译/Set/SetStrDictionatyText")]
public static void SetStrTabText1()
{
SetStrTabText("繁体", TABFILEPATH);
Debug.Log("SetStrDictionatyText success!");
}
[MenuItem("ProTool/Multilingual/繁体/翻译/Get/GetAllUIText")]
public static void AllUIText1()
{
GetAllUIText("繁体");
}
[MenuItem("ProTool/Multilingual/繁体/翻译/Get/GetAllTabText")]
public static void AllTabText1()
{
GetAllTabText("繁体", TABFILEPATH, "AllTabText.txt");
}
#endregion
/// <summary>
/// 修剪
/// </summary>
/// <param name="ChinaText"></param>
/// <returns></returns>
public static string TrimAll(string ChinaText)
{
ChinaText = ChinaText.TrimEnd('"');
ChinaText = ChinaText.TrimStart('"');
return ChinaText;
}
/// <summary>
/// 替换中文符号
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string ReplaceMaoHao(string text)
{
text = text.Replace("", "");
text = text.Replace("。", ".");
text = text.Replace(":", "");
return text.Replace("", "");
}
/// <summary>
/// AllUIText 本地上次获取的所有UIPrefab上的文本信息 NewAllUIText 需要进行翻译的文本的信息
/// 该函数先对比 AllUIText NewAllUIText两个文件把NewAllUIText中有而AllUIText中没有或者NewAllUIText
/// 和AllUIText不一样的部分合并到AllUIText中并清空NewAllUIText文件。然后读取所有UIPrefab上的文本信息并和AllUIText中的信息对比
/// 把有变化的或者不存在的保存到NewAllUIText中完成后NewAllUIText中的文本就是最新需要翻译的文本
/// </summary>
/// <param name="country"></param>
public static void GetAllUIText(string country)
{
//所有ui文本
string allUItextPath = AllTEXTPATH + string.Format("//{0}//AllUIText.txt", country);
//新的未翻译的ui文本
string newallUItextPath = AllTEXTPATH + string.Format("//{0}//TranslateAllUIText.txt", country);
//1对比两个文件
FileInfo fi = new FileInfo(allUItextPath);
FileInfo fiNew = new FileInfo(newallUItextPath);
if(fi.Exists != fiNew.Exists )
{
EditorUtility.DisplayDialog("123", "有问题 AllUIText TranslateAllUIText 要么都存在要么都不存在检查一下SVN的记录是不是本地的被删掉了", "123");
return;
}
//字典
Dictionary<string,GualInfo> fiLineDics = new Dictionary<string, GualInfo>();
if (fiNew.Exists)
{
//以Unicode编码读取文件
StreamReader sw1 = new StreamReader(allUItextPath, System.Text.Encoding.Unicode);
StreamReader swnew1 = new StreamReader(newallUItextPath, System.Text.Encoding.Unicode);
int index = 0;
string line = sw1.ReadLine();
while(line!=null)
{
index++;
//读取一行
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
{
Debug.Log(index+" null "+line);
continue;
}
string[] lines = line.Split('\t');
if (lines.Length < 2)
{
Debug.Log(index + " <2 " + line);
continue;
}
Debug.Log("sdafsafdsaf : "+line);
GualInfo gualInfo = new GualInfo();
//删除无用
gualInfo.ChineseText = TrimAll(lines[0]);
gualInfo.TranslateText = lines[1];
//替换符号
string chinaText = ReplaceMaoHao(gualInfo.ChineseText);
fiLineDics[chinaText] = gualInfo;
}
line = swnew1.ReadLine();
while (line!=null)
{
line = swnew1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] lines = line.Split('\t');
if (lines.Length < 2)
continue;
GualInfo gualInfo = new GualInfo();
//删除
gualInfo.ChineseText = TrimAll(lines[0]);
//替换
string chinaText = ReplaceMaoHao(gualInfo.ChineseText);
gualInfo.TranslateText = lines[1];
if (fiLineDics.ContainsKey(chinaText) == false || gualInfo.TranslateText != "-1")
{
fiLineDics[chinaText] = gualInfo;
}
}
//关闭
sw1.Close();
swnew1.Close();
}
//获取目录信息
DirectoryInfo directoryInfo = Directory.CreateDirectory(PREFABPATH);
//文件信息
FileInfo[] fileInfos = directoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < fileInfos.Length; i++)
{
//替换
string assetPath = fileInfos[i].FullName.Replace("\\", "/");
assetPath = assetPath.Replace(Application.dataPath, "Assets");
//加载对应路径的资产
UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(assetPath);
for (int j = 0; j < objs.Length; j++)
{
if (objs[j] == null)
continue;
if (objs[j].GetType() != typeof(Text))
continue;
Text text = objs[j] as Text;
if (text == null)
continue;
string txtStr = text.text;
//是否有中文
if (Regex.IsMatch(txtStr, @"[\u4e00-\u9fa5]+") == false)
{
continue;
}
//替换
string LabelText = txtStr.Replace("\t", "#t").Replace("\r\n", "#r#n").Replace("\r", "#r").Replace("\n", "#n");
GualInfo gualInfo = new GualInfo();
LabelText = TrimAll(LabelText);//删除
string chinaText = ReplaceMaoHao(LabelText);//替换
if (fiLineDics.ContainsKey(chinaText) == false)
{
gualInfo.ChineseText = LabelText;
fiLineDics[chinaText] = gualInfo;
}
//对话框
EditorUtility.DisplayProgressBar("Find All Text", string.Format("Files : {0}/{1}", i, fileInfos.Length), i * 1.0f / fileInfos.Length);
}
}
if (!fi.Directory.Exists)
{
fi.Directory.Create();//创建
}
//读取
StreamWriter sw = new StreamWriter(allUItextPath,false, System.Text.Encoding.Unicode);
StreamWriter swnew = new StreamWriter(newallUItextPath,false, System.Text.Encoding.Unicode);
StreamWriter swOriginal = new StreamWriter(AllTEXTPATH + "//AllUIText.txt", false, System.Text.Encoding.Unicode);
//写出列名称
string linew = "中文" + "\t" + country;
//写入
sw.WriteLine(linew);
swnew.WriteLine(linew);
swOriginal.WriteLine(linew);
foreach (var gual in fiLineDics)
{
linew = gual.Value.ToLine();
//写入
if (gual.Value.TranslateText == "-1")
swnew.WriteLine(linew);
sw.WriteLine(linew);
swOriginal.WriteLine(linew);
}
//关闭
sw.Close();
swnew.Close();
swOriginal.Close();
EditorUtility.ClearProgressBar();
}
public static void GetAllTabText(string country,string tabsDictionary,string translatetabName)
{
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
string allUItextPath = AllTEXTPATH + string.Format("//{0}//{1}", country, translatetabName);
string newallUItextPath = AllTEXTPATH + string.Format("//{0}//Translate{1}", country, translatetabName);
//1对比两个文件
FileInfo fi = new FileInfo(allUItextPath);
FileInfo fiNew = new FileInfo(newallUItextPath);
if (fi.Exists != fiNew.Exists)
{
EditorUtility.DisplayDialog("123", "有问题 AllUIText NewAllUIText 要么都存在要么都不存在检查一下SVN的记录是不是本地的被删掉了", "123");
return;
}
Dictionary<string, GualInfo> fiLineDics = new Dictionary<string, GualInfo>();
if (fiNew.Exists)
{
StreamReader sw1 = new StreamReader(allUItextPath, System.Text.Encoding.Unicode);
StreamReader swnew1 = new StreamReader(newallUItextPath, System.Text.Encoding.Unicode);
string line = sw1.ReadLine();
while (line!=null)
{
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] lines = line.Split('\t');
if (lines.Length < 2)
continue;
GualInfo gualInfo = new GualInfo();
gualInfo.ChineseText = TrimAll(lines[0]);
string chinaText = ReplaceMaoHao(gualInfo.ChineseText);
gualInfo.TranslateText = lines[1];
fiLineDics[chinaText] = gualInfo;
}
line = swnew1.ReadLine();
while (line!=null)
{
line = swnew1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] lines = line.Split('\t');
if (lines.Length < 2)
continue;
GualInfo gualInfo = new GualInfo();
gualInfo.ChineseText = TrimAll(lines[0]);
string chinaText = ReplaceMaoHao(gualInfo.ChineseText);
gualInfo.TranslateText = lines[1];
if (fiLineDics.ContainsKey(chinaText) ==false || gualInfo.TranslateText != "-1")
{
fiLineDics[chinaText] = gualInfo;
}
}
sw1.Close();
swnew1.Close();
}
DirectoryInfo directoryInfo = Directory.CreateDirectory(tabsDictionary);
FileInfo[] fileInfos = directoryInfo.GetFiles("*.txt", SearchOption.AllDirectories);
for (int n = 0; n < fileInfos.Length; n++)
{
if (fileInfos[n].Name.Contains("StrDictionary"))
continue;
if (fileInfos[n].Name.Contains("StrFilter"))
continue;
if (fileInfos[n].Name.Contains("RoleName"))
continue;
if (fileInfos[n].Name.Contains("QuestionBank"))
continue;
StreamReader sw1 = new StreamReader(fileInfos[n].FullName, utf8WithoutBom);
List<string> lines = new List<string>();
string line = sw1.ReadLine();
lines.Add(line);
while (line!=null)
{
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] strs = line.Split('\t');
if (strs.Length < 2)
continue;
lines.Add(line);
}
sw1.Close();
if (lines.Count < 2)
continue;
string[] column = lines[0].Split('\t');
for (int i = 2; i < lines.Count; i++)
{
if (!string.IsNullOrEmpty(lines[i]) && !lines[i][0].Equals('#') && !lines[i][0].Equals('\t'))
{
string[] segments = lines[i].Split('\t');
if (segments.Length != column.Length)
{
Debug.Log("error : " + lines[i]);
return;
}
for (int index = 2; index < column.Length; index++)
{
if (Regex.IsMatch(segments[index], @"[\u4e00-\u9fa5]+") == false)
{
continue;
}
string ChineText = segments[index];
ChineText = TrimAll(ChineText);
string chinaText = ReplaceMaoHao(ChineText);
if (fileInfos[n].Name.Contains("StrFilter") || fileInfos[n].Name.Contains("RoleName"))
{
if(fiLineDics.ContainsKey(chinaText))
fiLineDics.Remove(chinaText);
}
else if (fiLineDics.ContainsKey(chinaText) == false)
{
GualInfo gualInfo = new GualInfo();
gualInfo.ChineseText = ChineText;
gualInfo.TranslateText = "-1";
fiLineDics[chinaText] = gualInfo;
}
}
EditorUtility.DisplayProgressBar("Find All Text", string.Format("Files : {0}/{1} {2}", n, fileInfos.Length, fileInfos[n].FullName), n * 1.0f / fileInfos.Length);
}
}
}
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
StreamWriter sw = new StreamWriter(allUItextPath,false, System.Text.Encoding.Unicode);
StreamWriter swnew = new StreamWriter(newallUItextPath,false, System.Text.Encoding.Unicode);
StreamWriter swOriginal = new StreamWriter(AllTEXTPATH + "//" + translatetabName, false, System.Text.Encoding.Unicode);
//写出列名称
string linew = "中文" + "\t" + country;
sw.WriteLine(linew);
swnew.WriteLine(linew);
swOriginal.WriteLine(linew);
foreach (var gual in fiLineDics)
{
linew = gual.Value.ToLine();
if (gual.Value.TranslateText == "-1")
swnew.WriteLine(linew);
sw.WriteLine(linew);
swOriginal.WriteLine(linew);
}
sw.Close();
swnew.Close();
swOriginal.Close();
EditorUtility.ClearProgressBar();
}
/// <summary>
/// 翻译或还原所有UI文本
/// </summary>
/// <param name="country">国家</param>
/// <param name="Opera">0还原 1翻译</param>
public static void SetAllUIText(string country,int Opera) //Opera 0还原 1翻译
{
//所有UI文本路径
string allUItextPath = AllTEXTPATH + string.Format("{0}AllUIText.txt", country);
//文件信息
FileInfo fi = new FileInfo(allUItextPath);
//文件信息路线字典
Dictionary<string, GualInfo> fiLineDics = new Dictionary<string, GualInfo>();
//文件信息翻译路线字典
Dictionary<string, GualInfo> fiLineDicsTrasn = new Dictionary<string, GualInfo>();
if (fi.Exists)
{
//使其以一种特定的编码从字节流中读取字符。
StreamReader sw1 = new StreamReader(allUItextPath, System.Text.Encoding.Unicode);
string line = "5432";
while (line!=null)
{
//读取路线
line = sw1.ReadLine();
//为Null或空
if (string.IsNullOrEmpty(line))
continue;
//分割
string[] lines = line.Split('\t');
if (lines.Length < 2 || string.IsNullOrEmpty(lines[1]) || lines[1] == "-1")
continue;
GualInfo gual = new GualInfo();
//修剪
gual.ChineseText = TrimAll(lines[0]);
//替换冒号
gual.ChineseText = ReplaceMaoHao(gual.ChineseText);
gual.TranslateText = TrimAll(lines[1]);
//存入字典
fiLineDics[gual.ChineseText] = gual;
fiLineDicsTrasn[lines[1]] = gual;
//Debug.Log(string.Format("key words:string:{0}", gual.ChineseText));
}
//关闭
sw1.Close();
}
else
{
//显示一个模态对话框。
EditorUtility.DisplayDialog("123", string.Format("找不到 {0} 文件", allUItextPath), "123");
return;
}
string[] listFiles = { "PetMainWnd.prefab", "OtherPetInfo.prefab", "PetSkillsInfo.prefab", "MeridiaSoul.prefab" };
//读取目录信息
DirectoryInfo directoryInfo = Directory.CreateDirectory(PREFABPATH);
//获取信息中 .prefab 后缀文件
FileInfo[] fileInfos = directoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories);
//目录信息长度
int fileCount = fileInfos.Length;
for (int i = 0; i < fileInfos.Length; i++)
{
//替换
string fullPath = fileInfos[i].FullName.Replace("\\", "/");
//if (listFiles.Contain(fileInfos[i].Name) == false)
{
// continue;
}
Debug.Log(fileInfos[i].Name);
//替换
string asstePath = fullPath.Replace(Application.dataPath.Replace("\\","/"), "Assets");
//通过路径加载资源
GameObject _prefab = AssetDatabase.LoadAssetAtPath(asstePath, typeof(GameObject)) as GameObject;
if (_prefab == null)
{
Debug.Log(string.Format("找不到 {0} 文件", asstePath));
continue;
}
//实例化预设
GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;
//获取预设文本
Text[] objs = prefabGameobject.GetComponentsInChildren<Text>(true);
for (int j = 0; j < objs.Length; j++)
{
if (objs[j] == null)
continue;
string txtStr = objs[j].text;
//翻译 且 不包含中文
if (Opera == 1 && Regex.IsMatch(txtStr, @"[\u4e00-\u9fa5]+") == false)
{
continue;
}
//替换
string LabelText = txtStr.Replace("\t", "#t").Replace("\r\n", "#r#n").Replace("\r", "#r").Replace("\n", "#n");
//Debug.Log(string.Format("find string:{0}", LabelText));
GualInfo gualInfo = null;
//修剪
LabelText = TrimAll(LabelText);
//替换符号
LabelText = ReplaceMaoHao(LabelText);
//是否存在
if (fiLineDics.ContainsKey(LabelText))
{
//Debug.Log(string.Format("has:string:{0}", LabelText));
gualInfo = fiLineDics[LabelText];
}
else if(fiLineDicsTrasn.ContainsKey(LabelText))
{
gualInfo = fiLineDicsTrasn[LabelText];
}
else
continue;
if(Opera == 1)
{
//替换
string text = gualInfo.TranslateText.Replace("#t", "\t").Replace("#r#n", "\r\n").Replace("#n", "\n").Replace("#r", "\r");
objs[j].text = text;
}
else
{
//替换
string text = gualInfo.ChineseText.Replace("#t", "\t").Replace("#r#n", "\r\n").Replace("#n", "\n").Replace("#r", "\r");
objs[j].text = text;
}
}
//进度条
EditorUtility.DisplayProgressBar("Set All Text", string.Format("prefabs : {0}/{1} {2}", i, fileCount, fileInfos[i].Name), i * 1.0f / fileCount);
//PrefabUtility.SaveAsPrefabAsset(prefabGameobject, asstePath);
//创建预制游戏对象后返回该对象。如果connectToPrefab启用prefabGameobject将成为创建的预制实例。
PrefabUtility.ReplacePrefab(prefabGameobject, _prefab, ReplacePrefabOptions.Default);
//立即销毁
MonoBehaviour.DestroyImmediate(prefabGameobject);
}
//关闭进度天
EditorUtility.ClearProgressBar();
//将所有未保存的资产更改写入磁盘
AssetDatabase.SaveAssets();
}
/// <summary>
/// 翻译所有lua表格
/// </summary>
/// <param name="country">国家</param>
/// <param name="tabsDictionary">表格字典</param>
/// <param name="translatetabName">翻译表格名字</param>
public static void SetAllLuaTabText(string country, string tabsDictionary, string translatetabName)
{
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
//所有表格文本路径
string allTabtextPath = AllTEXTPATH + string.Format("//{0}//{1}", country, translatetabName);
//文件信息
FileInfo fi = new FileInfo(allTabtextPath);
//字典
Dictionary<string, string> fiLineDics = new Dictionary<string, string>();
if (fi.Exists)
{
//使其以一种特定的编码从字节流中读取字符。
StreamReader sw1 = new StreamReader(allTabtextPath, System.Text.Encoding.Unicode);
string line = "1213";
while (line != null)
{
//读取
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] lines = line.Split('\t');
if (lines.Length < 2 || string.IsNullOrEmpty(lines[1]) || lines[1] == "-1" || lines[1] == "1")
continue;
//删除
string ChinaText = TrimAll(lines[0]);
//替换中文符号
ChinaText = ReplaceMaoHao(ChinaText);
//删除
fiLineDics[ChinaText] = TrimAll(lines[1]);
}
//关闭
sw1.Close();
}
else
{
//对话框
EditorUtility.DisplayDialog("123", string.Format("找不到 {0} 文件", allTabtextPath), "123");
return;
}
//读取目录信息
DirectoryInfo directoryInfo = Directory.CreateDirectory(tabsDictionary);
//文件信息
FileInfo[] allTabFiles = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
for (int i = 0; i < allTabFiles.Length; i++)
{
FileInfo fileInfo = allTabFiles[i];
//后缀 txt 或者 lua
if (fileInfo.Extension.Contains("txt") || fileInfo.Extension.Contains("lua"))
{
//读取所有
string[] lines = File.ReadAllLines(fileInfo.FullName);
List<string> writeLines = new List<string>(lines);
for (int j = 0; j < writeLines.Count; j++)
{
//解析每一行
writeLines[j] = ParseOneLine(writeLines[j], fiLineDics);
}
//覆盖写入
File.WriteAllLines(fileInfo.FullName, writeLines.ToArray());
}
}
}
/// <summary>
/// 解析
/// </summary>
/// <param name="line">行</param>
/// <param name="fiLineDics">对应字典</param>
/// <returns></returns>
private static string ParseOneLine(string line,Dictionary<string, string> fiLineDics)
{
//StartsWith确定此字符串实例的开头是否与指定的字符串匹配。
if (line.StartsWith("--") == false)
{
//切割后的孩子
List<string> Child1s = new List<string>(line.Split(','));
for (int i = 0; i < Child1s.Count; i++)
{
if (string.IsNullOrEmpty(Child1s[i]))
continue;
List<string> Child2s = new List<string>(Child1s[i].Split('='));
if (Child2s.Count != 2)
continue;
for (int j = 0; j < Child2s.Count; j++)
{
if (string.IsNullOrEmpty(Child2s[j]))
continue;
if (Child2s[j].Length < System.Text.Encoding.Default.GetBytes(Child2s[j]).Length)
{
//TrimStart()只删除字符串的头部的。TrimEnd()只删除字符串尾部的。
string child = Child2s[j].TrimStart(' ');
child = child.TrimEnd(' ');
child = child.TrimStart('\'');
child = child.TrimEnd('\'');
child = child.TrimEnd('"');
child = child.TrimStart('"');
if (Regex.IsMatch(child, @"[\u4e00-\u9fa5]+"))
{
child = ReplaceMaoHao(child);
if (fiLineDics.ContainsKey(child) && fiLineDics[child] != "-1")
child = fiLineDics[child];
}
Child2s[j] = string.Format("\'{0}\'", child);
}
}
Child1s[i] = string.Format("{0} = {1}", Child2s[0].TrimEnd(' '), Child2s[1].TrimStart(' '));
}
line = string.Join(",", Child1s.ToArray());
}
return line;
}
/// <summary>
/// 翻译所有前端表格
/// </summary>
/// <param name="country">国家</param>
/// <param name="tabsDictionary">表格字典</param>
/// <param name="translatetabName">翻译表格名称</param>
public static void SetAllTabText(string country,string tabsDictionary,string translatetabName)
{
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
//所有表格路径
string allTabtextPath = AllTEXTPATH + string.Format("//{0}//{1}",country, translatetabName);
//文件信息
FileInfo fi = new FileInfo(allTabtextPath);
//文件信息字典
Dictionary<string, string> fiLineDics = new Dictionary<string,string>();
if (fi.Exists)
{
//使其以一种特定的编码从字节流中读取字符。
StreamReader sw1 = new StreamReader(allTabtextPath, System.Text.Encoding.Unicode);
string line = "1213";
while (line!=null)
{
//读取
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] lines = line.Split('\t');
if (lines.Length < 2 || string.IsNullOrEmpty(lines[1]) || lines[1] == "-1" || lines[1] == "1")
continue;
//剪切
string ChinaText = TrimAll(lines[0]);
//替换中文符号
ChinaText = ReplaceMaoHao(ChinaText);
//剪切
fiLineDics[ChinaText] = TrimAll(lines[1]);
}
//关闭
sw1.Close();
}
else
{
//对话框
EditorUtility.DisplayDialog("123", string.Format("找不到 {0} 文件", allTabtextPath), "123");
return;
}
//读取目录信息
DirectoryInfo directoryInfo = Directory.CreateDirectory(tabsDictionary);
//文件信息
FileInfo[] fileInfos = directoryInfo.GetFiles("*.txt", SearchOption.AllDirectories);
for (int n = 0; n < fileInfos.Length; n++)
{
if (fileInfos[n].Name.Contains("StrDictionary"))
continue;
if (fileInfos[n].Name.Contains("StrFilter"))
continue;
if (fileInfos[n].Name.Contains("RoleName"))
continue;
if (fileInfos[n].Name.Contains("QuestionBank"))
continue;
//使其以一种特定的编码从字节流中读取字符。
StreamReader sw1 = new StreamReader(fileInfos[n].FullName, utf8WithoutBom);
List<string> lines = new List<string>();
//读取
string line = sw1.ReadLine();
lines.Add(line);
while (line!=null)
{
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] strs = line.Split('\t');
if (strs.Length < 2)
continue;
lines.Add(line);
}
sw1.Close();
if (lines.Count <= 2)
continue;
string[] column = lines[0].Split('\t');
for (int i = 2; i < lines.Count; i++)
{
if (!string.IsNullOrEmpty(lines[i]) && !lines[i][0].Equals('#') && !lines[i][0].Equals('\t'))
{
string[] segments = lines[i].Split('\t');
if (segments.Length != column.Length)
{
Debug.Log("error : "+ fileInfos[n].Name +" " +i+" "+ lines[i]);
return;
}
for (int index = 2; index < column.Length; index++)
{
// "[\u4e00-\u9fa5]+ == 中文
if (Regex.IsMatch(segments[index], @"[\u4e00-\u9fa5]+") == false)
{
continue;
}
string ChineText = segments[index];
//删除
ChineText = TrimAll(ChineText);
//替换中文符号
ChineText = ReplaceMaoHao(ChineText);
if (fiLineDics.ContainsKey(ChineText) && fiLineDics[ChineText]!="-1")
{
segments[index] = fiLineDics[ChineText];
}
}
//连接
lines[i] = string.Join("\t", segments);
//进度条
EditorUtility.DisplayProgressBar("Find All Text", string.Format("Files : {0}/{1} {2}", n, fileInfos.Length, fileInfos[n].FullName), n * 1.0f / fileInfos.Length);
}
}
StreamWriter sw = new StreamWriter(fileInfos[n].FullName,false, utf8WithoutBom);
for(int i=0;i<lines.Count;i++)
{
//输出
sw.WriteLine(lines[i]);
}
//关闭
sw.Close();
}
//关闭进度条
EditorUtility.ClearProgressBar();
}
/// <summary>
/// 字典表文本
/// </summary>
/// <param name="country">国家</param>
/// <param name="tabsDictionary">表字典</param>
public static void SetStrTabText(string country, string tabsDictionary)
{
//可忽略ID
List<string> IgnorID = new List<string>() {"2626","2627","2628","5990","5991","5992","5993", "46267" };
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
//路径格式
string allTabtextPath = AllTEXTPATH + string.Format("//{0}//{1}", country, "StrDictionatyID.txt");
//文件信息
FileInfo fi = new FileInfo(allTabtextPath);
//字典
Dictionary<string, List<string>> fiLineDics = new Dictionary<string, List<string>>();
if (fi.Exists)
{
//使用Unicode编码从字节流中读取文件
StreamReader sw2 = new StreamReader(allTabtextPath, System.Text.Encoding.Unicode);
//读取一行
string line1 = sw2.ReadLine();
while (line1 != null)
{
line1 = sw2.ReadLine();
if (string.IsNullOrEmpty(line1))
continue;
string[] lines1 = line1.Split('\t');
if (lines1.Length < 2)
{
Debug.Log("0 line len less 2 : " + line1);
continue;
}
fiLineDics[lines1[0]] = new List<string>(lines1);
}
sw2.Close();
}
else
{
EditorUtility.DisplayDialog("123", string.Format("找不到 {0} 文件", allTabtextPath), "123");
return;
}
FileInfo fileInfo = new FileInfo(tabsDictionary + "/StrDictionary.txt");
//使用utf8编码从字节流中读取文件
StreamReader sw1 = new StreamReader(fileInfo.FullName, utf8WithoutBom);
List<string> lines = new List<string>();
//读取
string line = sw1.ReadLine();
lines.Add(line);
while (line != null)
{
line = sw1.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
string[] strs = line.Split('\t');
if (strs.Length < 2)
{
Debug.Log("1 line len less 2 : " + line);
continue;
}
lines.Add(line);
}
sw1.Close();
if (lines.Count <= 2)
return;
string[] column = lines[0].Split('\t');
for (int i = 2; i < lines.Count; i++)
{
if (!string.IsNullOrEmpty(lines[i]) && !lines[i][0].Equals('#') && !lines[i][0].Equals('\t'))
{
string[] segments = lines[i].Split('\t');
if (segments.Length != column.Length || IgnorID.Contains(segments[0]))
{
Debug.Log("error : " + fileInfo.Name + " " + i + " " + lines[i]);
continue;
}
if (fiLineDics.ContainsKey(segments[0]))
{
if(segments.Length > 2 && fiLineDics[segments[0]].Count > 2)
{
segments[2] = fiLineDics[segments[0]][2];
segments[2] = TrimAll(segments[2]);
segments[2] = segments[2];
lines[i] = string.Join("\t", segments);
}
}
}
}
StreamWriter sw = new StreamWriter(fileInfo.FullName, false, utf8WithoutBom);
for (int i = 0; i < lines.Count; i++)
{
sw.WriteLine(lines[i]);
}
sw.Close();
//关闭进度条
EditorUtility.ClearProgressBar();
}
[MenuItem("ProTool/Multilingual/繁体/PrintPrafabAllText")]
public static void PrintPrafabAllText()
{
Text[] texts = Selection.activeGameObject.GetComponentsInChildren<Text>(true);
for(int i=0;i<texts.Length;i++)
{
string text = texts[i].text;
text = text.Replace('\\', '#');
Debug.Log(text);
}
}
}