How to get the device locale after changing the application locale

后端 未结 4 1077
滥情空心
滥情空心 2021-01-02 08:36

I am changing the application locale based on user choice. Independent of device locale.

using

public void setDefaultLocale(Context context, String          


        
4条回答
  •  长发绾君心
    2021-01-02 09:00

    If you need to support below Android 7.0 - Nougat (API level 24) you can use something like this:

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    // ToDo: Adjust this method once the target API is 24 or higher!
    private Locale getLocale()
    {
      if(Build.VERSION.SDK_INT >= 24)
      {
        return Resources.getSystem().getConfiguration().getLocales().get(0);
      }
    
      else
      {
        return Resources.getSystem().getConfiguration().locale;
      }
    }
    

    Note the annotation regarding deprecation and NewApi. Adjust this method once you target API 24 or higher. Inspired by this answer.

提交回复
热议问题