gpt4 book ai didi

java - Android-为所有版本的Android更改应用程序语言

转载 作者:行者123 更新时间:2023-12-02 12:41:13 25 4
gpt4 key购买 nike

我尝试了几种更改android应用程序语言环境的方法,但是当我在Android 7.0或更低版本上测试我的应用程序时,语言环境未更改。

注意:这些方法适用于Android 8.0或更高版本,但不适用于Android 7.0或更低版本。

在所有Android版本上都可以使用哪种方式?
你知道吗?

我尝试的方式:

  • The first way
  • The second way
  • The third way

  • 注意:我在装有Android 7.0的Galaxy Tab S2上测试了我的应用程序(无效)。并在配备Android 8.0的Galaxy A5 2017上运行(工作)。

    注意:我的许多 Activity 都是用Kotlin写的。

    我的代码:

    语言环境帮助程序类:
    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, String language) {
    persist(context, language);

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

    return updateResourcesLegacy(context, language);
    }

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

    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;
    }

    }

    attachBaseContext方法(我在所有 Activity 中都添加了这些代码):
    override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(LocaleHelper.onAttach(base, "en"))
    }

    更改语言环境方法(波斯语):
    LocaleHelper.setLocale(getContext(), "fa")

    最佳答案

    例如,您应该创建一个基本 Activity 并在其上覆盖onConfigurationChange方法,设置在共享首选项中保存的默认语言。毕竟,您必须从基本 Activity 扩展您的 Activity 。

    关于java - Android-为所有版本的Android更改应用程序语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59319426/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com