Android 7本地化问题

时间:2017-11-20 07:25:46

标签: android localization

我有android项目支持一些language.String文件位于合适的文件夹中。例如:带有french的文件字符串,位于文件夹'values-fr'中。在运行时我想改变语言。我有类LocaleHelper

public 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, final String language) {
    String tmpLang = language;
    if (language.length() > 2) {
        tmpLang = language.substring(0, 2);
    }
    persist(context, tmpLang);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, tmpLang);
    }

    return updateResourcesLegacy(context, tmpLang);
}

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

    Resources.getSystem().updateConfiguration(configuration, context.getResources().getDisplayMetrics());

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

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

    return context;
}

public static void clearPersist(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.remove(SELECTED_LANGUAGE);
    editor.apply();
}

}

并且在BaseActivity中

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base));
}

当我以'法式模式'启动我的应用程序时

LocaleHelper.setLocale(this, "fr");

在Android_Version上比Android7少,每件事情都正常但在Android_Version> = Android7我有部分本地化的应用。部分字符串在英语中,部分在法语中。
P.S
工具栏中的标题具有正确的区域设置

1 个答案:

答案 0 :(得分:0)

您需要使用国家/地区代码作为第二个参数初始化Locale类。例如。在我的代码中:

if (language.equals("ru") {
    locale = new Locale(language, "RU");
} else if (language.equals("en")) {
    locale = new Locale(language, "US");
}