Android Question Retrieving Phone or Tablet Language

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I am trying to get the two letter code of the language used by the Tablet or Phone (EN) (PT) (ES), etc
My device, a Samsung TabA is in Portuguese wirt all texts and messages also in Portuguese, but I always get EN, US, EN-US. Any idea why ?

B4X:
Sub Get_Language As String

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

End Sub

or

B4X:
Sub GetDefaultLanguage As String
 
Dim r As Reflector
    r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)

    Log(r.RunMethod("getDisplayName")) '
    Log(r.RunMethod("getCountry"))
    Log(r.RunMethod("getLanguage"))
  
End Sub
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Pedro the correct result on my phone is:

Logger connected to: samsung SM-J730F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Nederlands (Nederland)
NL
nl

** Activity (main) Resume **

So it looks that it works as expected
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
@Pedro Caldeira did restarting solve the problem or did it not help? In the latter case, there are some challenges why localization does not always work well or differently between different Java versions.
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
@Pedro Caldeira did restarting solve the problem or did it not help? In the latter case, there are some challenges why localization does not always work well or differently between different Java versions.

Well it works on other devices, so it must be related to Samsung specific builds, maybe, since in other tablets with the same Android version (Version 9), works just fine.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Yes localization is a real mine field. As alway there are many ways to solve the challenges. What is the result of this approach on both Samsung and your other devices?

1588158253137.png


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    Private NativeMe As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    
    Activity.LoadLayout("main")
    If FirstTime Then
        NativeMe.InitializeContext
    End If

    Dim s As String = NativeMe.RunMethod("Test", Null)
    Log("Test = " & s)
    EditText1.Text = s

'    Dim s As String = NativeMe.RunMethod("displayDate", Null)
'    Log("Test = " & s)
'    EditText1.Text = s
    
        
End Sub

#IF JAVA

import java.util.*;
import java.text.*;

    static public String displayDate() {

        Locale currentLocale = Locale.getDefault();
        Date today;
        String dateOut;
        DateFormat dateFormatter;

        dateFormatter = 
         DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
        today = new Date();
        dateOut = dateFormatter.format(today);

        return (dateOut + "   " + currentLocale.toString());
   }


#End If

#IF JAVA

    // Access to the main API for dates, times, instants, and durations.
    import java.time.*;

    import java.lang.*;
    import java.security.*;
    
    // Needed for date format
    import java.util.*;
    import java.text.*;
    
    //import java.text.spi.*;    // doesn't exist
    //import java.util.spi;        // doesn't exist
    import java.time.*;
    
    /* Contains the collections framework, legacy collection classes, event model, date and time facilities, 
       internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator,
       and a bit array). */
    
    import java.util.*;

    public static String Test() { //<-- static method
        
        String s, t;
        s = "\n\n";
        try {

            //  Maintain backwards compatibility of legacy code
            // System.setProperty("java.locale.providers", "COMPAT,CLDR");
                
            Locale currentLocale = Locale.getDefault();
            Date today;
            String dateOut;
            DateFormat dateFormatter;

            dateFormatter = 
             DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
            today = new Date();
            dateOut = dateFormatter.format(today);
            
            t = Locale.getDefault().getCountry();
            s = s + "\n" + "\n" + ("The following " + t);

            t = (dateOut + " with the Locales:  " + currentLocale.toString());
            s = s + (" properties are detected on: " + t);
            
            t = System.getProperty("user.language", "not found!");
            s = s + "\n" + "\n" + ("  The language is : " + t); 
            
            t = System.getProperty("user.script", "not found!");
            s = s + "\n" + "\n" + ("  The script is : " + t); 
            
            t = System.getProperty("user.country", "not found!");
            s = s + "\n" + "\n" + ("  The country is : " + t); 
            
            t = System.getProperty("user.variant", "not found!");
            s = s + "\n" + "\n" + ("  The variant is : " + t); 
            
            
            t = System.getProperty("os.name", "not specified");
            s = s + "\n" + "\n" + ("  The name of your operating system is: " + t);

            t = System.getProperty("java.version", "not specified");
            s = s + "\n" + "\n" + ("  The running Java version: " + t);

            t = System.getProperty("user.home", "not specified");
            s = s + "\n" + "\n" + ("  Your user home directory is: " + t);

            t = System.getProperty("java.home", "not specified");
            s = s + "\n" + "\n" + ("  Your JRE installation directory is: " + t);

            return s;
            
        } catch (Exception e) {
            return(s + "\n" + "\n" + "Caught exception " + e.toString());
        }
    }

#End If
 
Upvote 0
Top