多语言相关

This commit is contained in:
bob
2025-06-05 17:52:21 +08:00
parent f9c0975f10
commit c3ff5b81a4
10 changed files with 237 additions and 58 deletions

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using FairyGUI;
using TMPro;
using UnityEngine;
namespace NBC
@@ -28,40 +30,40 @@ namespace NBC
public void AddLanguage(SystemLanguage language)
{
// var configDic = GameConfig.Inst.GetLanguageFontConfig(LanguageConst.languageMap[language]);
// string fontLocation = "Assets/ResRaw/Fonts/";
// //字体使用前需要先注册
// foreach (var config in configDic.Values)
// {
// if (config.type == "1")
// {
// DynamicFont font = new DynamicFont();
// font.name = config.name;
// font.nativeFont = Assets.LoadAsset<Font>(fontLocation + config.relativePath).Asset as Font;
// FontManager.RegisterFont(font);
// }
// else if (config.type == "2")
// {
// TMP_FontAsset fontAsset =
// Assets.LoadAsset<TMP_FontAsset>(fontLocation + config.relativePath).Asset as TMP_FontAsset;
// TMPFont font = new TMPFont();
// font.name = config.name; //这个名字要和编辑器里字体资源的名字一致
// font.fontAsset = fontAsset;
// FontManager.RegisterFont(font);
// }
// }
//
// var font2KeyDic = GameConfig.Inst.GetFont2KeyDic();
// Dictionary<string, string> keys = new Dictionary<string, string>();
// foreach (var pair in font2KeyDic)
// {
// keys.Add(pair.Key, configDic[pair.Value].name);
// }
//
// if (keys != null)
// {
// _languages[language] = keys;
// }
var configDic = Lan.Inst.GetLanguageFontConfig(LanguageConst.languageMap[language]);
string fontLocation = "Assets/Resources/Fonts/";
//字体使用前需要先注册
foreach (var config in configDic.Values)
{
if (config.Type == "1")
{
DynamicFont font = new DynamicFont();
font.name = config.Name;
font.nativeFont = Resources.Load<Font>(fontLocation + config.RelativePath);
FontManager.RegisterFont(font);
}
else if (config.Type == "2")
{
TMP_FontAsset fontAsset =
Resources.Load<TMP_FontAsset>(fontLocation + config.RelativePath) as TMP_FontAsset;
TMPFont font = new TMPFont();
font.name = config.Name; //这个名字要和编辑器里字体资源的名字一致
font.fontAsset = fontAsset;
FontManager.RegisterFont(font);
}
}
var font2KeyDic = Lan.Inst.GetFont2KeyDic();
Dictionary<string, string> keys = new Dictionary<string, string>();
foreach (var pair in font2KeyDic)
{
keys.Add(pair.Key, configDic[pair.Value].Name);
}
if (keys != null)
{
_languages[language] = keys;
}
}
public bool UseLanguage(SystemLanguage language)

View File

@@ -33,15 +33,15 @@ namespace NBC
public void AddLanguage(SystemLanguage language)
{
// var keys = GameConfig.Inst.GetLanguageImageConfig(LanguageConst.languageMap[language]);
// if (keys != null)
// {
// _languages[language] = keys;
// }
// else
// {
// _languages[language] = new Dictionary<string, string>();
// }
var keys = Lan.Inst.GetLanguageImageConfig(LanguageConst.languageMap[language]);
if (keys != null)
{
_languages[language] = keys;
}
else
{
_languages[language] = new Dictionary<string, string>();
}
}
public bool UseLanguage(SystemLanguage language)

View File

@@ -28,22 +28,22 @@ namespace NBC
public void AddLanguage(SystemLanguage language)
{
// var keys = GameConfig.Inst.GetLanguageConfig(LanguageConst.languageMap[language]);
// if (keys != null)
// {
// _languages[language] = keys;
// }
// else
// {
// _languages[language] = new Dictionary<string, string>();
// }
var keys = Lan.Inst.GetLanguageConfig(LanguageConst.languageMap[language]);
if (keys != null)
{
_languages[language] = keys;
}
else
{
_languages[language] = new Dictionary<string, string>();
}
}
public bool UseLanguage(SystemLanguage language)
{
if (_languages.ContainsKey(language))
if (_languages.TryGetValue(language, out var language1))
{
_currentLanguageDictionary = _languages[language];
_currentLanguageDictionary = language1;
return true;
}

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace NBC
{
public struct LanguageFontConfig
{
public string Type;
public string Name;
public string RelativePath;
}
public class LanguageConfig
{
public string Key;
private readonly Dictionary<string, string> _languages = new Dictionary<string, string>();
public string this[string key] => _languages.GetValueOrDefault(key, key);
public Dictionary<string, string>.KeyCollection Keys => _languages.Keys;
public void Parse(JToken row)
{
Key = row["key"].ToString();
var children = row.Children();
foreach (var child in children)
{
if (child is not JProperty property) continue;
if (property.Name == "key") continue;
_languages[property.Name] = property.Value.ToString();
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dd21030efb0f447988cd659f8a754506
timeCreated: 1749112259

View File

@@ -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
}
}

View File

@@ -1,3 +1,17 @@
{
"name": "NBC.Lan"
}
"name": "NBC.Lan",
"rootNamespace": "",
"references": [
"GUID:8c8f9d96103e94a7da84b012fd7e9f13",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -3,9 +3,9 @@
"rootNamespace": "",
"references": [
"GUID:b3c9c3fa0cbae6e438ac062aa5d78b76",
"GUID:8beb767f9f7a66f4e87dd9db57cdd64e",
"GUID:8c8f9d96103e94a7da84b012fd7e9f13",
"GUID:3f4a88279c0696a488a2e08f8bccf903"
"GUID:3f4a88279c0696a488a2e08f8bccf903",
"GUID:8beb767f9f7a66f4e87dd9db57cdd64e"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -79,6 +79,7 @@ namespace NBF
}
UI.Inst.SetUILanguage<UILangeageConfig>();
Lan.Inst.AutoUseLanguage();
}
#endregion

Binary file not shown.