多语言相关
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC
|
||||
@@ -17,6 +18,11 @@ namespace NBC
|
||||
/// </summary>
|
||||
private Action _changeAction;
|
||||
|
||||
public LanguageManager()
|
||||
{
|
||||
LoadLanguageConfig();
|
||||
}
|
||||
|
||||
public void AddLanguageModule(int type, ILanguage languageModule)
|
||||
{
|
||||
_lanModuleDic.Add(type, languageModule);
|
||||
@@ -97,5 +103,124 @@ namespace NBC
|
||||
{
|
||||
_changeAction -= callback;
|
||||
}
|
||||
|
||||
#region config
|
||||
|
||||
private readonly Dictionary<string, LanguageConfig> _languageConfigs = new Dictionary<string, LanguageConfig>();
|
||||
private readonly Dictionary<string, string> _font2Key = new Dictionary<string, string>();
|
||||
|
||||
private readonly Dictionary<string, LanguageConfig> _languageFontConfigs =
|
||||
new Dictionary<string, LanguageConfig>();
|
||||
|
||||
private readonly Dictionary<string, LanguageConfig> _languageImagesConfigs =
|
||||
new Dictionary<string, LanguageConfig>();
|
||||
|
||||
public Dictionary<string, string> GetLanguageConfig(LanguageInfo languageInfo)
|
||||
{
|
||||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||||
foreach (var key in _languageConfigs.Keys)
|
||||
{
|
||||
var value = _languageConfigs[key];
|
||||
dic[key] = value[languageInfo.Code];
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
public Dictionary<string, LanguageFontConfig> GetLanguageFontConfig(LanguageInfo languageInfo)
|
||||
{
|
||||
if (_font2Key.Count != _languageFontConfigs.Count)
|
||||
{
|
||||
foreach (var fKey in _languageFontConfigs.Keys)
|
||||
{
|
||||
var config = _languageFontConfigs[fKey];
|
||||
foreach (var key in config.Keys)
|
||||
{
|
||||
var result = config[key];
|
||||
if (result != null)
|
||||
{
|
||||
string[] strs = result.Split(",");
|
||||
_font2Key[strs[1]] = fKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<string, LanguageFontConfig> dic = new Dictionary<string, LanguageFontConfig>();
|
||||
foreach (var key in _languageFontConfigs.Keys)
|
||||
{
|
||||
var config = _languageFontConfigs[key];
|
||||
string result = config[languageInfo.Code];
|
||||
if (result != null)
|
||||
{
|
||||
string[] strs = result.Split(",");
|
||||
var result2 = new LanguageFontConfig()
|
||||
{
|
||||
Type = strs[0],
|
||||
Name = strs[1],
|
||||
RelativePath = strs[2],
|
||||
};
|
||||
dic[key] = result2;
|
||||
}
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
|
||||
public Dictionary<string, string> GetLanguageImageConfig(LanguageInfo languageInfo)
|
||||
{
|
||||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||||
foreach (var key in _languageImagesConfigs.Keys)
|
||||
{
|
||||
var value = _languageImagesConfigs[key];
|
||||
dic[key] = value[languageInfo.Code];
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetFont2KeyDic()
|
||||
{
|
||||
return _font2Key;
|
||||
}
|
||||
|
||||
private void LoadLanguageConfig()
|
||||
{
|
||||
_languageConfigs.Clear();
|
||||
var textAsset = Resources.Load<TextAsset>("config/language");
|
||||
if (textAsset)
|
||||
{
|
||||
var jToken = JObject.Parse(textAsset.text);
|
||||
foreach (var obj in jToken)
|
||||
{
|
||||
switch (obj.Key)
|
||||
{
|
||||
case "language":
|
||||
LoadLanguageConfig(obj.Value, _languageConfigs);
|
||||
break;
|
||||
case "languageFont":
|
||||
LoadLanguageConfig(obj.Value, _languageFontConfigs);
|
||||
break;
|
||||
case "languageImage":
|
||||
LoadLanguageConfig(obj.Value, _languageImagesConfigs);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadLanguageConfig(JToken token, Dictionary<string, LanguageConfig> dictionary)
|
||||
{
|
||||
if (token is not JArray jArray) return;
|
||||
foreach (var j in jArray)
|
||||
{
|
||||
var config = new LanguageConfig();
|
||||
config.Parse(j);
|
||||
dictionary[config.Key] = config;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user