Skip to content

Instantly share code, notes, and snippets.

@jp2masa
Last active February 9, 2021 18:27
Show Gist options
  • Save jp2masa/6b427f2bfc925fd7a3b2cea3efddf3f2 to your computer and use it in GitHub Desktop.
Save jp2masa/6b427f2bfc925fd7a3b2cea3efddf3f2 to your computer and use it in GitHub Desktop.
internal sealed class TranslationBinding : IBinding
{
private readonly IBinding _binding;
public TranslationBinding(IBinding binding)
{
_binding = binding;
}
public InstancedBinding Initiate(IAvaloniaObject target, AvaloniaProperty targetProperty, object anchor = null, bool enableDataValidation = false)
{
var instance = _binding.Initiate(target, null);
IBinding CreateTranslationBinding(string translationKey) =>
new Binding($"[{translationKey}]")
{
Mode = BindingMode.OneWay,
Source = TranslationManager.Instance,
};
var observable = from x in instance.Subject
from y in IsTranslation((string)x, out var key)
? CreateTranslationBinding(key).Initiate(target, null).Subject
: Observable.Return(x)
select y;
return new InstancedBinding(Subject.Create<object>(Observer.Create<object>(x => { }), observable), BindingMode.OneWay, instance.Priority);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment