做多语言的时候用中文做KEY绝对是有100%的好处,如果用英文表示那么代码里面给文字赋值的地方全都是英文,写的代码多了以后维护起来就没有人能看懂了,或者看起来很费劲。
说说用中文做KEY的原理:Unity中给文字赋值的地方就两处, 一个是提前预制在UI Prefab上的文字,还有一个是写在代码里面的文字。那么在开发阶段我们在Prefab和代码里面直接就写中文,等项目后期通过工具把所有中文的地方全部提取出来。然后把提取出来的中文交给策划,让策划他们去翻译去,这样我们之前写的中文就是多语言的KEY,最终显示的界面上的文字是用这个中文KEY读表读出来的。
NGUI里所有的文字都是在UILabel中,可是我们要做图文混排,一般都是在UILabel上在拓展一个自己的脚本,用这个脚本在生成对应的UILabel和UISprite。这篇文章我就先以UILabel来说明原理。
1.遍历所有UIPrefab把包含UILabe(或者是你自己写的)组件找出来,并且把文字提取出来。
2.遍历所有的CS代码,把所有 StrUtil.GetText(“雨松MOMO\n我要换行“);双引号中间的中文以及字符全部提取出来。
直接上思路代码。
usingUnityEngine;
usingSystem.Collections;
usingUnityEditor;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
publicclassTestStart:Editor
{
//UIPrefab文件夹目录
privatestaticstringUIPrefabPath=Application.dataPath+"/UI";
//脚本的文件夹目录
privatestaticstringScriptPath=Application.dataPath+"/Scripts";
//导出的中文KEY路径
privatestaticstringOutPath=Application.dataPath+"/out.txt";
privatestaticListLocalization=null;
privatestaticstringstaticWriteText="";
[MenuItem("Tools/导出多语言")]
staticvoidExportChinese()
{
Localization=newList();
staticWriteText="";
//提取Prefab上的中文
staticWriteText+="----------------Prefab----------------------\n";
LoadDiectoryPrefab(newDirectoryInfo(UIPrefabPath));
//提取CS中的中文
staticWriteText+="----------------Script----------------------\n";
LoadDiectoryCS(newDirectoryInfo(ScriptPath));
//最终把提取的中文生成出来
stringtextPath=OutPath;
if(System.IO.File.Exists(textPath))
{
File.Delete(textPath);
}
using(StreamWriterwriter=newStreamWriter(textPath,false,Encoding.UTF8))
{
writer.Write(staticWriteText);
}
AssetDatabase.Refresh();
}
//递归所有UI Prefab
staticpublicvoidLoadDiectoryPrefab(DirectoryInfodictoryInfo)
{
if(!dictoryInfo.Exists)return;
FileInfo[]fileInfos=dictoryInfo.GetFiles("*.prefab",SearchOption.AllDirectories);
foreach(FileInfofilesinfileInfos)
{
stringpath=files.FullName;
stringassetPath=path.Substring(path.IndexOf("Assets/"));
GameObjectprefab=AssetDatabase.LoadAssetAtPath(assetPath,typeof(GameObject))asGameObject;
GameObjectinstance=GameObject.Instantiate(prefab)asGameObject;
SearchPrefabString(instance.transform);
GameObject.DestroyImmediate(instance);
}
}
//递归所有C#代码
staticpublicvoidLoadDiectoryCS(DirectoryInfodictoryInfo)
{
if(!dictoryInfo.Exists)return;
FileInfo[]fileInfos=dictoryInfo.GetFiles("*.cs",SearchOption.AllDirectories);
foreach(FileInfofilesinfileInfos)
{
stringpath=files.FullName;
stringassetPath=path.Substring(path.IndexOf("Assets/"));
TextAssettextAsset=AssetDatabase.LoadAssetAtPath(assetPath,typeof(TextAsset))asTextAsset;
stringtext=textAsset.text;
//用正则表达式把代码里面两种字符串中间的字符串提取出来。
Regexreg=newRegex("(?<=StrUtil.GetText\\s*\\(\\s*\"\\s*)[\\s\\S]*?(?=\\s*\")");
MatchCollectionmc=reg.Matches(text);
foreach(Matchminmc)
{
stringformat=m.Value;
if(!Localization.Contains(format)&&!string.IsNullOrEmpty(format)){
Localization.Add(format);
staticWriteText+=format+"\n";
}
}
}
}
//提取Prefab上的中文
staticpublicvoidSearchPrefabString(Transformroot)
{
foreach(Transformchindinroot)
{
//因为这里是写例子,所以我用的是UILabel
//这里应该是写你用于图文混排的脚本。
UILabellabel=chind.GetComponent();
if(label!=null)
{
stringtext=label.text;
if(!Localization.Contains(text)&&!string.IsNullOrEmpty(text)){
Localization.Add(text);
text=text.Replace("\n",@"\n");
staticWriteText+=text+"\n";
}
}
if(chind.childCount>0)
SearchPrefabString(chind);
}
}
}
比如这个是个简单界面上赋值的代码。用StrUtil.GetText()去取中文,StrUtiL类是我们自己写的。
usingUnityEngine;
usingSystem.Collections;
publicclassUIMain:MonoBehaviour
{
privateUILabelmName=null;
voidAwake()
{
mName=transform.Find("name").GetComponent();
mName.text=StrUtil.GetText("雨松MOMO\n我要换行");
mName.text=StrUtil.GetText("我是{0}我的网名{1}","宣雨松","雨松MOMO");
}
}
StrUtiL类里面去处理Key从本地数据表里中替换对应多语言显示的文字。
usingUnityEngine;
usingSystem.Collections;
publicclassStrUtil
{
staticpublicstringGetText(stringtext)
{
//通过传进来的中文KEY 去数据表里面读对应替换的多语言文字
returntext;
}
staticpublicstringGetText(stringtext,paramsobject[]args)
{
//通过传进来的中文KEY 去数据表里面读对应替换的多语言文字
returnstring.Format(text,args);
}
}
使用工具代码提取,最终将所有多语言中文的地方提取在txt里面。
最后就是让策划拿着生成出来的中文KEY在Excel表里,给出对应的翻译文字。
还有一个重要的知识点就是换行问题,可能你在Prefab上进行的换行的操作,但是\n并不是字符串,所以我们要把\n转成”\n”字符串写进去。
text.Replace(“\n”,@”\n”);
反过来在读取表的时候还是需要再把”\n”字符串转成\n换行符
text.Replace(@”\n”,”\n”);
这样就没问题了。策划也可以直接在数据表里填写\n来进行换行了。
最后的思考
1.开发的过程中可能要修改代码或者要加新功能删功能,所以我们要把差异性的中文Key提取出来,也就是把新增加的KEY 或者 新删除的KEY列举出来。因为没有变化的就不需要策划重新翻译了。
2.最好能直接帮策划生成Excel文件,Windows上很容易,但是MAC就不行。我知道怎么在Mac上读取excel文件,但是我不知道在mac上怎么生成Excel有哪位大神知道还请告知一下我。要能生成.xlsx的那种,谢谢啦。
3.因为要做图文混排,所以UILabel我已经不直接使用了,而是又写了一个类去管理UILable和UISprite, 其实就是根据XML或者JSON 一类的描述符去动态生成UILable和UISprite在帮它的动态的算坐标,算间距 一类的。因为你的中文KEY需要传参数 比如 “我叫{0}我今年{1}大了” 一类的字符串,所以还是在写一个方法。
最后是本文的下载地址,其实本文主要还是提供一个思路。 如果你有对多语言更好的建议,或者是办法,欢迎在下面给我留言,谢谢。
补充, 之前的正则表达式有点问题。因为在代码编写的时候允许中间存在 空格或者换行。 比如下面这样,编译器是不会报错的。。
StrUtil.GetText(“我是222中文”);
StrUtil.GetText ( “22 2222雨松MOMO” );
StrUtil.GetText
“222雨松520”
);
所以改一下即可。
stringexpr="(?<=StrUtil.GetText\\s*\\(\\s*\"\\s*)[\\s\\S]*?(?=\\s*\")";
MatchCollectionmc=Regex.Matches(text,expr);
foreach(Matchminmc)
{
Debug.Log(m);
}
作者:雨松MOMO