0

I have 2 string files to 2 languages in my android app, PT and EN. But I need to know which is the string file in use because I need to add in my SQLite database the current language in use.

Actually, I'm using this code to detect the current language in my SQLite database, but this function only works if the user changes the language manually in config screen. because I don't know how to get the first language selected when the user opens the application in the first time.

if(!dbl.selectIni().getCurrent_lang().equalsIgnoreCase("system")){
  String languageToLoad  = dbl.selectIni().getCurrent_lang();
  Locale locale = new Locale(languageToLoad);
  Locale.setDefault(locale);
  Configuration config = new Configuration();
  config.locale = locale; 
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
FelipeRsN
  • 887
  • 1
  • 9
  • 19

1 Answers1

0

You can use:

Locale.getDefault().getLanguage();

Or use the following code if you want to get the Locale when user change language from setting:

defaultLocale = Resources.getSystem().getConfiguration().locale;

It gets the system locale, no matter which default locale is set for the app/activity. Read https://developer.android.com/reference/android/content/res/Resources.html#getSystem%28%29

But please remember that Resources.getSystem() references to the system resources and might cause a crash if used incorrectly.

For other option you can use the following:

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

From https://stackoverflow.com/a/14389640/4758255

And please be noted that this has been deprecated in the Configuration class, see the latest docs for this advice: locale This field was deprecated in API level 24. Do not set or read this directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96