| | |
| | | { |
| | | public class LangUtil |
| | | { |
| | | public const string DefaultLang = "zh-CN"; |
| | | public static List<LangInfo> langs = new List<LangInfo>() { |
| | | new LangInfo("zh-CN","中文"), |
| | | new LangInfo(DefaultLang,"中文"), |
| | | new LangInfo("en-US","English") |
| | | }; |
| | | |
| | |
| | | private static string GetLang() |
| | | { |
| | | var lang = SystemSetting.Instance.CurrentLanguage; |
| | | if (string.IsNullOrEmpty(lang)) lang = CultureInfo.CurrentUICulture.ToString(); |
| | | if (string.IsNullOrEmpty(lang)) lang = "zh-CN"; |
| | | return lang; |
| | | } |
| | | |
| | |
| | | { |
| | | try |
| | | { |
| | | /// 按Json加载 |
| | | //LoadAllDataByJson(); |
| | | /// 下面是按xaml加载 |
| | | LoadAllDataByXaml(); |
| | | LoadAllData(); |
| | | LoadLang(); |
| | | } |
| | | finally { |
| | | |
| | | finally |
| | | { |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | catch |
| | | { |
| | | } |
| | | } |
| | |
| | | ResourceMap[lang] = resource; |
| | | } |
| | | |
| | | |
| | | private static void LoadDataByXaml(string lang) |
| | | private static void LoadXamlFromJson2(string lang) |
| | | { |
| | | string langFolder = PathHelper.LangFolder; |
| | | string folder = Path.Combine(langFolder, lang); |
| | | if (!Directory.Exists(folder)) |
| | | LoadDataByJson(lang); |
| | | |
| | | if (!JsonMap.ContainsKey(lang)) return; |
| | | JObject json = JsonMap[lang]; |
| | | var resource = new ResourceDictionary(); |
| | | foreach (var item in json) |
| | | { |
| | | return; |
| | | resource.Add(item.Key, item.Value.ToString()); |
| | | } |
| | | |
| | | string[] files = Directory.GetFiles(folder, "*.xaml", SearchOption.AllDirectories); |
| | | |
| | | foreach (var path in files) |
| | | ResourceMap[lang] = resource; |
| | | if (lang != DefaultLang) |
| | | { |
| | | try |
| | | var defaultMap = new ResourceDictionary(); |
| | | foreach (var item in json) |
| | | { |
| | | using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read)) |
| | | { |
| | | var resource = (ResourceDictionary)XamlReader.Load(stream); |
| | | if (!ResourceMap.ContainsKey(lang)) |
| | | { |
| | | ResourceMap[lang] = resource; |
| | | } |
| | | else |
| | | { |
| | | ResourceMap[lang].MergedDictionaries.Add(resource); |
| | | } |
| | | } |
| | | defaultMap.Add(item.Key, item.Key); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | } |
| | | ResourceMap[DefaultLang] = defaultMap; |
| | | } |
| | | } |
| | | |
| | | private static void LoadAllDataByJson() |
| | | { |
| | | foreach (var lang in langs) |
| | | { |
| | | LoadDataByJson(lang.Code); |
| | | } |
| | | } |
| | | |
| | | private static void LoadAllDataByXaml() |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | public static class LangReader |