Add option to export a single localized string table to .pot file

This commit is contained in:
Wojciech Figat
2021-11-15 16:35:59 +01:00
parent 0797ee2a1f
commit 8459d7f3ad
2 changed files with 66 additions and 54 deletions

View File

@@ -165,60 +165,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
var exportLocalization = group.Button("Export...").Button;
exportLocalization.TooltipText = "Exports the localization strings into .pot file for translation";
exportLocalization.Height = 16.0f;
exportLocalization.Clicked += delegate
{
if (FileSystem.ShowSaveFileDialog(null, null, "*.pot", false, "Export localization for translation to .pot file", out var filenames))
return;
Profiler.BeginEvent("LocalizationSettingsEditor.Export");
if (!filenames[0].EndsWith(".pot"))
filenames[0] += ".pot";
var nplurals = 1;
foreach (var e in tableEntries)
{
foreach (var value in e.Value.Values)
{
if (value != null && value.Length > nplurals)
nplurals = value.Length;
}
}
using (var writer = new StreamWriter(filenames[0], false, Encoding.UTF8))
{
writer.WriteLine("msgid \"\"");
writer.WriteLine("msgstr \"\"");
writer.WriteLine("\"Language: English\\n\"");
writer.WriteLine("\"MIME-Version: 1.0\\n\"");
writer.WriteLine("\"Content-Type: text/plain; charset=UTF-8\\n\"");
writer.WriteLine("\"Content-Transfer-Encoding: 8bit\\n\"");
writer.WriteLine($"\"Plural-Forms: nplurals={nplurals}; plural=(n != 1);\\n\"");
writer.WriteLine("\"X-Generator: FlaxEngine\\n\"");
var written = new HashSet<string>();
foreach (var e in tableEntries)
{
foreach (var pair in e.Value)
{
if (written.Contains(pair.Key))
continue;
written.Add(pair.Key);
writer.WriteLine("");
writer.WriteLine($"msgid \"{pair.Key}\"");
if (pair.Value == null || pair.Value.Length < 2)
{
writer.WriteLine("msgstr \"\"");
}
else
{
writer.WriteLine("msgid_plural \"\"");
for (int i = 0; i < pair.Value.Length; i++)
writer.WriteLine($"msgstr[{i}] \"\"");
}
}
if (written.Count == allKeys.Count)
break;
}
}
Profiler.EndEvent();
};
exportLocalization.Clicked += () => Export(tableEntries, allKeys);
// Find localized strings in code button
var findStringsCode = group.Button("Find localized strings in code").Button;
@@ -284,6 +231,62 @@ namespace FlaxEditor.CustomEditors.Dedicated
Profiler.EndEvent();
}
internal static void Export(Dictionary<LocalizedStringTable, Dictionary<string, string[]>> tableEntries, HashSet<string> allKeys = null)
{
if (FileSystem.ShowSaveFileDialog(null, null, "*.pot", false, "Export localization for translation to .pot file", out var filenames))
return;
Profiler.BeginEvent("LocalizationSettingsEditor.Export");
var filename = filenames[0];
if (!filename.EndsWith(".pot"))
filename += ".pot";
var nplurals = 1;
foreach (var e in tableEntries)
{
foreach (var value in e.Value.Values)
{
if (value != null && value.Length > nplurals)
nplurals = value.Length;
}
}
using (var writer = new StreamWriter(filename, false, Encoding.UTF8))
{
writer.WriteLine("msgid \"\"");
writer.WriteLine("msgstr \"\"");
writer.WriteLine("\"Language: English\\n\"");
writer.WriteLine("\"MIME-Version: 1.0\\n\"");
writer.WriteLine("\"Content-Type: text/plain; charset=UTF-8\\n\"");
writer.WriteLine("\"Content-Transfer-Encoding: 8bit\\n\"");
writer.WriteLine($"\"Plural-Forms: nplurals={nplurals}; plural=(n != 1);\\n\"");
writer.WriteLine("\"X-Generator: FlaxEngine\\n\"");
var written = new HashSet<string>();
foreach (var e in tableEntries)
{
foreach (var pair in e.Value)
{
if (written.Contains(pair.Key))
continue;
written.Add(pair.Key);
writer.WriteLine("");
writer.WriteLine($"msgid \"{pair.Key}\"");
if (pair.Value == null || pair.Value.Length < 2)
{
writer.WriteLine("msgstr \"\"");
}
else
{
writer.WriteLine("msgid_plural \"\"");
for (int i = 0; i < pair.Value.Length; i++)
writer.WriteLine($"msgstr[{i}] \"\"");
}
}
if (allKeys != null && written.Count == allKeys.Count)
break;
}
}
Profiler.EndEvent();
}
private static void FindNewKeysCSharp(string file, Dictionary<string, string> newKeys, HashSet<string> allKeys)
{
var startToken = "Localization.GetString";

View File

@@ -133,6 +133,8 @@ namespace FlaxEditor.Windows.Assets
_toolstrip.AddSeparator();
_undoButton = (ToolStripButton)_toolstrip.AddButton(Editor.Icons.Undo64, _undo.PerformUndo).LinkTooltip("Undo (Ctrl+Z)");
_redoButton = (ToolStripButton)_toolstrip.AddButton(Editor.Icons.Redo64, _undo.PerformRedo).LinkTooltip("Redo (Ctrl+Y)");
_toolstrip.AddSeparator();
_toolstrip.AddButton(Editor.Icons.Up64, OnExport).LinkTooltip("Export localization table entries for translation to .pot file");
// Panel
var panel = new Panel(ScrollBars.Vertical)
@@ -158,6 +160,13 @@ namespace FlaxEditor.Windows.Assets
UpdateToolstrip();
}
private void OnExport()
{
var tableEntries = new Dictionary<LocalizedStringTable, Dictionary<string, string[]>>();
tableEntries[Asset] = _proxy.Entries;
CustomEditors.Dedicated.LocalizationSettingsEditor.Export(tableEntries);
}
/// <inheritdoc />
public override void Save()
{