如何使用切换器更改应用程序语言

时间:2017-11-23 07:14:33

标签: java android

我有一个包含两种语言的Android应用程序,我想添加切换器按下时,应用程序是英文的,按下时,应用程序变为阿拉伯语

2 个答案:

答案 0 :(得分:0)

调用以下函数更新语言。

private void updateLanguage(String languageCode)  //languageCode ex. 'en' for english 
{
    Context context = LocaleHelper.setLocale(MainActivity.this, languageCode);
    Log.v("langcode", languageCode);
    Resources resources = context.getResources();
}

创建一个名为 LocaleHelper 的类来执行操作。

class LocaleHelper {

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

public static Context onAttach(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    return setLocale(context, lang);
}

public static Context onAttach(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    return setLocale(context, lang);
}

public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static Context setLocale(Context context, String language) {
    persist(context, language);

    Log.v("abcdef", Locale.getDefault().getLanguage());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Log.v("Updatinglocale","Nougat");
        return updateResources(context, language);
    }

    return updateResourcesLegacy(context, language);
}

public static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return context;
}

}

答案 1 :(得分:0)

试试这个......

默认情况下,android将英语视为主要语言,并从res⇒values⇒strings.xml加载字符串资源。如果要添加对其他语言的支持,则需要通过附加连字符和ISO语言代码来创建值文件夹

在您的语言更新方法

If(strlanguageToLoad.contentEquals("en"){
  Locale locale = new Locale(strlanguageToLoad);
  Locale.setDefault(locale);
  Configuration config = new Configuration();
  config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());

//save strlanguageToLoad in sharedPreference to check later
  sharePref_Editor = sharePrefs.edit();
  sharePref_Editor.putString(“SAVED_LANGUAGE”, "en");
  sharePref_Editor.commit();

//refresh the activity 

}else{
//for arabic “ar”
//Do the same here like “en” above
}

<强>的onCreate

检查sharedPreference以获取每项活动的语言,然后执行以下操作

if (sharePrefs.getString(“SAVED_LANGUAGE”, "").equalsIgnoreCase("ar")) {
    String languageToLoad = "ar"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

} else {
    String languageToLoad = "en";
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}

样本检查here