Java Android 在应用程序中更改并设置默认语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22402491/
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
Android change and set default locale within the app
提问by Xavier DSouza
I am working on globalization of Android app. I have to provide options to choose different locales from within the app. I am using the following code in my activity (HomeActivity) where I provide option to change the locale
我正在研究 Android 应用程序的全球化。我必须提供从应用程序中选择不同语言环境的选项。我在我的活动 (HomeActivity) 中使用以下代码,我提供了更改语言环境的选项
Configuration config = new Configuration();
config.locale = selectedLocale; // set accordingly
// eg. if Hindi then selectedLocale = new Locale("hi");
Locale.setDefault(selectedLocale); // has no effect
Resources res = getApplicationContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
This works fine as long as there are no configuration changes like screen rotation where the locale defaults to the android system level locale rather than the locale set by the code.
只要没有诸如屏幕旋转之类的配置更改,其中语言环境默认为 android 系统级别语言环境而不是代码设置的语言环境,这就可以正常工作。
Locale.setDefault(selectedLocale);
One solution I can think of is to persist the user selected locale using SharedPreferences and in each activity's onCreate() method have the locale set to the persisted locale as the onCreate() gets called again and again for every configuration change. Is there any better way to do this so that I don't have to do it in every activity.
我能想到的一个解决方案是使用 SharedPreferences 保留用户选择的语言环境,并且在每个活动的 onCreate() 方法中,将语言环境设置为持久性语言环境,因为每次配置更改都会一次又一次地调用 onCreate() 。有没有更好的方法来做到这一点,这样我就不必在每项活动中都这样做。
Basically what I want is that - Once I change/set to some locale in my HomeActivity I want all the activities within my app to use that locale itself irrespective of any configuration changes....unless and until it is changed to other locale from the app's HomeActivity that provides options to change locale.
基本上我想要的是 - 一旦我在我的 HomeActivity 中更改/设置为某个语言环境,我希望我的应用程序中的所有活动都使用该语言环境本身,而不管任何配置更改......除非并且直到它从应用程序的 HomeActivity 提供更改区域设置的选项。
回答by Mika?l Mayer
Although the solution stated in this answer works in the general case, I found myself adding to my preference screen:
尽管此答案中所述的解决方案适用于一般情况,但我发现自己添加到我的首选项屏幕中:
<activity android:name="com.example.UserPreferences"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
This is because when the application is in landscape mode and the preference screen in portrait mode, changing the locale and going back to the application might cause trouble. Setting both to be in landscape mode prevent this from happening.
这是因为当应用程序处于横向模式且首选项屏幕处于纵向模式时,更改区域设置并返回应用程序可能会导致问题。将两者都设置为横向模式可以防止这种情况发生。
General solution
通用解决方案
You need to change the locale at the Application level, so that its effects are everywhere.
您需要在应用程序级别更改区域设置,以便其效果无处不在。
public class MyApplication extends Application
{
@Override
public void onCreate()
{
updateLanguage(this);
super.onCreate();
}
public static void updateLanguage(Context ctx)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String lang = prefs.getString("locale_override", "");
updateLanguage(ctx, lang);
}
public static void updateLanguage(Context ctx, String lang)
{
Configuration cfg = new Configuration();
if (!TextUtils.isEmpty(lang))
cfg.locale = new Locale(lang);
else
cfg.locale = Locale.getDefault();
ctx.getResources().updateConfiguration(cfg, null);
}
}
Then in your manifest you have to write:
然后在你的清单中你必须写:
<application android:name="com.example.MyApplication" ...>
...
</application>