unity多语言本地化
简介
嗯...一般来说做游戏啥的都不会只发一个国家,但是每个国家语言不同,就存在多语言本地化的问题,然后直接用过一个通过xml完成本地化的东东,然后策划反馈不会修改xml,扔给我一个excel让我自己把字段填进去,然后我就自己写个csv的本地化工具....
PS:至于为啥用csv不用xls或者xlsx,因为!csv格式简单啊,自己就可以写解析器,excel的解析去又需要一大堆第三方库啥的
用法
先在Assets/Resources文件夹下创建一个LTLocalization文件夹,然后在里面创建一个Localization.csv文件
然后通过wps或者excel编辑
像这样:
然后将csv文件转换成UTF-8编码格式
// 直接使用,会自动判断当前系统语言
LTLocalization.GetText(Key);
// 手动指定语言使用
LTLocalization.ManualSetLanguage(SystemLanguage.language);
LTLocalization.GetText(Key);
这个用法还算简单吧...
原理
1.通过csv解析器将CSV文件整体读到内存中
2.转换为相应的table表格
3.根据需要的语言将对应的数据提取出来形成一个dictionary
4.通过dictionary需要需要的本地化语言
代码
CSV解析器
using System.Collections.Generic;
using System.IO;
using System.Text;
// col是竖行,row是横排,防止我忘了
public class LTCSVLoader
{
private TextReader inStream = null;
private List<string> vContent;
private List<List<string>> table;
/// <summary>
/// 只支持GBK2312的编码(WPS直接保存的编码支持,暂时不可用)
/// </summary>
/// <param name="fileName"></param>
private void ReadFile(string fileName)
{
inStream = new StreamReader(fileName, Encoding.GetEncoding("GBK"));
table = new List<List<string>>();
List<string> temp = this.getLineContentVector();
while (null != temp)
{
List<string> tempList = new List<string>();
for (int i = 0; i < temp.Count; ++i)
{
tempList.Add(temp[i]);
}
table.Add(tempList);
temp = this.getLineContentVector();
}
}
/// <summary>
/// 目前只支持UTF-8的编码(WPS直接保存的编码不支持)
/// </summary>
/// <param name="str"></param>
public void ReadMultiLine(string str)
{
inStream = new StringReader(str);
table = new List<List<string>>();
List<string> temp = this.getLineContentVector();
while (null != temp)
{
List<string> tempList = new List<string>();
for (int i = 0; i < temp.Count; ++i)
{
tempList.Add(temp[i]);
}
table.Add(tempList);
temp = this.getLineContentVector();
}
}
private int containsNumber(string parentStr, string parameter)
{
int containNumber = 0;
if (parentStr == null || parentStr.Equals(""))
{
return 0;
}
if (parameter == null || parameter.Equals(""))
{
return 0;
}
for (int i = 0; i < parentStr.Length; i++)
{
i = parentStr.IndexOf(parameter, i);
if (i > -1)
{
i = i + parameter.Length;
i--;
containNumber = containNumber + 1;
}
else
{
break;
}
}
return containNumber;
}
private bool isQuoteAdjacent(string p_String)
{
bool ret = false;
string temp = p_String;
temp = temp.Replace("\"\"", "");
if (temp.IndexOf("\"") == -1)
{
ret = true;
}
return ret;
}
private bool isQuoteContained(string p_String)
{
bool ret = false;
if (p_String == null || p_String.Equals(""))
{
return false;
}
if (p_String.IndexOf("\"") > -1)
{
ret = true;
}
return ret;
}
private string[] readAtomString(string lineStr)
{
string atomString = "";// 要读取的原子字符串
string orgString = "";// 保存第一次读取下一个逗号时的未经任何处理的字符串
string[] ret = new string[2];// 要返回到外面的数组
bool isAtom = false;// 是否是原子字符串的标志
string[] commaStr = lineStr.Split(new char[] { ',' });
while (!isAtom)
{
foreach (string str in commaStr)
{
if (!atomString.Equals(""))
{
atomString = atomString + ",";
}
atomString = atomString + str;
orgString = atomString;
if (!isQuoteContained(atomString))
{
// 如果字符串中不包含引号,则为正常,返回
isAtom = true;
break;
}
else
{
if (!atomString.StartsWith("\""))
{
// 如果字符串不是以引号开始,则表示不转义,返回
isAtom = true;
break;
}
else if (atomString.StartsWith("\""))
{
// 如果字符串以引号开始,则表示转义
if (containsNumber(atomString, "\"") % 2 == 0)
{
// 如果含有偶数个引号
string temp = atomString;
if (temp.EndsWith("\""))
{
temp = temp.Replace("\"\"", "");
if (temp.Equals(""))
{
// 如果temp为空
atomString = "";
isAtom = true;
break;
}
else
{
// 如果temp不为空,则去掉前后引号
temp = temp.Substring(1, temp.LastIndexOf("\""));
if (temp.IndexOf("\"") > -1)
{
// 去掉前后引号和相邻引号之后,若temp还包含有引号
// 说明这些引号是单个单个出现的
temp = atomString;
temp = temp.Substring(1);
temp = temp.Substring(0, temp.IndexOf("\""))
+ temp.Substring(temp.IndexOf("\"") + 1);
atomString = temp;
isAtom = true;
break;
}
else
{
// 正常的csv文件
temp = atomString;
temp = temp.Substring(1, temp.LastIndexOf("\""));
temp = temp.Replace("\"\"", "\"");
atomString = temp;
isAtom = true;
break;
}
}
}
else
{
// 如果不是以引号结束,则去掉前两个引号
temp = temp.Substring(1, temp.IndexOf('\"', 1))
+ temp.Substring(temp.IndexOf('\"', 1) + 1);
atomString = temp;
isAtom = true;
break;
}
}
else
{
// 如果含有奇数个引号
if (!atomString.Equals("\""))
{
string tempAtomStr = atomString.Substring(1);
if (!isQuoteAdjacent(tempAtomStr))
{
// 这里做的原因是,如果判断前面的字符串不是原子字符串的时候就读取第一个取到的字符串
// 后面取到的字符串不计入该原子字符串
tempAtomStr = atomString.Substring(1);
int tempQutoIndex = tempAtomStr.IndexOf("\"");
// 这里既然有奇数个quto,所以第二个quto肯定不是最后一个
tempAtomStr = tempAtomStr.Substring(0, tempQutoIndex)
+ tempAtomStr.Substring(tempQutoIndex + 1);
atomString = tempAtomStr;
isAtom = true;
break;
}
}
}
}
}
}
}
// 先去掉之前读取的原字符串的母字符串
if (lineStr.Length > orgString.Length)
{
lineStr = lineStr.Substring(orgString.Length);
}
else
{
lineStr = "";
}
// 去掉之后,判断是否以逗号开始,如果以逗号开始则去掉逗号
if (lineStr.StartsWith(","))
{
if (lineStr.Length > 1)
{
lineStr = lineStr.Substring(1);
}
else
{
lineStr = "";
}
}
ret[0] = atomString;
ret[1] = lineStr;
return ret;
}
private bool readCSVNextRecord()
{
// 如果流未被初始化则返回false
if (inStream == null)
{
return false;
}
// 如果结果向量未被初始化,则初始化
if (vContent == null)
{
vContent = new List<string>();
}
// 移除向量中以前的元素
vContent.Clear();
// 声明逻辑行
string logicLineStr = "";
// 用于存放读到的行
StringBuilder strb = new StringBuilder();
// 声明是否为逻辑行的标志,初始化为false
bool isLogicLine = false;
while (!isLogicLine)
{
string newLineStr = inStream.ReadLine();
if (newLineStr == null)
{
strb = null;
vContent = null;
isLogicLine = true;
break;
}
if (newLineStr.StartsWith("#"))
{
// 去掉注释
continue;
}
if (!strb.ToString().Equals(""))
{
strb.Append("\r\n");
}
strb.Append(newLineStr);
string oldLineStr = strb.ToString();
if (oldLineStr.IndexOf(",") == -1)
{
// 如果该行未包含逗号
if (containsNumber(oldLineStr, "\"") % 2 == 0)
{
// 如果包含偶数个引号
isLogicLine = true;
break;
}
else
{
if (oldLineStr.StartsWith("\""))
{
if (oldLineStr.Equals("\""))
{
continue;
}
else
{
string tempOldStr = oldLineStr.Substring(1);
if (isQuoteAdjacent(tempOldStr))
{
// 如果剩下的引号两两相邻,则不是一行
continue;
}
else
{
// 否则就是一行
isLogicLine = true;
break;
}
}
}
}
}
else
{
// quotes表示复数的quote
string tempOldLineStr = oldLineStr.Replace("\"\"", "");
int lastQuoteIndex = tempOldLineStr.LastIndexOf("\"");
if (lastQuoteIndex == 0)
{
continue;
}
else if (lastQuoteIndex == -1)
{
isLogicLine = true;
break;
}
else
{
tempOldLineStr = tempOldLineStr.Replace("\",\"", "");
lastQuoteIndex = tempOldLineStr.LastIndexOf("\"");
if (lastQuoteIndex == 0)
{
continue;
}
if (tempOldLineStr[lastQuoteIndex - 1] == ',')
{
continue;
}
else
{
isLogicLine = true;
break;
}
}
}
}
if (strb == null)
{
// 读到行尾时为返回
return false;
}
// 提取逻辑行
logicLineStr = strb.ToString();
if (logicLineStr != null)
{
// 拆分逻辑行,把分离出来的原子字符串放入向量中
while (!logicLineStr.Equals(""))
{
string[] ret = readAtomString(logicLineStr);
string atomString = ret[0];
logicLineStr = ret[1];
vContent.Add(atomString);
}
}
return true;
}
private List<string> getLineContentVector()
{
if (this.readCSVNextRecord())
{
return this.vContent;
}
return null;
}
private List<string> getVContent()
{
return this.vContent;
}
public int GetRow()
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
return table.Count;
}
public int GetCol()
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
return table[0].Count;
}
public int GetFirstIndexAtCol(string str, int col)
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
if (col >= table[0].Count)
{
throw new System.Exception("参数错误:col大于最大行");
}
for (int i = 0; i < table.Count; ++i)
{
if (table[i][col].Equals(str))
{
return i;
}
}
return -1;
}
public int GetFirstIndexAtRow(string str, int row)
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
if (row >= table.Count)
{
throw new System.Exception("参数错误:cow大于最大列");
}
int tempCount = table[0].Count;
for (int i = 0; i < tempCount; ++i)
{
if (table[row][i].Equals(str))
{
return i;
}
}
return -1;
}
public int[] GetIndexsAtCol(string str, int col)
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
if (col >= table[0].Count)
{
throw new System.Exception("参数错误:col大于最大行");
}
List<int> tempList = new List<int>();
for (int i = 0; i < table.Count; ++i)
{
if (table[i][col].Equals(str))
{
// 增加
tempList.Add(i);
}
}
return tempList.ToArray();
}
public int[] GetIndexsAtRow(string str, int row)
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
if (row >= table.Count)
{
throw new System.Exception("参数错误:cow大于最大列");
}
int tempCount = table[0].Count;
List<int> tempList = new List<int>();
for (int i = 0; i < tempCount; ++i)
{
if (table[row][i].Equals(str))
{
tempList.Add(i);
}
}
return tempList.ToArray();
}
public string GetValueAt(int col, int row)
{
if (null == table)
{
throw new System.Exception("table尚未初始化,请检查是否成功读取");
}
if (table.Count == 0)
{
throw new System.Exception("table内容为空");
}
if (row >= table.Count)
{
throw new System.Exception("参数错误:row大于最大列");
}
if (col >= table[0].Count)
{
throw new System.Exception("参数错误:col大于最大行");
}
return table[row][col];
}
}
本地化工具类
using UnityEngine;
using System.Collections.Generic;
using System.IO;
public class LTLocalization
{
public const string LANGUAGE_ENGLISH = "EN";
public const string LANGUAGE_CHINESE = "CN";
public const string LANGUAGE_JAPANESE = "JP";
public const string LANGUAGE_FRENCH = "FR";
public const string LANGUAGE_GERMAN = "GE";
public const string LANGUAGE_ITALY = "IT";
public const string LANGUAGE_KOREA = "KR";
public const string LANGUAGE_RUSSIA = "RU";
public const string LANGUAGE_SPANISH = "SP";
private const string KEY_CODE = "KEY";
private const string FILE_PATH = "LTLocalization/localization";
private SystemLanguage language = SystemLanguage.Chinese;
private Dictionary<string, string> textData = new Dictionary<string, string>();
private static LTLocalization mInstance;
private LTLocalization()
{
}
private static string GetLanguageAB(SystemLanguage language)
{
switch (language)
{
case SystemLanguage.Afrikaans:
case SystemLanguage.Arabic:
case SystemLanguage.Basque:
case SystemLanguage.Belarusian:
case SystemLanguage.Bulgarian:
case SystemLanguage.Catalan:
return LANGUAGE_ENGLISH;
case SystemLanguage.Chinese:
case SystemLanguage.ChineseTraditional:
case SystemLanguage.ChineseSimplified:
return LANGUAGE_CHINESE;
case SystemLanguage.Czech:
case SystemLanguage.Danish:
case SystemLanguage.Dutch:
case SystemLanguage.English:
case SystemLanguage.Estonian:
case SystemLanguage.Faroese:
case SystemLanguage.Finnish:
return LANGUAGE_ENGLISH;
case SystemLanguage.French:
return LANGUAGE_FRENCH;
case SystemLanguage.German:
return LANGUAGE_GERMAN;
case SystemLanguage.Greek:
case SystemLanguage.Hebrew:
case SystemLanguage.Icelandic:
case SystemLanguage.Indonesian:
return LANGUAGE_ENGLISH;
case SystemLanguage.Italian:
return LANGUAGE_ITALY;
case SystemLanguage.Japanese:
return LANGUAGE_JAPANESE;
case SystemLanguage.Korean:
return LANGUAGE_KOREA;
case SystemLanguage.Latvian:
case SystemLanguage.Lithuanian:
case SystemLanguage.Norwegian:
case SystemLanguage.Polish:
case SystemLanguage.Portuguese:
case SystemLanguage.Romanian:
return LANGUAGE_ENGLISH;
case SystemLanguage.Russian:
return LANGUAGE_RUSSIA;
case SystemLanguage.SerboCroatian:
case SystemLanguage.Slovak:
case SystemLanguage.Slovenian:
return LANGUAGE_ENGLISH;
case SystemLanguage.Spanish:
return LANGUAGE_SPANISH;
case SystemLanguage.Swedish:
case SystemLanguage.Thai:
case SystemLanguage.Turkish:
case SystemLanguage.Ukrainian:
case SystemLanguage.Vietnamese:
case SystemLanguage.Unknown:
return LANGUAGE_ENGLISH;
}
return LANGUAGE_CHINESE;
}
private void ReadData()
{
textData.Clear();
string fileName = Application.dataPath + "/Resources/LTLocalization/localization.csv";
string csvStr = ((TextAsset)Resources.Load(FILE_PATH, typeof(TextAsset))).text;
LTCSVLoader loader = new LTCSVLoader();
// loader.ReadFile(fileName);
loader.ReadMultiLine(csvStr);
int languageIndex = loader.GetFirstIndexAtRow(GetLanguageAB(language), 0);
if (-1 == languageIndex)
{
Debug.LogError("未读取到" + language + "任何数据,请检查配置表");
return;
}
int tempRow = loader.GetRow();
for (int i = 0; i < tempRow; ++i)
{
textData.Add(loader.GetValueAt(0, i), loader.GetValueAt(languageIndex, i));
}
}
private void SetLanguage(SystemLanguage language)
{
this.language = language;
}
public static void Init()
{
mInstance = new LTLocalization();
mInstance.SetLanguage(SystemLanguage.Chinese);
mInstance.ReadData();
}
public static void ManualSetLanguage(SystemLanguage setLanguage)
{
if (null == mInstance)
{
mInstance = new LTLocalization();
}
mInstance.SetLanguage(setLanguage);
mInstance.ReadData();
}
public static string GetText(string key)
{
if (null == mInstance)
{
Init();
}
if (mInstance.textData.ContainsKey(key))
{
return mInstance.textData[key];
}
return "[NoDefine]" + key;
}
}
缺陷
1.读取csv的时候是一次性全部读取到内存中,对于大文件可能会出问题
2.对于有中文的文件只支持UTF-8的格式,需要手动转换成UTF-8编码
总结
其实还是高估的自己的代码突破能力,本来想通过File去读取的
结果发现unity打包支持的文件无法进行正常的File读取
只能使用unity自带的TextAsset文件类型去支持,然后就只支持UTF-8了
以后有机会的话再优化吧,先用着...