【问题标题】:Automatically change drawer language when app resume from backstack当应用程序从后台恢复时自动更改抽屉语言
【发布时间】:2018-04-09 06:22:26
【问题描述】:

我在我的应用程序“英语”和“法语”中实现了两种语言,并且它们都可以单独工作。当我尝试使用设备语言检查语言更改时,出现问题。

最近我的应用和设备语言是“法语”。现在我将设备语言从“法语”更改为“英语”,然后从后台打开应用程序,应用程序语言仍然是“法语”,但在应用程序中,导航抽屉相关内容更改为“英语”

下面是我在 GlobalClass 中更改语言的代码

public void changelanguage(String languageToLoad, Context context) {

            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config,
                    context.getResources().getDisplayMetrics());

        }

下面是 MainActivity 和 Splashscreen 上比较语言的代码

gc = GlobalClass.getInstance()
prefsWrapper = PreferencesWrapper(applicationContext)
sel_langague = prefsWrapper.getPreferenceStringValue(SipConfigManager.LANGUAGE)
println("Language Main : " + sel_langague)
var languageToLoad = ""
if (sel_langague == "0") {
     languageToLoad ="en"
} else {
     languageToLoad ="fr"
}
gc!!.changelanguage(languageToLoad, baseContext)

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 重启你的应用时你能看到语言改变了吗?

标签: android localization kotlin resources


【解决方案1】:

检查一下,

String language = preferences.getString("language", null);

这是我的 onclick 按钮监听器

 llChangeLanguage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            if (language == null) {
                LocaleHelper.setLocale(BaseActivity.this, "de");
                Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
                storeLanguageInPref("en");
                startActivity(intent);
                finish();
            } else if ("kn".contentEquals(language)) {
                LocaleHelper.setLocale(BaseActivity.this, "de");
                Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
                startActivity(intent);
                storeLanguageInPref("en");
                finish();
            } else {
                LocaleHelper.setLocale(BaseActivity.this, "kn");
                Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
                storeLanguageInPref("kn");
                startActivity(intent);

                finish();
            }

        }
    });

在这里我将所选语言存储为偏好

 private void storeLanguageInPref(String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(BaseActivity.this);
    SharedPreferences.Editor editor = preferences.edit();

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

LocaleHelper 类

public class LocaleHelper {

public static Context onAttach(Context context) {
    String locale = getPersistedLocale(context);
    return setLocale(context, locale);
}

public static String getPersistedLocale(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SettingsFragment.KEY_PREF_LANGUAGE, "");
}

/**
 * Set the app's locale to the one specified by the given String.
 *
 * @param context
 * @param localeSpec a locale specification as used for Android resources (NOTE: does not
 *                   support country and variant codes so far); the special string "system" sets
 *                   the locale to the locale specified in system settings
 * @return
 */
public static Context setLocale(Context context, String localeSpec) {
    Locale locale;
    if (localeSpec.equals("system")) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = Resources.getSystem().getConfiguration().getLocales().get(0);
        } else {
            //noinspection deprecation
            locale = Resources.getSystem().getConfiguration().locale;
        }
    } else {
        locale = new Locale(localeSpec);
    }
    Locale.setDefault(locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, locale);
    } else {
        return updateResourcesLegacy(context, locale);
    }
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLayoutDirection(locale);
    }

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

    return context;
}
}

【讨论】:

    【解决方案2】:

    您有两种不同的选择。

    1. 设置包括“语言环境”到 android:configChanges 在 xml。建议按照doc 说的改语言的方式,

    android:configChanges 列出 Activity 将自行处理的配置更改。当运行时发生配置更改时,默认情况下会关闭并重新启动 Activity,但使用该属性声明配置会阻止 Activity 重新启动。相反,活动保持运行并调用其 onConfigurationChanged() 方法。

    注意:应避免使用此属性,仅将其用作最后的手段。有关如何正确处理由于配置更改而重新启动的更多信息,请阅读处理运行时更改。

    以下任何或所有字符串都是此属性的有效值。多个值用“|”分隔— 例如,“locale|navigation|orientation”。

    1. 另一种方法是通过广播接收器。 ACTION_LOCALE_CHANGED。 您可以在清单中注册一个 BroadcastReceiver 来处理它。

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2015-06-19
      相关资源
      最近更新 更多