using Brutal.Localization.ResX; namespace Brutal.Localization.ResX; /// /// A ResX writer to replace the defunct ResXResourceWriter that no longer works in /// .NET Core. Supports only strings at present. /// public class ResXWriter : IDisposable { /// /// Create a ResXWriter that writes to a file. /// /// Path to the file. Can be relative or absolute. public ResXWriter(string filePath) { _writer = new StreamWriter(filePath); _resXFile = new(); } /// /// Create a ResX writer that writes to a TextWriter. /// /// The writer to write ResX data to. public ResXWriter(TextWriter writer) { _writer = writer; _resXFile = new(); } /// /// Adds a string along with a value and options comment. /// /// The key for the value in the ResX. /// The value of the string /// A user supplied comment public void AddString(string name, string value, string? comment = null) { _resXFile.DataDictionary.Add(name, new ResXData(name, value, comment)); } /// /// Close a ResXWriter, writing all content first. /// public void Close() { if (_closed) return; Generate(); _writer!.Flush(); _writer.Close(); _closed = true; } /// /// Close and dispose of the ResXWriter. /// public void Dispose() { Close(); _writer?.Dispose(); GC.SuppressFinalize(this); } private void Generate() { if (_writer is null) throw new InvalidOperationException("ResXWriter already closed"); var content = _resXFile.Build(); _writer.Write(content); } private readonly ResXFile _resXFile; private readonly TextWriter? _writer; private bool _closed; }