应用程序被终止后,应用程序语言重置为默认值

发布于 2025-01-17 06:38:56 字数 3491 浏览 3 评论 0原文

您好,我有一个应用程序,它加载一个 webview,并且其中有一个 FLOWING DRAWER 。 我正在尝试更改语言环境。 我有一个 localeHelper java 类和一个扩展应用程序的 AppClass

但它没有发生。

仅当我重新启动应用程序时,区域设置才会发生变化 super.Onbackpressed() 但是当我杀死它时〜语言环境变回英语;它重置了

我从大约四天开始就发疯了。

请帮助我,我是初学者;

localeHelp.java

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);
        configuration.setLayoutDirection(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;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

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

        return context;
    }
}

只需调用 setLocale("fr"); 在MainActivity中使用这个函数

 private void setLocale() {
        Locale locale;
        locale = new Locale("hi");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;


        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
       Intent intent = getIntent();
       finish();

       startActivity(intent);}

Hi I have an App which is a loads a webview and there's a FLOWING DRAWER in it.
I am trying to change the locale.
I have a localeHelper java class and an AppClass which extends Application

But its just not happening.

What happens is the locale changes only when I restart the app super.Onbackpressed()
but when I kills it ~ the locale turns back to english; it resets

I have been going mad from around 4 days.

Please help me, I'm a beginner;

localeHelp.java

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);
        configuration.setLayoutDirection(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;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

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

        return context;
    }
}

simply calling
setLocale("fr");
with this function in MainActivity

 private void setLocale() {
        Locale locale;
        locale = new Locale("hi");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;


        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
       Intent intent = getIntent();
       finish();

       startActivity(intent);}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2025-01-24 06:38:56

问题:活动被重新创建,但语言仍然相同。
好吧,经过多次尝试和错误,这就是我要做的工作。

我把这段代码放在 OnResume 中

// todo load locale
        Locale current = getResources().getConfiguration().locale; 
 // todo get the current locale

        //Toast.makeText(MainActivity.this, ""+current, Toast.LENGTH_SHORT).show();
        String currentLanginString = current.toString(); // convert current language to string
        if(!currentLanginString.contains(wholeAppLanguage)) {
            recreate();
           // Toast.makeText(this, "recreatingResume", Toast.LENGTH_SHORT).show();
        }
        loadLocale();

,然后创建了 Loadlocale 函数,

 public void loadLocale (){


        int langcode=0;
        String a="a";
        if (wholeAppLanguage.contains("hi")) {
            langcode = 0;
            a="hi";
        }
        else if (wholeAppLanguage.contains("en"))
        {
            langcode=1;
            a="en";
        }

        setLocale2(langcode);


    }

然后它完美地工作了~

The problem: activity was recreated but the language was still the same..
Okay so after much trial and error this is what I got to work..

I put this code in OnResume

// todo load locale
        Locale current = getResources().getConfiguration().locale; 
 // todo get the current locale

        //Toast.makeText(MainActivity.this, ""+current, Toast.LENGTH_SHORT).show();
        String currentLanginString = current.toString(); // convert current language to string
        if(!currentLanginString.contains(wholeAppLanguage)) {
            recreate();
           // Toast.makeText(this, "recreatingResume", Toast.LENGTH_SHORT).show();
        }
        loadLocale();

and then I created the Loadlocale function

 public void loadLocale (){


        int langcode=0;
        String a="a";
        if (wholeAppLanguage.contains("hi")) {
            langcode = 0;
            a="hi";
        }
        else if (wholeAppLanguage.contains("en"))
        {
            langcode=1;
            a="en";
        }

        setLocale2(langcode);


    }

then it worked perfectly~

她比我温柔 2025-01-24 06:38:56

我也遇到了应用程序终止后语言重置的相同问题,但是,这个解决方案对我有用。

摘要:

  • 重写所有活动和应用程序类中的attachBaseContext 方法。
  • 将语言首选项保存到 SharedPreferences 或从 SharedPreferences 加载。

I also experienced the same problem of language getting reset after app kill, However, this solution worked for me.

Summary:

  • Override attachBaseContext method in all activities and application class.
  • Save/Load the language preference to/from SharedPreferences.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文