B4J Question OS language detection

Solution
You can try this:

B4X:
Public Sub getSystemLanguage As String
    ' Get the default locale of the system
    Dim defaultLocale As JavaObject
    defaultLocale.InitializeStatic("java.util.Locale")
    Dim systemLocale As JavaObject = defaultLocale.RunMethod("getDefault", Null)
   
    ' Get the language code of the default locale
    Return systemLocale.RunMethod("getLanguage", Null)
End Sub

Swissmade

Well-Known Member
Licensed User
Longtime User
Yes there is

Get OS:
OS = GetSystemProperty("os.name", "")
 
Upvote 0

FrostCodes

Active Member
Licensed User
Longtime User
You can try this:

B4X:
Public Sub getSystemLanguage As String
    ' Get the default locale of the system
    Dim defaultLocale As JavaObject
    defaultLocale.InitializeStatic("java.util.Locale")
    Dim systemLocale As JavaObject = defaultLocale.RunMethod("getDefault", Null)
   
    ' Get the language code of the default locale
    Return systemLocale.RunMethod("getLanguage", Null)
End Sub
 
Upvote 0
Solution

Sergio Haurat

Active Member
Licensed User
Longtime User
You can try this:

B4X:
Public Sub getSystemLanguage As String
    ' Get the default locale of the system
    Dim defaultLocale As JavaObject
    defaultLocale.InitializeStatic("java.util.Locale")
    Dim systemLocale As JavaObject = defaultLocale.RunMethod("getDefault", Null)
  
    ' Get the language code of the default locale
    Return systemLocale.RunMethod("getLanguage", Null)
End Sub
This code works perfectly, in my case it returns the variable with the value "es". I want to see if GetSystemProperty("lang", "not found"), or whatever, performs better.
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
Yes there is

Get OS:
OS = GetSystemProperty("os.name", "")
I found on Google, to show all the variables, in the command prompt you need to run:

java -XshowSettings:all

User = GetSystemProperty("user.language", ""), but is user not operating system.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you want the language DisplayName change

B4X:
Return systemLocale.RunMethod("getLanguage", Null)

to

B4X:
Return systemLocale.RunMethod("getDisplayName", Null)

First one gives me 'en'
Second 'English (United Kingdom)'
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
If you want the language DisplayName change

B4X:
Return systemLocale.RunMethod("getLanguage", Null)

to

B4X:
Return systemLocale.RunMethod("getDisplayName", Null)

First one gives me 'en'
Second 'English (United Kingdom)'

Thanks @Daestrum, I need to identify the language with an id, getLanguage is the one I need.
My project is to make a B4X Ping, I have the B4A ready, the B4I halfway since I use @Erel's iPing and I don't know if it works in a class.

In the case of B4J, I did not find a native ping, just like B4A I am going to execute the CMD command with the Shell and capture the result. B4J becomes more complex because I depend on the language in which the operating system is installed.
Google Example for English:
ping paessler.com
Pinging paessler.com [104.16.182.252] with 32 bytes of data:
Reply from 104.16.182.252: bytes=32 time=4ms TTL=57
Reply from 104.16.182.252: bytes=32 time=4ms TTL=57
Reply from 104.16.182.252: bytes=32 time=4ms TTL=57
Reply from 104.16.182.252: bytes=32 time=4ms TTL=57

Ping statistics for 104.16.182.252:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 4ms, Maximum = 4ms, Average = 4ms

My computer in Spanish:
Haciendo ping a www.google.com.ar [172.217.173.227] con 32 bytes de datos:
Respuesta desde 172.217.173.227: bytes=32 tiempo=14ms TTL=116
Respuesta desde 172.217.173.227: bytes=32 tiempo=15ms TTL=116
Respuesta desde 172.217.173.227: bytes=32 tiempo=16ms TTL=116
Respuesta desde 172.217.173.227: bytes=32 tiempo=16ms TTL=116

Estadísticas de ping para 172.217.173.227:
    Paquetes: enviados = 4, recibidos = 4, perdidos = 0
    (0% perdidos),
Tiempos aproximados de ida y vuelta en milisegundos:
    Mínimo = 14ms, Máximo = 16ms, Media = 15ms

To solve this issue, I am making a json file with the different character strings for the RegEx, depending on the language. I will make a post later asking forum users to copy and paste the text in other languages to cover all possible languages.

1719255296009.png


Thank you for your collaboration
 
Upvote 0
Top