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

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

问题描述

我有一个 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(Nougat)最近发生变化之后,我们需要覆盖 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天全站免登陆