feat(i18n): add ClaudeDo.Localization project with nested-JSON locale parser
This commit is contained in:
46
src/ClaudeDo.Localization/LocaleJson.cs
Normal file
46
src/ClaudeDo.Localization/LocaleJson.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ClaudeDo.Localization;
|
||||
|
||||
public static class LocaleJson
|
||||
{
|
||||
public static LocaleFile Parse(string json)
|
||||
{
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
|
||||
var code = "";
|
||||
var name = "";
|
||||
if (root.TryGetProperty("metadata", out var meta) && meta.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
if (meta.TryGetProperty("code", out var c)) code = c.GetString() ?? "";
|
||||
if (meta.TryGetProperty("name", out var n)) name = n.GetString() ?? "";
|
||||
}
|
||||
|
||||
var strings = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
foreach (var prop in root.EnumerateObject())
|
||||
{
|
||||
if (prop.NameEquals("metadata")) continue;
|
||||
Flatten(prop.Name, prop.Value, strings);
|
||||
}
|
||||
|
||||
return new LocaleFile(code, name, strings);
|
||||
}
|
||||
|
||||
private static void Flatten(string prefix, JsonElement el, IDictionary<string, string> into)
|
||||
{
|
||||
switch (el.ValueKind)
|
||||
{
|
||||
case JsonValueKind.Object:
|
||||
foreach (var p in el.EnumerateObject())
|
||||
Flatten($"{prefix}.{p.Name}", p.Value, into);
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
into[prefix] = el.GetString() ?? "";
|
||||
break;
|
||||
default:
|
||||
into[prefix] = el.ToString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user