【问题标题】:Android 7 localization issueAndroid 7 本地化问题
【发布时间】:2017-11-20 07:25:46
【问题描述】:

我有 android 项目,支持一些位于合适文件夹中的 language.String 文件。例如:法语文件字符串,位于文件夹“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");

在低于 Android7 的 Android_Version 上,一切正常,但在 Android_Version>=Android7 上,我有部分本地化的应用程序。部分字符串为英文,部分为法文。
附言
工具栏中的标题具有正确的语言环境

【问题讨论】:

标签: android localization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2012-03-26
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多