Change display language within an xamarin android app

扶醉桌前 提交于 2021-02-11 08:23:21

问题


How can I change the display language within an app? I want that users with different cultures can work with one scanner without changing the global culture of the device.

I have a laguage button with a click event in wich I call a method:

public void SetLocale(string language = "")
{
    Locale locale = String.IsNullOrEmpty(language)
                               ? new Locale("de-DE")
                               : new Locale(language);
    Locale.Default = locale;
    var config = new global::Android.Content.Res.Configuration();
    config.Locale = locale;
    var context = global::Android.App.Application.Context;
    context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
}

But unfortunately nothing happens when I press the button.

The click event is:

_btnen.Click += delegate
{
    SetLocale("en-GB");
};

回答1:


What can work for You, if the context can be forgotten (all property values can be lost), You can just force the Activity to redraw itself.

btn.Click += delegate
{
    SetLocale("en-GB");

    this.Recreate(); //this line
}



回答2:


With help of @Rafael Stahl, I found following solution:

_btnen.Click += delegate
{              
    Java.Util.Locale.Default = new Locale("en", "GB");
    Resources.Configuration.Locale = Java.Util.Locale.Default;
    Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
    Intent intent = new Intent(this, this.Class);
    StartActivity(intent);
};


来源:https://stackoverflow.com/questions/41978802/change-display-language-within-an-xamarin-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!