Created
April 28, 2022 16:48
-
-
Save Char0394/b5746b7d788258efb400129766b52829 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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