Mishil D. Mishil D. - 1 year ago 75
Java Question

How to check for a device language in android?

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 + "!";

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?

EDIT: Thanks for the help. I appreciate it.

Answer Source

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

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

The string.xml file in values-en:

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

And you load the resource like this:

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

Niek was faster...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download