Android Question How to find out the installed language?

Midimaster

Active Member
Licensed User
I'm searching the forum now for 20 minutes. but all my keywords seem not to point to a solution...
How do I check the language the user installed on a smartphone? Is there a short code snipplet? I do not want to install a big library like Localizer.
 

Sandman

Expert
Licensed User
Longtime User
For B4A and B4i.

B4X:
Sub getDeviceLanguage
    
    Dim language As String
    
#if B4A

    Dim jo As JavaObject
    language = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)

    If language.Length > 2 Then language = language.Substring2(0, 2)

#Else
    
    Dim no As NativeObject
    language = no.Initialize("NSLocale") _
        .RunMethod("preferredLanguages", Null) _
        .RunMethod("objectAtIndex:", Array(0)) _
        .AsString

    If language.Length > 2 Then language = language.Substring2(0, 2)

#End If
    
    deviceLanguage = language.Replace(separator, "_")
    
End Sub
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Glad to help. That's the exact code I use in my app so it's battle tested in production. :)
 
Upvote 0
Top