在应用程序内的Andr​​oid变化并设置为默认的语言环境 [英] Android change and set default locale within the app

查看:222
本文介绍了在应用程序内的Andr​​oid变化并设置为默认的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作对Android应用程序全球化。我必须提供的选项,选择从应用程序内不同的语言环境。我现在用的是下面的code。在我的活动(HomeActivity),我提供的选项更改地区

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

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());

这工作得很好,只要有喜欢的屏幕旋转任何配置更改,其中的语言环境默认为android系统级语言环境,而不是语言环境的code设置。

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.

Locale.setDefault(selectedLocale);

一个解决方案,我能想到的是使用共享preferences坚持用户选择的语言环境,并在每个活动的onCreate()方法都)的区域设置为持久化语言环境作为的onCreate(被一次又一次地呼吁每配置更改。有没有更好的办法做到这一点,这样我就不必做的每一项活动。

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.

基本上我想要的是 - 有一次,我改变/设置为某个区域在我的HomeActivity我希望所有在我的应用程序中的活动使用的任何配置更改的语言环境本身无关....除非将其更改为其他区域设置从应用程序的HomeActivity提供选项来改变语言环境。

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.

推荐答案

虽然这个答案说明的解决方案可在一般情况下,我发现自己加入到我的preference屏幕:

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>

这是因为,当应用程序在横向模式和preference屏幕在纵向模式下,改变语言环境中去回应用程序可能会引起麻烦。设置既要在横向模式prevent这种情况的发生。

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.

<一个href="http://roosmaa.net/application-wide-locale-override/">http://roosmaa.net/application-wide-locale-override/

您需要更改区域设置在应用程序级别,因此它的影响无处不在。

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>

这篇关于在应用程序内的Andr​​oid变化并设置为默认的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆