如何在android中检查设备语言? [英] How to check for a device language in android?

查看:27
本文介绍了如何在android中检查设备语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序检查手机使用的语言.这个 if 语句应该这样做:

I want my application to check which language the phone is using. This if statement is supposed to do that:

 if (Locale.getDefault().getLanguage().equals("en")) {
     yourYesResponse = "That is great " + usersName + "!";
 }
 else if (Locale.getDefault().getLanguage().equals("fr")) {
     yourYesResponse = "C\'est bon " + usersName + "!";
 }

但即使我的设备设置为法语,它仍然显示英语.这个 if 语句有什么问题吗?如果有,那又怎样?

But even if my device is set to French it still displays the English. Is there anything wrong with this if statement and if yes, then what?

感谢您的帮助.我很感激.

Thanks for the help. I appreciate it.

推荐答案

你可以通过为每种语言声明一个字符串资源文件来解决这个问题.

You could solve this by declaring a string resource file for each language.

创建名为 values-fr 和 values-en 的资源文件夹,并将名为 strings.xml 的文件添加到这两个文件夹中.

Create resource folders named values-fr and values-en and add a file called strings.xml to both folders.

values-en 中的 string.xml 文件:

The string.xml file in values-en:

<resources>
<string name="good">That is great </string> 
</resources>

然后像这样加载资源:

yourYesResponse = getResources().getText(R.string.good) + usersName + "!";

Niek 更快...

这篇关于如何在android中检查设备语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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