Android Question Get_Language not showing full description of devices default language setting

JackKirk

Well-Known Member
Licensed User
Longtime User
In this post:

https://www.b4x.com/android/forum/threads/how-to-get-the-device-locale.81928/post-519035

Erel shows this code to determine an Android device's default language, which I reproduce here:

B4X:
Sub Get_Language As String
   Dim jo As JavaObject
   Return jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
End Sub

If I set my device (Pixel 3) with a default language of "Francais (Canada)" this code produces "fr" instead of "fr_CA".

Does anyone have any suggestions?

Thanks in anticipation...
 
Last edited:

agraham

Expert
Licensed User
Longtime User
That's odd, I get 'en_GB' when I try it. You could try this but I don't expect it to work as I think it is returns just the suffix of the default locale.
B4X:
Sub Get_Country As String
   Dim jo As JavaObject
   jo = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
     Return jo.RunMethod("getCountry", Null)   
End Sub
There are various other methods you could try but they also may not do anything if the default locale does not include a country code.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This is what I get with the code below:

fr
CH
français
français (Suisse)
Suisse
fra
CHE
fr_CH

B4X:
Private Sub GetLanguage
    Private jo As JavaObject
  
    jo = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
  
    Log(jo.RunMethod("getLanguage", Null))
    Log(jo.RunMethod("getCountry", Null))
    Log(jo.RunMethod("getDisplayLanguage", Null))
    Log(jo.RunMethod("getDisplayName", Null))
    Log(jo.RunMethod("getDisplayCountry", Null))
    Log(jo.RunMethod("getISO3Language", Null))
    Log(jo.RunMethod("getISO3Country", Null))
    Log(jo.RunMethod("toString", Null))
End Sub

EDIT: I added Log(jo.RunMethod("toString", Null)) according to posts # 4 and 5.
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I'm embarrassed - the code I actually was having issues with was:
B4X:
            wrk_jo = wrk_jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
            Gen_language = wrk_jo.RunMethod("getLanguage", Null)

which in my manic rush I took to be the same as Erel's code.

What actually works is:
B4X:
            wrk_jo = wrk_jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
            Gen_language = wrk_jo.RunMethod("toString", Null)

The first code just delivers the generic language code ("fr" in Klaus's example).

The second delivers the full localized code ("fr_CH" I suspect for Klaus)

The second is also the equivalent of Erel's code (the "toString" being done when returning the result as string) - so agraham was correct.

Apologies all round...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I confirm, with Log(jo.RunMethod("toString", Null)) I get fr_CH.
I added it into post #3.
Well if this thread achieved nothing else it showed how to get all locale details.:)
 
Upvote 0
Top