我的应用程序中遇到多语言设置问题吗?
在我的应用程序中,我想创建一个设置,用户可以根据自己的选择更改语言。为此,我为每种语言创建了各自的值文件夹和字符串文件。我的代码片段如下:
public class MultiLanguage extends Activity implements OnClickListener {
private Button germany, english, french;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language);
english = (Button) findViewById(R.id.english);
english.setOnClickListener(this);
germany = (Button) findViewById(R.id.germany);
germany.setOnClickListener(this);
french = (Button) findViewById(R.id.french);
french.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.english:
setLocale("en");
break;
case R.id.germany:
setLocale("de");
break;
case R.id.french:
setLocale("fr");
break;
}
}
public void setLocale(String localStr) {
Locale localeLang = new Locale(localStr);
Locale.setDefault(localeLang);
Configuration config = new Configuration();
config.locale = localeLang;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
finish();
Intent refreshActivity = new Intent(this, Home.class);
refreshActivity.setAction(Intent.ACTION_CONFIGURATION_CHANGED);
startActivity(refreshActivity);
}
}
但是每次我从应用程序中出来并再次启动应用程序时,所选的语言都会变成英语。我没有得到这个问题的根源。
在清单文件中,我还提到了以下行:
android:configChanges="locale"
任何人都可以纠正我在这里犯的错误吗?
In my application I would like create a settings where user can be able to change the language according to his choice. For this i have created respective values folder and string files for each language. Also my code snippet is as follows:
public class MultiLanguage extends Activity implements OnClickListener {
private Button germany, english, french;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language);
english = (Button) findViewById(R.id.english);
english.setOnClickListener(this);
germany = (Button) findViewById(R.id.germany);
germany.setOnClickListener(this);
french = (Button) findViewById(R.id.french);
french.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.english:
setLocale("en");
break;
case R.id.germany:
setLocale("de");
break;
case R.id.french:
setLocale("fr");
break;
}
}
public void setLocale(String localStr) {
Locale localeLang = new Locale(localStr);
Locale.setDefault(localeLang);
Configuration config = new Configuration();
config.locale = localeLang;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
finish();
Intent refreshActivity = new Intent(this, Home.class);
refreshActivity.setAction(Intent.ACTION_CONFIGURATION_CHANGED);
startActivity(refreshActivity);
}
}
BUt each time I am coming out from the application and launching the app again the language selected became english. I am not getting the root of this problem.
In manifest file I have also mentioned the following line:
android:configChanges="locale"
Can any body correct the mistake I have made here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您没有在程序启动之间保存您的语言。使用 首选项 保留它,并在
onCreate.
The problem is that you don't save your language in between of program launches. Persist it using preferences and read them in your
onCreate
.你不是想重新发明轮子吗?
为什么不使用用户在手机上选择的默认区域设置?这是Android生态系统中的首选方式。
您唯一需要做的就是提供资源文件的多个版本(每个所需的区域设置一个)(例如
values/strings.xml
)。如果在应用程序的资源中找不到用户的区域设置,则默认为values/strings.xml
。否则它将使用values-[locale]/strings.xml
更多详细信息:http://developer.android.com/guide/topics/resources/localization.html
Aren't you trying to reinvent the wheel?
Why not using the default locale selected by the user on his phone? This is the preferred way in the Android ecosystem.
The only thing you have to do is provide multiple versions (one per desired locale) of your resource files (e.g.
values/strings.xml
). If the user's locale is not found in your application's resources, it will default to thevalues/strings.xml
. Else it will usevalues-[locale]/strings.xml
More details: http://developer.android.com/guide/topics/resources/localization.html
问题在于你的流程,你完成了你的活动,然后有目的地开始新的活动,所以这样做:
The problem is with your flow you finish your Activity and and then start new one with intent so do like this: