项目需求:SDK需要多语言
旧思路:采用打标签的方式进行多语言切换,不能动态增加减少,且增加的时候比较麻烦
新思路:在config目录下检索所有的文件,每一个文件对应一种语言,可方便动态新增减少语言种类
//当前语言
public static Dictionary<string, string> defaultLanuage = new Dictionary<string, string>();
//语言集合
public static Dictionary<string, Dictionary<string, string>> listLanuageColliction = new Dictionary<string, Dictionary<string, String>>();
private static int lastLanguageInt;
private static int languageInt;
public static int LanguageInt
{
get
{
return languageInt;
}
set
{
languageInt = value;
if (lastLanguageInt != languageInt)
{
lastLanguageInt = languageInt;
string currentLanguage = ((ICollection<string>)Main.listLanuageColliction.Keys).ToArray()[languageInt];
Debug.Log(lastLanguageInt);
defaultLanuage = listLanuageColliction[currentLanguage];
}
}
}
/// <summary>
/// Plugins/Editor/Asset/Configs 路径下以txt为后缀的都是默认为多语言文本
/// 解析多语言文本
/// </summary>
public static void GetLanuage()
{
defaultLanuage.Clear();
listLanuageColliction.Clear();
DirectoryInfo d_info = new DirectoryInfo(Path.Combine(Application.dataPath, "Plugins/Editor/Asset/Configs"));
foreach (var item in d_info.GetFiles())
{
if(item.Extension.Equals(".txt"))
{
var key = item.Name.Replace(".txt","");
listLanuageColliction.Add(key, new Dictionary<string, string>());
using (var sw = item.OpenText())
{
ParsingTxt(sw.ReadToEnd(), listLanuageColliction[key]);
}
}
}
}
public static void Init()
{
GetLanuage();
defaultLanuage = listLanuageColliction[((ICollection<string>)Main.listLanuageColliction.Keys).ToArray()[0]];
//初始化数据
}