Reputation: 107
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.
Upvotes: 6
Views: 11600
Reputation: 378
As everyone already explain that i18N (String XMLs in different languages) have to be used to achieve this simple thing but if you are looking for user language for some other purpose then please use one of this.
Locale.getDefault().getLanguage();
This will give language iso code i.e. "de", "ru".
OR
Resources.getSystem().getConfiguration().locale;
This returns global shared Resources object that provides access to only system resources.
Upvotes: 4
Reputation: 56
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...
Upvotes: 4
Reputation: 100358
The Android way of doing this, is using xml resources, as explained in Localizing with Resources.
values/strings.xml:
<resources>
<string name="yes_response">That is great, %s!</string>
</resources>
values-fr/strings.xml:
<resources>
<string name="yes_response">C\'est bon, %s!</string>
</resources>
Code:
yourYesResponse = context.getString(R.string.yes_response, usersName);
Motivation:
It is good practice to use the Android resource framework to separate the localized aspects of your application as much as possible from the core Java functionality:
- You can put most or all of the contents of your application's user interface into resource files, as described in this document and in Providing Resources.
- The behavior of the user interface, on the other hand, is driven by your Java code. For example, if users input data that needs to be formatted or sorted differently depending on locale, then you would use Java to handle the data programmatically. This document does not cover how to localize your Java code.
Upvotes: 2
Reputation: 3659
Usually locale is provided with followig format fr_FR where first fr is language code and second one is country code, thats why you should use
Locale.getDefault().getLanguage().startsWith("fr")
but Android way is to use resources
getString(R.string.hello, userName);
Edited: Ok, shame on me, I didn't notice that you call getLanguage(), but second part is correct.
Upvotes: 3
Reputation: 771
In order to get the device language, you can use this:
Locale.getDefault().getDisplayLanguage();
or,
Locale.getDefault().getLanguage(); //to get usual language code
Upvotes: 8
Reputation: 486
Use :
Locale.getDefault().getLanguage().contentEquals("en")
The String#equals()
not only compares the String's contents, but also checks if the other object is also an instance of a String
. The String#contentEquals()
methods only compares the contents (the character sequence) and does not check if the other object is also an instance of String
. It can be anything as long as it is an implementation of CharSequence
or an instance of StringBuffer
.
Upvotes: 7
Reputation: 158
For have the system language:
Locale.getDefault().getDisplayLanguage();
Upvotes: 5