【问题标题】:How to change the library locale as well when I change the app locale in android当我在android中更改应用程序区域设置时如何更改库区域设置
【发布时间】:2023-04-02 12:11:01
【问题描述】:

我创建了一个利用 google map 的应用程序,并在 android 中使用了 google play 服务库。

问题是当我在我的应用程序中更改语言环境时,库不会更改语言环境。但它使用电话设备的语言环境。

这是我更改语言的方法,

1) 在扩展应用程序中

@Override
    public void onCreate() {
        updateLanguage(this);
        super.onCreate();
    }


    public static void updateLanguage(Context ctx) {
        SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        String lang = langPrefs.getString("lang", Locale.getDefault().toString());
        updateLanguage(ctx, lang);
    }

    public static void updateLanguage(Context ctx, String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);

        Configuration cfg = new Configuration();

        switch (lang) {
        case "en":
            cfg.locale = Locale.ENGLISH;
            break;
        case "tl":
            cfg.locale = new Locale("tl", "PH");
            break;
        case "fr":
            cfg.locale = Locale.FRENCH;
            break;
        }

        ctx.getResources().updateConfiguration(cfg, null);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext().getApplicationContext());
        String lang = langPrefs.getString("lang", Locale.getDefault().toString());
        super.onConfigurationChanged(newConfig);
        updateLanguage(this, lang);
    }

2) 在更改语言页面中

public void selectLang(String lang, int position) {
    MyApp.updateLanguage(ctx, lang);
    refreshAfterLocaleChanged(lang ,position);
}

public void refreshAfterLocaleChanged(String lang_tmp ,int position) {
    SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
    Editor editor = langPrefs.edit();
    editor.putString("lang", lang_tmp).putString("language", lang[position]).commit();
    Intent i = new Intent(ctx, Main.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
}

非常感谢您的帮助

【问题讨论】:

    标签: android localization locale android-support-library android-location


    【解决方案1】:

    我用于更改语言的代码。它对我有用:更改所有应用的语言环境。

    public static void applyLanguage(Context context)
    {
        Configuration config = context.getResources().getConfiguration();
    
        if (!"".equals(languageCode) && !config.locale.getLanguage().equals(languageCode))
        {
            Locale locale = new Locale(languageCode);
            Locale.setDefault(locale);
            config.locale = locale;
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }
    }
    

    languageCode 是您的 lang 变量

    【讨论】:

    • 感谢您的帮助。它似乎与我之前使用的代码非常相似,我不知道为什么使用您的代码后库语言环境仍然没有更新。
    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多