| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Newtonsoft.Json;
- namespace LocalizationTool.Services
- {
- /// <summary>
- /// 语言包数据模型(JSON 格式)。
- /// 字段名与主程序 TeamAAS.Localization.LanguageResourcePack 的 JSON 契约完全一致,
- /// 两个程序读写同一份 .json 文件。
- /// </summary>
- public class LanguagePack
- {
- [JsonProperty("languageCode")]
- public string LanguageCode { get; set; }
- [JsonProperty("languageName")]
- public string LanguageName { get; set; }
- [JsonProperty("version")]
- public string Version { get; set; } = "1.0.0";
- [JsonProperty("author")]
- public string Author { get; set; }
- /// <summary>翻译字典:中文原文 → 译文。</summary>
- [JsonProperty("translations")]
- public Dictionary<string, string> Translations { get; set; } = new Dictionary<string, string>();
- }
- /// <summary>语言包读写(JSON,人类可读)。</summary>
- public static class LanguagePackIO
- {
- /// <summary>
- /// 保存为 .json(UTF-8 无 BOM、缩进格式化,方便外部人员直接查看编辑)。
- /// </summary>
- public static void SaveJson(LanguagePack pack, string filePath)
- {
- File.WriteAllText(filePath, JsonConvert.SerializeObject(pack, Formatting.Indented),
- new UTF8Encoding(false));
- }
- /// <summary>从 .json 加载。</summary>
- public static LanguagePack LoadJson(string filePath)
- {
- var pack = JsonConvert.DeserializeObject<LanguagePack>(File.ReadAllText(filePath, Encoding.UTF8));
- if (pack == null) throw new InvalidDataException("JSON 内容为空");
- if (string.IsNullOrWhiteSpace(pack.LanguageCode))
- pack.LanguageCode = Path.GetFileNameWithoutExtension(filePath);
- if (pack.Translations == null) pack.Translations = new Dictionary<string, string>();
- return pack;
- }
- }
- }
|