feat(i18n): add ClaudeDo.Localization project with nested-JSON locale parser
This commit is contained in:
10
src/ClaudeDo.Localization/ClaudeDo.Localization.csproj
Normal file
10
src/ClaudeDo.Localization/ClaudeDo.Localization.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ClaudeDo.Localization.Tests" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
15
src/ClaudeDo.Localization/LocaleFile.cs
Normal file
15
src/ClaudeDo.Localization/LocaleFile.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace ClaudeDo.Localization;
|
||||
|
||||
public sealed class LocaleFile
|
||||
{
|
||||
public LocaleFile(string code, string name, IReadOnlyDictionary<string, string> strings)
|
||||
{
|
||||
Code = code;
|
||||
Name = name;
|
||||
Strings = strings;
|
||||
}
|
||||
|
||||
public string Code { get; }
|
||||
public string Name { get; }
|
||||
public IReadOnlyDictionary<string, string> Strings { get; }
|
||||
}
|
||||
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