How to check device language in Android?

I want my application to check what language the phone works in. This if statement should do this:

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 English. Something is wrong with this if statement, and if so, then what?

EDIT: Thanks for the help. I appreciate it.

+5
source share
7 answers

This can be solved 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 en values:

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

And you load the resource as follows:

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

Niek was faster ...

+3
source

To get the device language, you can use this:

 Locale.getDefault().getDisplayLanguage(); 

or,

 Locale.getDefault().getLanguage(); //to get usual language code 
+5
source

For the system language:

 Locale.getDefault().getDisplayLanguage(); 
+5
source

Using:

  Locale.getDefault().getLanguage().contentEquals("en") 

String#equals() not only compares the contents of String, but also checks whether another object is also an instance of String . String#contentEquals() methods compare only the content (character sequence) and does not check if the other object is also an instance of String . It can be anything if it is an implementation of CharSequence or an instance of StringBuffer .

+4
source

Typically, the locale is provided in the following format fr_FR, where the first fr is the language code and the second is the country code, so you should use

 Locale.getDefault().getLanguage().startsWith("fr") 

but Android's way is to use resources

 getString(R.string.hello, userName); 

Edited: Okay, ashamed of me, I didn’t notice that you are calling getLanguage (), but the second part is correct.

+2
source

As everyone already explains, i18N (String XMLs in different languages) must be used to achieve this simple thing, but if you are looking for a user language for some other purpose, then use one of them.

 Locale.getDefault().getLanguage(); 

This will give the language code iso, that is, "de", "ru".

OR

 Resources.getSystem().getConfiguration().locale; 

This returns a global share that provides access only to system resources.

+2
source

The Android method for this, uses xml resources, as described in Localization with resources .

<strong> values ​​/strings.xml:

 <resources> <string name="yes_response">That is great, %s!</string> </resources> 

<strong> pt / strings.xml values:

 <resources> <string name="yes_response">C\'est bon, %s!</string> </resources> 

The 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 Java core:

  • You can place most or all of the contents of your application interface in resource files, as described in this document, and in Provision of resources.
  • The user interface behavior, on the other hand, is controlled by your Java code. For example, if a user enters data that needs to be formatted or sorted differently depending on the language, then you will use Java to process the data programmatically. This document does not describe how to localize your Java code.
+1
source

Source: https://habr.com/ru/post/1205009/


All Articles