Hello,
I have a viable solution that has been tested in Android 10 and later.
Here is the solution you can try it.
Step1. After creating resource files, you need to create TranslateExtension.cs
and Translator.cs
in your project. The following is their code:
TranslateExtension.cs:
public class TranslateExtension : IMarkupExtension<BindingBase>
{
public TranslateExtension(string text)
{
Text = text;
}
public string Text { get; set; }
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
public BindingBase ProvideValue(IServiceProvider serviceProvider)
{
var binding = new Binding
{
Mode = BindingMode.OneWay,
Path = $"[{Text}]",
Source = Translator.Instance,
};
return binding;
}
}
Translator.cs:
public class Translator :INotifyPropertyChanged
{
public string this[string text]
{
get
{
// Here is the name of your Resource file.
return AppResources.ResourceManager.GetString(text, CultureInfo);
}
}
public CultureInfo CultureInfo { get; set; }
public static Translator Instance { get; } = new Translator();
public event PropertyChangedEventHandler PropertyChanged;
public void Invalidate()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}
}
Step2. You need to use this style Binding [Task], Source={x:Static vm:Translator.Instance}
(the Task
is a Name in your resource file) in your XAML file to binding.
Step3. In your PickerEvent, for example, if you want to convert language to German, you can add the following code to the event:
Translator.Instance.CultureInfo = new CultureInfo("de");
Translator.Instance.Invalidate();
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.