Skip to content

Instantly share code, notes, and snippets.

@Char0394
Created April 28, 2022 16:48
Show Gist options
  • Save Char0394/b5746b7d788258efb400129766b52829 to your computer and use it in GitHub Desktop.
Save Char0394/b5746b7d788258efb400129766b52829 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SalesHub.Framework.Localization
{
[ContentProperty("Text")]
public class LocalizerTranslateExtension : BindableObject, IMarkupExtension<BindingBase>
{
public string Text { get; set; }
// public ILocalizer Localizer { get; set; }
public static readonly BindableProperty LocalizerProperty = BindableProperty.Create(nameof(Localizer), typeof(ILocalizer), typeof(ILocalizer), null);
public ILocalizer Localizer
{
get { return (ILocalizer) GetValue(LocalizerProperty); }
set { SetValue(LocalizerProperty, value); }
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
public BindingBase ProvideValue(IServiceProvider serviceProvider)
{
var binding = new Binding
{
Mode = BindingMode.TwoWay,
Path = $"[{Text}]",
Source = this,
};
return binding;
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if(propertyName == LocalizerProperty.PropertyName)
{
Text = Localizer.GetTranslation(Text);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment