country of device (or alternatively language in use)

moster67

Expert
Licensed User
Longtime User
Hi, quick question which I couldn't find an answer to...

is it possible to get the country of the device and/or the language in use?

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can get the selected language with this code (requires Reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetDefaultLanguage)
End Sub

Sub GetDefaultLanguage As String
   Dim r As Reflector
   r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
   Return r.RunMethod("getDisplayName")
End Sub
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
That's pretty much what I need as well.

I want to detect the user language and to check if some of the languages I've translated my app to coincides the current user interface language.

Your reflection method returns full language names. I turned my Phone to Russian and Polish and these are the results:

руский (Польша) which means Russian(Poland)
polski (Rumunia) which means Polish(Romania).

And now, to summarize the question - is this the correct way to do it?

I can easily check if the returned string includes ("english") or (polski) or ("deutsch").

I tried checking this with AHLocale but it doesn't seem to return the current activated language. It just returns the list of available locales. I will not be using this library for the translation processes, I have my own map of strings.

Maybe it would be better if I can get it like this: en, de, bg, gr, etc...

Edit:

I found my mistake, I just had to do initliaze which initializes the locale object with the default language.

But as for the reflection method, what should "getDisplayName" be replaced with?

Edit2:

Solved it with some reading about java.util.Locale.

B4X:
    Return r.RunMethod("getLanguage")
 
Last edited:
Upvote 0
Top