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