Android 更改语言环境:强制活动重新加载资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2644377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:44:52  来源:igfitidea点击:

Changing locale: Force activity to reload resources?

androidlocalizationresources

提问by pgsandstrom

So I have a language setting in my application. When the language is switched, I would like all the textviews etc to change language immediately. Currently I just change the locale in the configuration, so the language has changed when the user restarts the activity.

所以我的应用程序中有一个语言设置。切换语言后,我希望所有文本视图等立即更改语言。目前我只是更改了配置中的语言环境,因此当用户重新启动 Activity 时语言已更改。

An ugly solution to my problem would be to make each textview load the new resources each time the language is changed. Is there a better solution? Perhaps a neat way to discretely restart the activity? Or maybe just force reload of the resources?

我的问题的一个丑陋的解决方案是每次更改语言时让每个文本视图加载新资源。有更好的解决方案吗?也许是一种离散重新启动活动的巧妙方法?或者只是强制重新加载资源?

采纳答案by Jim Blackler

In your AndroidManifest.xml, add this attribute to your Activity

在您的 AndroidManifest.xml 中,将此属性添加到您的 Activity

android:configChanges="locale"

In your activity override onConfigurationChanged()

在您的活动覆盖中 onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
  super.onConfigurationChanged(newConfig);
}

https://developer.android.com/guide/topics/manifest/activity-element.html#config

https://developer.android.com/guide/topics/manifest/activity-element.html#config

回答by MSquare

I think the question is switching language in runtime for the app and displaying localized messages in UI. android:configChanges="locale"calls onConfigurationChangedif the system locale is changed (in setting of your device) while the app is running, and not if you change locale in the code for your app, which I guess is what you want to accomplish. That's why it's not refreshing.

我认为问题是在应用程序运行时切换语言并在 UI 中显示本地化消息。android:configChanges="locale"呼叫onConfigurationChanged如果系统语言环境改变(在你的设备的设置)应用程序运行时,而不是如果你改变区域代码为您的应用程序,这是我的猜测是你想要完成的任务。这就是为什么它不令人耳目一新。

回答by 3c71

Here is the method I use during every activity onCreate() or onResume() depending on my needs (if my activity will be resuming after user changed language settings or will always be created with language already set):

这是我在每个活动中使用的方法 onCreate() 或 onResume() 取决于我的需要(如果我的活动将在用户更改语言设置后恢复或将始终使用已设置的语言创建):

From there I just refresh the view manually or from onConfigurationChanged() which get called after this method finishes.

从那里我只是手动刷新视图或从 onConfigurationChanged() 刷新视图,在此方法完成后调用它。

public static void changeLocale(Activity activity, String language)
{
    final Resources res = activity.getResources();
    final Configuration conf = res.getConfiguration();
    if (language == null || language.length() == 0)
    {
        conf.locale = Locale.getDefault();
    }
    else
    {
        final int idx = language.indexOf('-');
        if (idx != -1)
        {
            final String[] split = language.split("-");
            conf.locale = new Locale(split[0], split[1].substring(1));
        }
        else
        {
            conf.locale = new Locale(language);
        }
    }

    res.updateConfiguration(conf, null);
}

回答by ubhusri

public void settingLocale(Context context, String language) {

Locale locale;

Configuration config = new Configuration();

 if(language.equals(LANGUAGE_ENGLISH)) {

    locale = new Locale("en");

    Locale.setDefault(locale);

    config.locale = locale;

}else if(language.equals(LANGUAGE_ARABIC)){

    locale = new Locale("hi");

    Locale.setDefault(locale);

    config.locale = locale;

}

context.getResources().updateConfiguration(config, null);

// Here again set the text on view to reflect locale change

// and it will pick resource from new locale

tv1.setText(R.string.one); //tv1 is textview in my activity

}

回答by himalayagiant

I'm not sure why this isn't picked up by onConfigurationChanged().

我不确定为什么这没有被onConfigurationChanged().

Hey, sandis, do you mean the method onConfigurationChanged()doesn't called in your activity when you changed the language? I met the same problem. The problem maybe this: when we change the language, the activity goes to onDestroy()(you can try this), so there is nobody to call onConfigurationChanged(). When we launch the activity again, the onCreate()is called, not the onConfigurationChanged(). There maybe something different in locale change and orientation change.

嘿,sandis,你的意思是onConfigurationChanged()当你改变语言时,这个方法不会在你的活动中调用吗?我遇到了同样的问题。问题可能是这样的:当我们更改语言时,活动会转到onDestroy()(您可以尝试此操作),因此没有人可以调用onConfigurationChanged(). 当我们再次启动活动时,onCreate()调用的是 ,而不是onConfigurationChanged()。语言环境变化和方向变化可能有所不同。

回答by Thorbear

Assuming you're changing the language through something like

假设您正在通过类似的方式更改语言

private void updateLocale(@NonNull final Context context,
                          @NonNull final Locale newLocale) {
    final Resources resources = context.getResources();
    final DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    final Configuration configuration = resources.getConfiguration();
    configuration.locale = newLocale;
    resources.updateConfiguration(configuration, displayMetrics);
    Locale.setDefault(newLocale);
}

You'll need to call Activity.recreate()in all currently open activities, which is what would happen if the user changed the system language while you were not subscribing to android:configChanges="locale".

您需要在所有当前打开的活动中调用Activity.recreate(),如果用户在您未订阅android:configChanges="locale".