如何改变语言的应用程序时,用户选择的语言?

我希望我的应用程序支持三种语言西班牙语,葡萄牙语和英语。并提供选择应用程序中的语言

1)可绘制文件夹可绘制文件夹可绘制文件夹可绘制文件夹可绘制文件夹可绘制文件夹。

2)3值文件夹的值-es,value-pt,value。根据语言更改 String.xml 值。

选择语言。当点击它的菜单打开,包括选项英语,西班牙语,葡萄牙语。

我通过这段代码在应用程序的选项选择上设置了区域设置

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.en:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, "Locale in English !", Toast.LENGTH_LONG).show();
break;


case R.id.pt:
Locale locale2 = new Locale("pt");
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());


Toast.makeText(this, "Locale in Portugal !", Toast.LENGTH_LONG).show();
break;


case R.id.es:
Locale locale3 = new Locale("es");
Locale.setDefault(locale3);
Configuration config3 = new Configuration();
config3.locale = locale3;
getBaseContext().getResources().updateConfiguration(config3, getBaseContext().getResources().getDisplayMetrics());


Toast.makeText(this, "Locale in Spain !", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}

我已在舱单 ConfigChanges = “ locale”中申报

它工作,但它有一些问题。

问题:-

1)语言选择时,包含语言选择图像的屏幕不变,但其他屏幕发生变化。

2)方向改变后,应用程序根据手机所在地恢复语言。

228483 次浏览

节选自网页: http://android.programmerguru.com/android-localization-at-runtime/

当用户从语言列表中选择应用程序时,改变它的语言很简单。有一个如下的方法,它接受本地语言环境作为字符串(如‘ en’表示英语,‘ hi’表示印地语) ,为您的应用程序配置本地语言环境,并刷新您当前的活动以反映语言的变化。应用的区域设置不会更改,直到您再次手动更改为止。

public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
finish();
startActivity(refresh);
}

确保您输入了以下软件包:

import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

将清单添加到活动 ConfigChanges = “ locale |  方向”

您应该从清单中删除 android:configChanges="locale",这将导致重新加载活动,或者重写 onConfigurationChanged方法:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// your code here, you can use newConfig.locale if you need to check the language
// or just re-set all the labels to desired string resource
}

好的解决方案在这里解释得很好。但是这里还有一个。

创建自己的扩展 ContextWrapperCustomContextWrapper类,并使用它更改完整应用程序的 Locale 设置。 下面是一个带有用法的 GIST

然后使用保存的地区标识符调用 CustomContextWrapper,例如,在活动生命周期方法 attachBaseContext中,用于印地语的 'hi'。用法:

@Override
protected void attachBaseContext(Context newBase) {
// fetch from shared preference also save the same when applying. Default here is en = English
String language = MyPreferenceUtil.getInstance().getString("saved_locale", "en");
super.attachBaseContext(MyContextWrapper.wrap(newBase, language));
}

Udhay 的示例代码运行良好。除了 Sofiane Hassaini 和 Chirag SolankI 的问题,对于重新进入,它不工作。在 super.onCreate (savedInstanceState) ; 之前,我尝试不重新启动 onCreate ()中的活动就调用 Udhay 的代码。那就行了! 只有一个小问题,菜单字符串仍然没有更改为设置语言环境。

    public void setLocale(String lang) { //call this in onCreate()
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
//Intent refresh = new Intent(this, AndroidLocalize.class);
//startActivity(refresh);
//finish();
}

以上所有@Uday 的代码都很完美,但是只缺少一样东西(build.gradle 中的默认配置)

public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
finish();
startActivity(refresh);

}

只是因为配置文件(build.gradle)中没有提到这些语言,所以我的程序无法正常工作

 defaultConfig {
resConfigs "en", "hi", "kn"
}

从那以后,所有的语言都开始流通了

那些得到版本问题的尝试这个代码. 。

public static void switchLocal(Context context, String lcode, Activity activity) {
if (lcode.equalsIgnoreCase(""))
return;
Resources resources = context.getResources();
Locale locale = new Locale(lcode);
Locale.setDefault(locale);
android.content.res.Configuration config = new
android.content.res.Configuration();
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
//restart base activity
activity.finish();
activity.startActivity(activity.getIntent());
}

使用上下文扩展的 Kotlin 解决方案

fun Context.applyNewLocale(locale: Locale): Context {
val config = this.resources.configuration
val sysLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.locales.get(0)
} else {
//Legacy
config.locale
}
if (sysLocale.language != locale.language) {
Locale.setDefault(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale)
} else {
//Legacy
config.locale = locale
}
resources.updateConfiguration(config, resources.displayMetrics)
}
return this
}

用法

override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase?.applyNewLocale(Locale("es", "MX")))
}
    Locale locale = new Locale(langCode);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.locale = locale;
configuration.setLayoutDirection(locale); // for RTL changes
preferences.setLocalePref(langCode);
context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());

在这里,langCode是所需的语言代码。可以在 sharedPreferences 中将语言代码保存为字符串。你可以在 onCreate中调用这个代码 super.onCreate(savedInstanceState)

There's a new (and better) implementation for in-app language pickers since Appcompat 1.6.0-alpha04 and later: Https://developer.android.com/about/versions/13/features/app-languages#androidx-impl

在 API 级别25中不推荐使用其他人建议的自定义实现中使用的方法。