How to get locale with region?

后端 未结 3 1716
不思量自难忘°
不思量自难忘° 2021-02-15 06:30

I am trying to get the current device locale with the region like \"en_us\",\"en_gb\".

I am calling Locale.getDefault().getLanguage() and it returns only th

相关标签:
3条回答
  • 2021-02-15 07:08

    Format like "en_us" or "en_gb" has "language code"_"country code"

    A Locale object contains both country code and language code.

    So you can use below snippet to format your own code..

    String cCode = Locale.getDefault().getCountry();
    String lCode = Locale.getDefault().getLanguage();
    String code = lCode+"_"+cCode;
    

    or

    you can use toString() method on Locale object to get the data

    String code = Locale.getDefault().toString();
    
    0 讨论(0)
  • 2021-02-15 07:22

    The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched. Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated.

    If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e.

    Locale current = getResources().getConfiguration().locale;
    

    You may find that this value is updated more quickly after a settings change if that is necessary for your application.

    i have tested this :)

    i have got from this as link may be deleted so Answer copied :)

    0 讨论(0)
  • 2021-02-15 07:31

    Use

    Locale.getDisplayName();
    

    This is shorthand for

    Locale.getDisplayName(Locale.getDefault());
    

    The documentation is in here:http://developer.android.com/reference/java/util/Locale.html

    0 讨论(0)
提交回复
热议问题