0

how does the if condition look like?

if(Locale.getDefault().equals("English")){
    Log.i("Language","Englisch");
} else if(Locale.getDefault().equals("Deutsch")){
    Log.i("Language","Deutsch");
} 

This won't work

weston
  • 54,145
  • 21
  • 145
  • 203
FabASP
  • 581
  • 1
  • 6
  • 20
  • 1
    Dont recreate wheel again. Just create file `strings.xml` in `res/values-de` so when you call `getString(R.string.hellow_world)` android will check what is system language and will try load from proper folder in your app. More info here http://developer.android.com/guide/topics/resources/localization.html#using-framework – deadfish Jul 26 '15 at 16:39

2 Answers2

3

Locale.getDefault() will return a static Locale Object, not a String. So calling Locale.getDefault().equals("English") will not work.

Try this:

String language = Locale.getDefault().getLanguage();

if (language.equals("en"))
{
    // Use English link
}
else if (language.equals("de"))
{
    // Use German link
}

public String getLanguage ()

Added in API level 1

Returns the language code for this Locale or the empty string if no language was set.

Or:

String language = Locale.getDefault().getDisplayLanguage();

if (language.equals("English"))
{
    // Use English link
}
else if (...) { ....

public final String getDisplayLanguage ()

Added in API level 1 Equivalent to

getDisplayLanguage(Locale.getDefault()).

Here are other possibilities:

Locale.getDefault().getLanguage()       ---> en      
Locale.getDefault().getISO3Language()   ---> eng 
Locale.getDefault().getCountry()        ---> US 
Locale.getDefault().getISO3Country()    ---> USA 
Locale.getDefault().getDisplayCountry() ---> United States 
Locale.getDefault().getDisplayName()    ---> English (United States) 
Locale.getDefault().toString()          ---> en_US
Locale.getDefault().getDisplayLanguage()---> English

Documentation here.

Community
  • 1
  • 1
pez
  • 3,859
  • 12
  • 40
  • 72
  • I want to use an another link from a webservice, if the language is different. So: if(Language == English){ use the english link } else if(Language == German) { use the german link } And now i ask myself, how will the if condition looks like, anyone an example? – FabASP Jul 26 '15 at 16:36
  • @FabASP See my edit. Do not use `==` for comparing `String` objects. Use `.equals()`. – pez Jul 26 '15 at 16:39
1
  1. Locale.getDefault returns a Locale, not a String. If you want to know more about that locale you call getLanguage and getCountry on the Locale. Look up ISO code for language and country.
  2. Usually, in android you solve something like this by different resources. Under res/values-en/ and res/values-de/ you can define language specific strings.
weston
  • 54,145
  • 21
  • 145
  • 203
Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37