设置区域不适用于自定义对话框

huangapple go评论74阅读模式
英文:

Setting Locale Not applying to custom dialogue

问题

以下是您所提供代码的翻译部分:

我有一个应用程序,在其中的基础活动中有以下代码。

    String CurrentLang = sessionManager.getStringValue("UserLang");
    Locale locale = new Locale(CurrentLang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= 24) {
        config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    } else {
        config.locale = locale;
        getBaseContext().getApplicationContext().createConfigurationContext(config);
    }

在片段(fragment)和活动(activity)等中,它能够正常工作。然而,在自定义对话框中,它显示默认语言。如果用户从设置菜单更改语言,应用程序将重新启动基础活动,并且它开始显示自定义语言,即使在对话框中也是如此。但是,当我从最近使用的应用程序中移除应用并重新启动时,它再次仅对自定义对话框显示默认语言,在其他地方显示正常。如果有人能够帮助我解决这个问题,敬请告知。谢谢!
英文:

I have app in which I have below code in base activity.

String CurrentLang = sessionManager.getStringValue("UserLang");
Locale locale = new Locale(CurrentLang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        if (Build.VERSION.SDK_INT >= 24) {
            config.setLocale(locale);
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        } else {
            config.locale = locale;
            getBaseContext().getApplicationContext().createConfigurationContext(config);
        }

Its working fine in fragment and activity etc, However in custom dialogue, its showing default language. If user change language from setting menu, app restarting with base activity and its start showing custom language even in dialogue but when I remove application from recent app and start again, its showing again default language only for custom dialogue, other place its showing fine, Let me know if someone can help me for solve the puzzle. Thanks!

答案1

得分: 0

尝试一下:

Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration conf = resources.getConfiguration();
conf.setLocale(new Locale(language/*例如:en表示英语,ar表示阿拉伯语等等*/));
resources.updateConfiguration(conf, dm);

然后您需要重新启动您的布局。

英文:

Try this

    Resources resources=getResources();
    DisplayMetrics dm=resources.getDisplayMetrics();
    Configuration conf=resources.getConfiguration();
    conf.setLocale(new Locale(language/*e.g: en for english, ar for arabic ....etc*/));
    resources.updateConfiguration(conf,dm);

Then you have to restart your layout

huangapple
  • 本文由 发表于 2020年7月26日 09:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63095246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定