using AddInPlugin.Setting;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using OpenTap;
|
using OpenTapEditor.Util;
|
using System.ComponentModel;
|
using System.IO;
|
using System.Windows;
|
|
namespace SeqEditor.Util
|
{
|
public class LangUtil
|
{
|
public const string DefaultLang = "zh-CN";
|
public static List<LangInfo> langs = new List<LangInfo>() {
|
new LangInfo(DefaultLang,"中文"),
|
new LangInfo("en-US","English")
|
};
|
|
private static ResourceDictionary currentResource;
|
|
private static Dictionary<string, ResourceDictionary> ResourceMap
|
= new Dictionary<string, ResourceDictionary>();
|
private static Dictionary<string, JObject> JsonMap = new Dictionary<string, JObject>();
|
|
private static StationSettings setting;
|
|
static LangUtil()
|
{
|
var td = TypeData.GetDerivedTypes<ComponentSettings>()
|
.Where(x => x.CanCreateInstance && (x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true))
|
.OfType<TypeData>()
|
.FirstOrDefault(x => x.Name == typeof(StationSettings).FullName);
|
if (td == null)
|
{
|
return;
|
}
|
setting = (StationSettings)ComponentSettings.GetCurrent(td.Load());
|
}
|
|
private static string GetLang()
|
{
|
var lang = setting?.Lang ?? DefaultLang;
|
if (string.IsNullOrEmpty(lang)) lang = "zh-CN";
|
return lang;
|
}
|
|
public static void LoadLang()
|
{
|
var lang = GetLang();
|
if (!ResourceMap.ContainsKey(lang)) return;
|
var newLang = ResourceMap[lang];
|
if (currentResource == newLang)
|
{
|
return;
|
}
|
if (currentResource != null)
|
{
|
Application.Current.Resources.MergedDictionaries.Remove(currentResource);
|
}
|
currentResource = newLang;
|
Application.Current.Resources.MergedDictionaries.Add(currentResource);
|
}
|
|
public static void Load()
|
{
|
try
|
{
|
LoadAllData();
|
LoadLang();
|
}
|
finally
|
{
|
|
}
|
}
|
|
public static string Get(string key)
|
{
|
var lang = GetLang();
|
if (!JsonMap.ContainsKey(lang)) return key;
|
if (!JsonMap[lang].ContainsKey(key)) return key;
|
return JsonMap[lang][key].ToString();
|
}
|
|
private static void LoadDataByJson(string lang)
|
{
|
string langFolder = PathHelper.LangFolder;
|
string folder = Path.Combine(langFolder, lang);
|
if (!Directory.Exists(folder))
|
{
|
return;
|
}
|
|
string[] files = Directory.GetFiles(folder, "*.json", SearchOption.AllDirectories);
|
|
foreach (var path in files)
|
{
|
try
|
{
|
using (FileStream stream = File.Open(path, FileMode.Open))
|
{
|
using (StreamReader streamReader = new StreamReader(stream))
|
{
|
JsonTextReader jsonTextReader = new JsonTextReader(streamReader);
|
JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
|
|
if (!JsonMap.ContainsKey(lang))
|
{
|
JsonMap[lang] = jsonObject;
|
}
|
else
|
{
|
foreach (var item in jsonObject)
|
{
|
JsonMap[lang][item.Key] = item.Value;
|
}
|
}
|
}
|
}
|
}
|
catch
|
{
|
}
|
}
|
}
|
|
private static void LoadXamlFromJson(string lang)
|
{
|
LoadDataByJson(lang);
|
if (!JsonMap.ContainsKey(lang)) return;
|
JObject json = JsonMap[lang];
|
var resource = new ResourceDictionary();
|
foreach (var item in json)
|
{
|
resource.Add(item.Key, item.Value.ToString());
|
}
|
ResourceMap[lang] = resource;
|
}
|
|
private static void LoadXamlFromJson2(string lang)
|
{
|
LoadDataByJson(lang);
|
|
if (!JsonMap.ContainsKey(lang)) return;
|
JObject json = JsonMap[lang];
|
var resource = new ResourceDictionary();
|
foreach (var item in json)
|
{
|
resource.Add(item.Key, item.Value.ToString());
|
}
|
ResourceMap[lang] = resource;
|
if (lang != DefaultLang)
|
{
|
var defaultMap = new ResourceDictionary();
|
foreach (var item in json)
|
{
|
defaultMap.Add(item.Key, item.Key);
|
}
|
ResourceMap[DefaultLang] = defaultMap;
|
}
|
}
|
|
private static void LoadAllData()
|
{
|
foreach (var lang in langs)
|
{
|
LoadXamlFromJson(lang.Code);
|
}
|
}
|
|
public static string Format(string key, params object[] datas)
|
{
|
return string.Format(Get(key), datas);
|
}
|
}
|
|
}
|