97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.Localization;
|
|
using UnityEngine.Localization.Tables;
|
|
using UnityEditor.Localization;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
public class LocalizationMultiTableExporter
|
|
{
|
|
[MenuItem("Localization/Export All Tables to CSV")]
|
|
public static async void ExportAllLocalizationTables()
|
|
{
|
|
string path = EditorUtility.SaveFilePanel("Save Localization CSV", "", "LocalizationExport.csv", "csv");
|
|
if (string.IsNullOrEmpty(path)) return;
|
|
|
|
var locales = LocalizationEditorSettings.GetLocales();
|
|
var tableCollections = LocalizationEditorSettings.GetStringTableCollections();
|
|
|
|
var allEntries = new List<LocalizationEntry>();
|
|
|
|
foreach (var collection in tableCollections)
|
|
{
|
|
string tableName = collection.TableCollectionName;
|
|
Dictionary<string, Dictionary<string, string>> keyToLocaleValue = new();
|
|
|
|
foreach (var locale in locales)
|
|
{
|
|
var stringTable = collection.GetTable(locale.Identifier) as StringTable;
|
|
if (stringTable == null) continue;
|
|
|
|
foreach (var entry in stringTable.Values)
|
|
{
|
|
if (entry.SharedEntry == null || string.IsNullOrEmpty(entry.SharedEntry.Key))
|
|
continue;
|
|
|
|
string key = entry.SharedEntry.Key;
|
|
|
|
if (!keyToLocaleValue.ContainsKey(key))
|
|
keyToLocaleValue[key] = new Dictionary<string, string>();
|
|
|
|
keyToLocaleValue[key][locale.Identifier.Code] = entry.LocalizedValue;
|
|
}
|
|
}
|
|
|
|
foreach (var kvp in keyToLocaleValue)
|
|
{
|
|
allEntries.Add(new LocalizationEntry
|
|
{
|
|
Table = tableName,
|
|
Key = kvp.Key,
|
|
LocaleValues = kvp.Value
|
|
});
|
|
}
|
|
}
|
|
|
|
// Optional: Sort entries for consistency
|
|
allEntries.Sort((a, b) => string.Compare(a.Table + a.Key, b.Table + b.Key));
|
|
|
|
// Build CSV
|
|
StringBuilder csv = new StringBuilder();
|
|
csv.Append("Table,Key");
|
|
|
|
foreach (var locale in locales)
|
|
{
|
|
csv.Append($",{locale.Identifier.Code}");
|
|
}
|
|
csv.AppendLine();
|
|
|
|
foreach (var entry in allEntries)
|
|
{
|
|
csv.Append($"{entry.Table},{entry.Key}");
|
|
foreach (var locale in locales)
|
|
{
|
|
entry.LocaleValues.TryGetValue(locale.Identifier.Code, out string value);
|
|
value ??= "";
|
|
value = value.Replace("\"", "\"\""); // Escape quotes
|
|
value = value.Replace("\n", "\\n").Replace("\r", "\\r"); // Escape line breaks
|
|
csv.Append($",\"{value}\"");
|
|
}
|
|
csv.AppendLine();
|
|
}
|
|
|
|
File.WriteAllText(path, csv.ToString(), Encoding.UTF8);
|
|
Debug.Log($"Localization exported to: {path}");
|
|
}
|
|
|
|
private class LocalizationEntry
|
|
{
|
|
public string Table;
|
|
public string Key;
|
|
public Dictionary<string, string> LocaleValues;
|
|
}
|
|
}
|