feat(i18n): add Avalonia loc:Tr markup extension and LocalizedString
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ClaudeDo.Data\ClaudeDo.Data.csproj" />
|
||||
<ProjectReference Include="..\ClaudeDo.Localization\ClaudeDo.Localization.csproj" />
|
||||
<ProjectReference Include="..\ClaudeDo.Releases\ClaudeDo.Releases.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
22
src/ClaudeDo.Ui/Localization/LocalizedString.cs
Normal file
22
src/ClaudeDo.Ui/Localization/LocalizedString.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel;
|
||||
using ClaudeDo.Localization;
|
||||
|
||||
namespace ClaudeDo.Ui.Localization;
|
||||
|
||||
public sealed class LocalizedString : INotifyPropertyChanged
|
||||
{
|
||||
private readonly ILocalizer _localizer;
|
||||
private readonly string _key;
|
||||
|
||||
public LocalizedString(ILocalizer localizer, string key)
|
||||
{
|
||||
_localizer = localizer;
|
||||
_key = key;
|
||||
_localizer.LanguageChanged += (_, _) =>
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
|
||||
}
|
||||
|
||||
public string Value => _localizer[_key];
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
}
|
||||
25
src/ClaudeDo.Ui/Localization/TrExtension.cs
Normal file
25
src/ClaudeDo.Ui/Localization/TrExtension.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using ClaudeDo.Localization;
|
||||
|
||||
namespace ClaudeDo.Ui.Localization;
|
||||
|
||||
public sealed class TrExtension : MarkupExtension
|
||||
{
|
||||
public TrExtension() { }
|
||||
public TrExtension(string key) => Key = key;
|
||||
|
||||
public string Key { get; set; } = "";
|
||||
|
||||
public static ILocalizer? Localizer { get; set; }
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
var loc = Localizer ?? throw new InvalidOperationException("TrExtension.Localizer not initialized");
|
||||
return new Binding(nameof(LocalizedString.Value))
|
||||
{
|
||||
Source = new LocalizedString(loc, Key),
|
||||
Mode = BindingMode.OneWay
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user