feat(i18n): add LocaleStore folder discovery
This commit is contained in:
31
src/ClaudeDo.Localization/LocaleStore.cs
Normal file
31
src/ClaudeDo.Localization/LocaleStore.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace ClaudeDo.Localization;
|
||||
|
||||
public sealed class LocaleStore
|
||||
{
|
||||
private readonly Dictionary<string, LocaleFile> _byCode;
|
||||
|
||||
private LocaleStore(Dictionary<string, LocaleFile> byCode) => _byCode = byCode;
|
||||
|
||||
public IReadOnlyList<LocaleFile> Available => _byCode.Values.ToList();
|
||||
|
||||
public bool TryGet(string code, out LocaleFile? file) => _byCode.TryGetValue(code, out file);
|
||||
|
||||
public static LocaleStore Load(string folder)
|
||||
{
|
||||
var byCode = new Dictionary<string, LocaleFile>(StringComparer.OrdinalIgnoreCase);
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
foreach (var path in Directory.EnumerateFiles(folder, "*.json"))
|
||||
{
|
||||
try
|
||||
{
|
||||
var file = LocaleJson.Parse(File.ReadAllText(path));
|
||||
if (!string.IsNullOrWhiteSpace(file.Code))
|
||||
byCode[file.Code] = file;
|
||||
}
|
||||
catch { /* skip malformed locale files */ }
|
||||
}
|
||||
}
|
||||
return new LocaleStore(byCode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user