33

I was doing this way:

context.getResources().getConfiguration().locale

Configuration.locale is deprecated if target is 24. So I made this change:

context.getResources().getConfiguration().getLocales().get(0)

Now it says that it's only for minSdkVersion 24, so I cannot use it because my min target is lower.

What's the right method?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user3290180
  • 4,260
  • 9
  • 42
  • 77

4 Answers4

61

Check which version you're running on and fallback to the deprecated solution:

Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = context.getResources().getConfiguration().getLocales().get(0);
} else {
    locale = context.getResources().getConfiguration().locale;
}
Egor
  • 39,695
  • 10
  • 113
  • 130
6

You could use Locale.getDefault(), which is the Java standard way of getting the current Locale.

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
  • 1
    This will only return the locale specified in the system settings. Many apps have their own language selector functionality. If that is the case, then the answer from @Egor is preferred. – Peter Serwylo Mar 22 '17 at 06:20
  • 1
    If you select the locale manually, you should call Locale.setDefault() – ByteWelder Mar 22 '17 at 07:34
  • 1
    This is very important, because otherwise Java libraries won't pick up the correct Locale correctly. – ByteWelder Mar 22 '17 at 08:23
  • Peter states that some apps have their own language selector. If they do, they should call Locale.setDefault as Ken states. So once again this solution will work. This should be the preferred answer. – LanDenLabs Apr 03 '19 at 14:34
6

Here's a one-liner that uses the ConfigurationCompat class:

ConfigurationCompat.getLocales(context.getResources().getConfiguration()).get(0)
veyndan
  • 382
  • 5
  • 9
3

In Configuration.java, there is:

/**
 * ...
 * @deprecated Do not set or read this directly. Use {@link #getLocales()} and
 * {@link #setLocales(LocaleList)}. If only the primary locale is needed,
 * <code>getLocales().get(0)</code> is now the preferred accessor.
 */
@Deprecated public Locale locale;
...
configOut.mLocaleList = LocaleList.forLanguageTags(localesStr);
configOut.locale = configOut.mLocaleList.get(0);

So basically using locale basically returns the primary locale the user sets. The accept answer does exactly the same as reading locale directly.

However this locale isn't necessarily the one used when getting resources. It might be the user's secondary locale if the primary locale isn't available.

Here's a more correct version:

Resources resources = context.getResources();
Locale locale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
        ? resources.getConfiguration().getLocales()
            .getFirstMatch(resources.getAssets().getLocales())
        : resources.getConfiguration().locale;
Mygod
  • 2,077
  • 1
  • 19
  • 43