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.
在我的Android应用程序,PT和EN中,我有2个字符串文件到2种语言。但我需要知道哪个是正在使用的字符串文件,因为我需要在我的SQLite数据库中添加当前使用的语言。
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.
实际上,我正在使用此代码来检测我的SQLite数据库中的当前语言,但此功能仅在用户在配置屏幕中手动更改语言时才有效。因为我不知道如何在用户第一次打开应用程序时选择第一种语言。
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());
}
1 个解决方案
#1
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
无论为app / activity设置哪个默认语言环境,它都会获得系统区域设置。阅读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.
但请记住,Resources.getSystem()引用了系统资源,如果使用不当可能会导致崩溃。
For other option you can use the following:
对于其他选项,您可以使用以下内容:
Locale current = getResources().getConfiguration().locale;
From https://*.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
.
请注意,这已在Configuration类中弃用,请参阅此建议的最新文档:locale此字段在API级别24中已弃用。请勿直接设置或读取此字段。使用getLocales()和setLocales(LocaleList)。如果只需要主要语言环境,则getLocales()。get(0)现在是首选访问者。
#1
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
无论为app / activity设置哪个默认语言环境,它都会获得系统区域设置。阅读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.
但请记住,Resources.getSystem()引用了系统资源,如果使用不当可能会导致崩溃。
For other option you can use the following:
对于其他选项,您可以使用以下内容:
Locale current = getResources().getConfiguration().locale;
From https://*.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
.
请注意,这已在Configuration类中弃用,请参阅此建议的最新文档:locale此字段在API级别24中已弃用。请勿直接设置或读取此字段。使用getLocales()和setLocales(LocaleList)。如果只需要主要语言环境,则getLocales()。get(0)现在是首选访问者。