当应用程序从后退中恢复时,自动更改抽屉语言 [英] Automatically change drawer language when app resume from backstack

查看:58
本文介绍了当应用程序从后退中恢复时,自动更改抽屉语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序英语"和法语"中实现了两种语言,并且它们各自都可以正常工作.当我尝试用设备语言检查语言更改时,就遇到了问题.

I have implemented two language in my app "English" and "French" and both of them are working fine individually. When I am trying to check the language change with device language that time getting problem.

最近,我的应用和设备语言为法语".现在,我将设备语言从法语"更改为英语",然后从堆栈中打开该应用程序,该应用程序语言仍为法语",但是在该应用程序中,与导航抽屉相关的内容已更改为英语"

Recently my app and device language are "French". Now I am changing the device language from "French" to "English" and then open the app from backstack, app language is still in "French" thats right but In the app, navigation drawer related content changed to "English"

下面是我在GlobalClass中完成的用于更改语言的代码

Below is the code for changing language which I have done in 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上的语言的代码

And below is the code for comparing language on MainActivity and 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)

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

Does any one have idea for how can I resolve that problem?

推荐答案

选中此项,

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

这是我的按钮的onclick侦听器

and this is my onclick listener of button

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

        }
    });

在这里,我将选择的语言存储为首选项

and here i am storing the selected language to preference

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

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

LocaleHelper类

LocaleHelper class

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

这篇关于当应用程序从后退中恢复时,自动更改抽屉语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆