Mishil D. Mishil D. - 1 month ago 6
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

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:

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

And you load the resource like this:

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

Niek was faster...

Comments