ファイル
manju summonerとClaude Opus 5.5 3f358dd4ef NuGet パッケージを .NET 11 に合わせて更新し、CsvHelper 34 に移行する
- CsvHelper 33.1.0 → 34.1.0、Microsoft.Bcl.AsyncInterfaces 10.0.11 → 11.0.0、
  Microsoft.CodeAnalysis.CSharp / Analyzers 5.9.0 → 5.12.0、
  Microsoft.Build.Framework / Utilities.Core 18.9.6 → 18.12.2
- CsvHelper 34 で CsvConfiguration・[Index]・反射による GetRecords<T> が無くなったので、
  翻訳 CSV の読み込みを TranslateRecord.ReadRecords に移し、低レベル API で書き直す。
  ソースジェネレーターと resx 生成タスクで共有する
- 読み方は 33 と同じにする(ヘッダーあり、カンマ区切り、空行は読み飛ばす、
  引用符の外側の前後の半角スペースを除く、足りない列は null、引用符の誤りは例外)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VFWrCqYda2FSoLvJNkEGwa
2026-11-24 00:00:00 +09:00

115 行
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace YukkuriMovieMaker.Generator.Build
{
/// <summary>
/// 翻訳CSVから言語別のresxを生成する。
/// resxはソースジェネレーターのAddSourceでは追加できず、ディスクに書き出して
/// EmbeddedResourceとして埋め込む必要がある。ソースジェネレーター(CoreCompile内)で書くと
/// resxを.resourcesへ変換するGenerateResourceより後になり、1回目のビルドに反映されないため、
/// MSBuildタスクからPrepareResourcesより前に呼び出す。
/// </summary>
public static class LocalizeResxGenerator
{
public sealed class ResxFile
{
public ResxFile(string path, bool updated)
{
Path = path;
Updated = updated;
}
public string Path { get; }
/// <summary>内容が変わったため書き込んだ場合はtrue。同じ内容ならタイムスタンプを保つために書き換えない</summary>
public bool Updated { get; }
}
/// <summary>
/// CSVがresxの生成対象かを判定する。
/// ソースジェネレーターは「[AutoGenLocalizer]付きpartial classと同名のCSV」を対象にするが、
/// ここでは同名の.csがコンパイル対象に含まれることだけを条件にし、属性の有無はソース文字列から推測しない。
/// テキスト判定はコメント中の属性や属性内のコメントで取りこぼしが起き、resx欠落は実行時例外になる一方、
/// 属性のないペアにresxが余分に付く害は小さいため、生成する側に倒す。
/// </summary>
/// <param name="csvPath">CSVのフルパス</param>
/// <param name="compileFullPaths">コンパイル対象ソースのフルパス集合(大文字小文字を区別しない比較器で構築すること)</param>
public static bool IsLocalizeCsv(string csvPath, ISet<string> compileFullPaths)
{
return compileFullPaths.Contains(Path.ChangeExtension(csvPath, ".cs"));
}
/// <summary>
/// CSVから全言語分のresxを生成し、対象となったresxのパスを返す(内容が同じで書き換えなかったものも含む)。
/// </summary>
public static IReadOnlyList<ResxFile> Generate(string csvPath)
{
var records = ReadRecords(csvPath)
.Distinct(new TranslateRecordKeyComparer())
.ToList();
var results = new List<ResxFile>();
foreach (var langCode in TranslateRecord.LangCodes)
{
var resxPath = GetResxPath(csvPath, langCode);
var newBytes = BuildResx(records, langCode);
var updated = WriteIfChanged(resxPath, newBytes);
results.Add(new ResxFile(resxPath, updated));
}
return results;
}
/// <summary>ja-jpは既定言語としてカルチャ名なしのresxにする</summary>
public static string GetResxPath(string csvPath, string langCode)
{
return langCode == "ja-jp"
? Path.ChangeExtension(csvPath, ".resx")
: Path.ChangeExtension(csvPath, $".{langCode}.resx");
}
static List<TranslateRecord> ReadRecords(string csvPath)
{
using var streamReader = new StreamReader(csvPath);
return TranslateRecord.ReadRecords(streamReader);
}
static byte[] BuildResx(List<TranslateRecord> records, string langCode)
{
using var memStream = new MemoryStream();
using (var writer = new ResXResourceWriter(memStream))
{
writer.AddResource("CurrentCulture", langCode);
foreach (var record in records)
{
var value = record.GetValue(langCode);
if (string.IsNullOrEmpty(record.Key) || string.IsNullOrEmpty(value))
continue;
writer.AddResource(record.Key!, value!);
}
}
return memStream.ToArray();
}
static bool WriteIfChanged(string path, byte[] newBytes)
{
if (File.Exists(path))
{
var existing = File.ReadAllBytes(path);
if (BytesEqual(existing, newBytes))
return false;
}
File.WriteAllBytes(path, newBytes);
return true;
}
static bool BytesEqual(byte[] a, byte[] b)
{
if (a.Length != b.Length) return false;
for (int i = 0; i < a.Length; i++)
if (a[i] != b[i]) return false;
return true;
}
}
}