通过更改Android中的应用程序语言来更改默认手机语言

时间:2012-11-23 06:57:25

标签: android

有没有办法通过更改应用程序中的语言来更改手机语言。

我的意思是当我更改应用程序的语言时,默认的手机语言也会发生变化。

对此有任何想法,请在此分享。

提前致谢。

3 个答案:

答案 0 :(得分:1)

Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);

然后

在res / values-ja中为japanese创建一个文件夹,为res / values-ar创建一个用于阿拉伯语的文件夹。

制作string.xml文件并将所需语言放在布局上

它的例子是res / values-ar for arabic -

<?xml version="1.0" encoding="UTF-8"?>
  <resources>
    <string name="spinner_label">حسب</string>
    <string name="app_name">فرق</string> 
    <string name="search">بحث:</string>
</resource>

答案 1 :(得分:1)

我不知道它可以以编程方式更改,但在您更改应用程序语言后,您可以要求用户也更改设备语言,

要求用户更改设备语言

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");            
startActivity(intent);

更改应用语言

<activity
    android:name=".ui.SomeActivity"
    android:configChanges="locale"
    :
    :
</activity>


public static void setLanguage(Context context, String languageToLoad) {
    Log.d(TAG, "setting language");
    Locale locale = new Locale(languageToLoad); //e.g "sv"
    Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
    if (systemLocale != null && systemLocale.equals(locale)) {
       Log.d(TAG, "Already correct language set");
       return;
    }
    Locale.setDefault(locale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Log.d(TAG, "Language set");
}

答案 2 :(得分:1)

android:configChanges =“locale”添加到Android Manifest文件中的活动声明中。

然后从该活动的onCreate调用以下方法。

public static void setLanguage(Context context, String languageToLoad) {
  Log.d(TAG, "setting language");
  Locale locale = new Locale(languageToLoad); //e.g "sv"
  Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
   if (systemLocale != null && systemLocale.equals(locale)) {
   Log.d(TAG, "Already correct language set");
   return;
 }
 Locale.setDefault(locale);
 android.content.res.Configuration config = new android.content.res.Configuration();
 config.locale = locale;
 context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());                                                            
 Log.d(TAG, "Language set");
  }
相关问题