如何仅单击即可更改整个应用程序的语言? [英] How can I change language of whole application by only single click?

查看:129
本文介绍了如何仅单击即可更改整个应用程序的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在登录页面上有三种语言选择的Spinner.我希望当用户选择一种语言时,假设波斯" 语言应更改整个应用程序的语言.我只能更改当前Activity的语言.如何更改整个应用程序的语言.

I have a Spinner with three language choice on login page. I want when user choose one language suppose "Persian" language the language of whole application should change. I am able to change language of current Activity only. How can I change the language of whole application.

推荐答案

在Android中以编程方式更改语言

以下是更改应用程序语言环境的适当方法:

要以编程方式更改语言,您需要克服一些困难.

There are some difficulties which you have to overcome to change the language programmatically.

1.)您的应用程序在配置更改期间关闭或重新创建后,将不记得您的语言更改.

1.)Your application will not remember your language change after it is closed or recreated during the configuration change.

2.)您应根据所选语言正确更新当前可见的用户界面.

2.)You should update currently visible UI properly according to the selected language.

解决方案

"LocaleHelper"是您所需要的解决方案.您只需要在应用程序的主类上初始化语言环境即可.之后,您所有的语言更改将继续.

"LocaleHelper" is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.

在Android API版本24(牛轧糖)中进行了最近的更改之后,我们需要覆盖attachBaseContext以反映更改.

After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.

以下用于更改应用语言的方法:

Below method used to change language for application :

private static boolean updateResources(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 true;
}

在下面的链接中找到更多详细信息:

find more details in below link :

以编程方式更改Android的语言环境

这篇关于如何仅单击即可更改整个应用程序的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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