mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-04-28 07:59:47 +00:00
Create Lua code from collected I18N keys and texts
This commit is contained in:
parent
09020415db
commit
6158ec78ca
@ -25,8 +25,8 @@ public sealed partial class CollectI18NKeysCommand
|
|||||||
var wwwrootPath = Path.Join(cwd, "wwwroot");
|
var wwwrootPath = Path.Join(cwd, "wwwroot");
|
||||||
var allFiles = Directory.EnumerateFiles(cwd, "*", SearchOption.AllDirectories);
|
var allFiles = Directory.EnumerateFiles(cwd, "*", SearchOption.AllDirectories);
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
|
var allI18NContent = new Dictionary<string, string>();
|
||||||
foreach (var filePath in allFiles)
|
foreach (var filePath in allFiles)
|
||||||
{
|
{
|
||||||
counter++;
|
counter++;
|
||||||
@ -47,16 +47,154 @@ public sealed partial class CollectI18NKeysCommand
|
|||||||
var ns = this.DetermineNamespace(filePath);
|
var ns = this.DetermineNamespace(filePath);
|
||||||
var fileInfo = new FileInfo(filePath);
|
var fileInfo = new FileInfo(filePath);
|
||||||
var name = fileInfo.Name.Replace(fileInfo.Extension, string.Empty);
|
var name = fileInfo.Name.Replace(fileInfo.Extension, string.Empty);
|
||||||
var langNamespace = $"{ns}::{name}".ToUpperInvariant().Replace(".", "::");
|
var langNamespace = $"{ns}.{name}".ToUpperInvariant();
|
||||||
foreach (var match in matches)
|
foreach (var match in matches)
|
||||||
{
|
{
|
||||||
var key = $"root::{langNamespace}::T{match.ToFNV32()}";
|
// The key in the format A.B.C.D.T{hash}:
|
||||||
|
var key = $"UI_TEXT_CONTENT.{langNamespace}.T{match.ToFNV32()}";
|
||||||
|
allI18NContent.TryAdd(key, match);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($" {counter:###,###} files processed.");
|
Console.WriteLine($" {counter:###,###} files processed, {allI18NContent.Count:###,###} keys found.");
|
||||||
Console.WriteLine();
|
|
||||||
|
Console.Write("- Creating Lua code ...");
|
||||||
|
var luaCode = this.ExportToLuaTable(allI18NContent);
|
||||||
|
|
||||||
|
// Build the path, where we want to store the Lua code:
|
||||||
|
var luaPath = Path.Join(cwd, "Assistants", "I18N", "allTexts.lua");
|
||||||
|
|
||||||
|
// Store the Lua code:
|
||||||
|
await File.WriteAllTextAsync(luaPath, luaCode, Encoding.UTF8);
|
||||||
|
|
||||||
|
Console.WriteLine(" done.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string ExportToLuaTable(Dictionary<string, string> keyValuePairs)
|
||||||
|
{
|
||||||
|
// Collect all nodes:
|
||||||
|
var root = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Split all collected keys into nodes:
|
||||||
|
//
|
||||||
|
foreach (var key in keyValuePairs.Keys.Order())
|
||||||
|
{
|
||||||
|
var path = key.Split('.');
|
||||||
|
var current = root;
|
||||||
|
for (var i = 0; i < path.Length - 1; i++)
|
||||||
|
{
|
||||||
|
// We ignore the AISTUDIO segment of the path:
|
||||||
|
if(path[i] == "AISTUDIO")
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!current.TryGetValue(path[i], out var child) || child is not Dictionary<string, object> childDict)
|
||||||
|
{
|
||||||
|
childDict = new Dictionary<string, object>();
|
||||||
|
current[path[i]] = childDict;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = childDict;
|
||||||
|
}
|
||||||
|
|
||||||
|
current[path.Last()] = keyValuePairs[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Inner method to build Lua code from the collected nodes:
|
||||||
|
//
|
||||||
|
void ToLuaTable(StringBuilder sb, Dictionary<string, object> innerDict, int indent = 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine("{");
|
||||||
|
var prefix = new string(' ', indent * 4);
|
||||||
|
foreach (var kvp in innerDict)
|
||||||
|
{
|
||||||
|
if (kvp.Value is Dictionary<string, object> childDict)
|
||||||
|
{
|
||||||
|
sb.Append($"{prefix} {kvp.Key}");
|
||||||
|
sb.Append(" = ");
|
||||||
|
|
||||||
|
ToLuaTable(sb, childDict, indent + 1);
|
||||||
|
}
|
||||||
|
else if (kvp.Value is string s)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{prefix} -- {s.Trim().Replace("\n", " ")}");
|
||||||
|
sb.Append($"{prefix} {kvp.Key}");
|
||||||
|
sb.Append(" = ");
|
||||||
|
sb.Append($"""
|
||||||
|
"{s}"
|
||||||
|
""");
|
||||||
|
sb.AppendLine(",");
|
||||||
|
sb.AppendLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.AppendLine(prefix + "},");
|
||||||
|
sb.AppendLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Write the Lua code:
|
||||||
|
//
|
||||||
|
var sbLua = new StringBuilder();
|
||||||
|
|
||||||
|
// To make the later parsing easier, we add the mandatory plugin
|
||||||
|
// metadata:
|
||||||
|
sbLua.AppendLine(
|
||||||
|
"""
|
||||||
|
-- The ID for this plugin:
|
||||||
|
ID = "77c2688a-a68f-45cc-820e-fa8f3038a146"
|
||||||
|
|
||||||
|
-- The icon for the plugin:
|
||||||
|
ICON_SVG = ""
|
||||||
|
|
||||||
|
-- The name of the plugin:
|
||||||
|
NAME = "Collected I18N keys"
|
||||||
|
|
||||||
|
-- The description of the plugin:
|
||||||
|
DESCRIPTION = "This plugin is not meant to be used directly. Its a collection of all I18N keys found in the project."
|
||||||
|
|
||||||
|
-- The version of the plugin:
|
||||||
|
VERSION = "1.0.0"
|
||||||
|
|
||||||
|
-- The type of the plugin:
|
||||||
|
TYPE = "LANGUAGE"
|
||||||
|
|
||||||
|
-- The authors of the plugin:
|
||||||
|
AUTHORS = {"MindWork AI Community"}
|
||||||
|
|
||||||
|
-- The support contact for the plugin:
|
||||||
|
SUPPORT_CONTACT = "MindWork AI Community"
|
||||||
|
|
||||||
|
-- The source URL for the plugin:
|
||||||
|
SOURCE_URL = "https://github.com/MindWorkAI/AI-Studio"
|
||||||
|
|
||||||
|
-- The categories for the plugin:
|
||||||
|
CATEGORIES = { "CORE" }
|
||||||
|
|
||||||
|
-- The target groups for the plugin:
|
||||||
|
TARGET_GROUPS = { "EVERYONE" }
|
||||||
|
|
||||||
|
-- The flag for whether the plugin is maintained:
|
||||||
|
IS_MAINTAINED = true
|
||||||
|
|
||||||
|
-- When the plugin is deprecated, this message will be shown to users:
|
||||||
|
DEPRECATION_MESSAGE = ""
|
||||||
|
|
||||||
|
-- The IETF BCP 47 tag for the language. It's the ISO 639 language
|
||||||
|
-- code followed by the ISO 3166-1 country code:
|
||||||
|
IETF_TAG = "en-US"
|
||||||
|
|
||||||
|
-- The language name in the user's language:
|
||||||
|
LANG_NAME = "English (United States)"
|
||||||
|
|
||||||
|
""");
|
||||||
|
|
||||||
|
sbLua.Append("UI_TEXT_CONTENT = ");
|
||||||
|
if(root["UI_TEXT_CONTENT"] is Dictionary<string, object> dict)
|
||||||
|
ToLuaTable(sbLua, dict);
|
||||||
|
|
||||||
|
return sbLua.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<string> FindAllTextTags(ReadOnlySpan<char> fileContent)
|
private List<string> FindAllTextTags(ReadOnlySpan<char> fileContent)
|
||||||
|
Loading…
Reference in New Issue
Block a user