5

I have a piece of code where I'm trying to change the language in my app, using a Spinner View Component. While working with this, I've come to realize that I'm probably not using the most smooth method, but more intrestingly, I've noticed that two strings that LOOK the same are not equal when compared. Why is it like this, and how should I do if I want to "check" if the language is a certain one?

if (myLocale.toLanguageTag()=="sv") 
{
    //Changing language from Swedish to English
}
else 
{
    Toast.makeText(parent.getContext(),
      myLocale.toString(), Toast.LENGTH_SHORT).show();
   //Here, the toast will tell me what myLocale.toString is "sv", 
   //and so is myLocale.toLanguageTag(). Yet they are not equal...
}
Zerato
  • 683
  • 9
  • 18
  • Are you sure your `myLocale.toLanguageTag()` also returns `sv`? If both of them returns `sv`, they must be equal. Try `myLocale.toLanguageTag().equals("sv")` in the `if` condition. – Niraj Niroula Oct 09 '17 at 18:14
  • Hm, interesting... `if (myLocale.toLanguageTag().equals("sv"))` returns `true`, but `myLocale.toString()` and `myLocale.toLanguageTag()` are both displayed as "sv" in a toast, yet they are not equal. – Zerato Oct 09 '17 at 18:33
  • Would you please explain me, where(in your code) and how they are not equal yet? – Niraj Niroula Oct 09 '17 at 18:51
  • 1
    If you are saying "they are not equal yet" comparing the value of those objects some where in your code then try comparing them this way `myLocale.toLanguageTag().equals(myLocale.to string())`. – Niraj Niroula Oct 09 '17 at 19:00
  • @Journey What I did was this test: `String test1 = myLocale.toLanguageTag(); String test2 = myLocale.toString(); if(test1.equals(test2)) //do something fun if(test1 == test2) //do something more fun` The first one (with `equals`) returned `true` but the one using `==` did not. – Zerato Oct 09 '17 at 21:03
  • 2
    The [reason](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for that. – Niraj Niroula Oct 10 '17 at 05:03
  • In short, `test1` and `test2` are not the reference of same object, only there values are equal. – Niraj Niroula Oct 10 '17 at 09:07

2 Answers2

5

As stated in the documentation:

Locale.toString() returns a string representation of this Locale object, consisting of language, country, variant, script, and extensions, whatever is available, as below:

language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions

Language is always lower case, country is always upper case, script is always title case, and extensions are always lower case.
for example en_US, en

Whereas Locale.toLanguageTag() returns you the same stuff (language, country, and variant), but as a tag. Here Tag means some code given for language, country and variant defined by some IETF's BCP 47 standard (BCP = 'Best Current Practice').
for example en-US
The only difference I can see is the _ and - or perhaps some language/country codes too.

In nutshell, both of them return String; one returns a normal string representation of the Locale whereas the later returns a string as a well-formed IETF BCP 47 language tag representing the locale.

The documentation also suggests using toString only in debugging mode:

This behavior is designed to support debugging and to be compatible with previous uses of toString that expected language, country, and variant fields only. To represent a Locale as a String for interchange purposes, use toLanguageTag().

Hope it helps.

Community
  • 1
  • 1
Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
  • Thank you @Journey for a well-written answer! It makes sense, but I don't understand why this information won't show if I put that string in, say, a TextView or Toast – Zerato Oct 09 '17 at 18:43
  • `Locale.toLanguageTag()` returns the up to date language for Indonesian, `id-ID`, whereas `Locale.toString()` will return `in_ID` for backwards compatability reasons. See https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6457127 – Brett Y Jun 04 '20 at 10:52
-1

Use .equals() for strings comparison. A good explanation can be found here

if ("sv".equals(myLocale.toLanguageTag())) 
Rami Yampolsky
  • 465
  • 4
  • 12