本文介绍了如何更改整个Android应用程序的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个可以显示英文或中文的应用程序.我已经准备了2个string.xml,分别是value/strings.xml和value-zh-rHK/strings.xml.但是我不知道如何通过android的ListPreference更改语言.

I want to write a application that can display either english or chinese. I have already prepared 2 string.xml which is value/strings.xml and value-zh-rHK/strings.xml. But I have no idea how to change the language via ListPreference of android.

xml/preferences.xml:

xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<SwitchPreference
    android:key="pref_nightmode"
    android:title="@string/nightmode"
    android:defaultValue="false">
</SwitchPreference>

<ListPreference
    android:key="pref_lang"
    android:title="@string/lang"
    android:dialogTitle="Choose Language"
    android:entries="@array/lang"
    android:entryValues="@array/lang_value"
    android:defaultValue="@string/lang_default">
</ListPreference>

和Preferences.java

and the Preferences.java

public class Preferences extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(drawer_menu[5]);
    getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainPreferenceFragment()).commit();
}

public static class MainPreferenceFragment extends PreferenceFragment {
    String locale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager pm = getPreferenceManager();
        ListPreference lang = (ListPreference) pm.findPreference("pref_lang");
        if(lang.getValue().equals("English")) {
            locale = "en_US";
        } else {
            locale = "zh_HK";
        }
    }
}

该活动扩展了BaseActivity,因为我在那里有一个抽屉菜单.

The activity extends BaseActivity because I have a drawer menu right there.

推荐答案

您只能更改应用程序的语言设置.使用Locale类并更新默认配置.仅当活动重新启动或重新启动应用程序时才应用语言更改.使用以下语言环境类别

You can change language settings for app only. Use Locale class and update default configuration. Language change only applied when activity restart or re-start application. Use following locale class

public class LocaleUtils {

    private static Locale sLocale;

    public static void setLocale(Locale locale) {
        sLocale = locale;
        if(sLocale != null) {
            Locale.setDefault(sLocale);
        }
    }

    public static void updateConfig(ContextThemeWrapper wrapper) {
        if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Configuration configuration = new Configuration();
            configuration.setLocale(sLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

    public static void updateConfig(Application app, Configuration configuration) {
        if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            config.locale = sLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }
}

应用程序类:

public class App extends Application {
    public void onCreate(){
        super.onCreate();
        // get user preferred language set locale accordingly new locale(language,country)
        LocaleUtils.setLocale(new Locale("iw"));
        LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleUtils.updateConfig(this, newConfig);
    }
}

BaseActivity:

BaseActivity :

public class BaseActivity extends Activity {
    public BaseActivity() {
        LocaleUtils.updateConfig(this);
    }
}

以上答案来自:在应用程序内部更改语言环境

Android文档: http://developer.android.com/reference/java/util/Locale.html

Android Document : http://developer.android.com/reference/java/util/Locale.html

这篇关于如何更改整个Android应用程序的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:38