Android Question [SOLVED] Hot to get the current language in device

Marcob

Member
Licensed User
Longtime User
I'm developing an app that needs to detect the language currently set in the device.
I tried something like this:

B4X:
Sub GetLanguage As String
    Dim jo As JavaObject
    Dim str As String
    
    str = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
    If str.Length>=2 Then
        str = str.SubString2(0,2).ToLowerCase
    Else
        str = ""
    End If
    Return str
End Sub

Unfortunately, that code doesn't work when the app makes use of values-xx folders with their own strings.xml. In such case, GetLanguage returns the language of the app rather than the device's locale.

According to this discussion https://stackoverflow.com/questions/4212320/get-the-current-language-in-device It seems that a call to:

B4X:
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());

should fix the problem.
How could that be translated to B4A by using JavaObject/Reflection?
 

Marcob

Member
Licensed User
Longtime User
Why use strings resources?

I took a look at B4XLocalizator, thank you for your suggestion.
Unfortunately, I have several old apps that already uses strings.xml files in multiple languages which need now to get the device's locate for an additional feature.
I think it could be simpler to just implement in B4a the method I mentioned rather than using B4XLocalizator and completely rewrite the language management sections of all the apps.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim compat As JavaObject
compat.InitializeStatic("android/support/v4/os/ConfigurationCompat".Replace("/", "."))
Dim Resources As JavaObject
Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethod("getConfiguration", Null)
Dim locales As List = compat.RunMethod("getLocales", Array(Resources))

And:
B4X:
#AdditionalJar: com.android.support:support-compat
 
Upvote 0

Marcob

Member
Licensed User
Longtime User

I tested the code and I found that I can't use locales object as a standard B4A List without getting the error:

java.lang.ClassCastException: android.support.v4.os.LocaleListCompat cannot be cast to java.util.List

What should I do to retrieve data from locales ?
 
Upvote 0

Marcob

Member
Licensed User
Longtime User
This works:

B4X:
Sub GetLanguage2 As String
    Dim str As String
   
    Dim compat As JavaObject
    compat.InitializeStatic("android/support/v4/os/ConfigurationCompat".Replace("/", "."))
    Dim Resources As JavaObject
    Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethod("getConfiguration", Null)
    Dim locales As JavaObject = compat.RunMethod("getLocales", Array(Resources))
    Dim locale As JavaObject = locales.RunMethod("get", Array(0))
    str = locale.RunMethod("getLanguage", Null)
    Return str
End Sub

Thank you very much for your support.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
in my phone above GetLanguage2 sub don't work, here is what I use:
B4X:
Sub GetPhoneLanguage As String
   Dim Resources As JavaObject
   Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethodJO("getConfiguration", Null).RunMethodJO("getLocales", Null)
   Dim Locale As String = Resources.RunMethod("get", Array(0))
   Return Locale
End Sub

Warning: this code works from API 24 and later
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…