[英]How to get supported locales via your app, not the system?
我正在尝试获取该应用程序支持的语言环境列表,但我只得到了一个。
我在清单中使用 android:localeConfig 将支持的语言添加到电话设置中。
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="es"/>
<locale android:name="en"/>
</locale-config>
添加清单:
<manifest
...
<application
...
android:localeConfig="@xml/locales_config">
</application>
</manifest>
稍后使用下一个 function 设置新语言:
fun setLanguage(localeSelected: String){
// Select language, the selected locale; en, es, etc...
if(getPreferredLocaleList().any { it.language == localeSelected }){
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(localeSelected))
}
}
fun getCurrentLanguage() : String {
return Locale.getDefault().language
}
现在我正在这些语言环境之间与 select 进行对话,但我只得到一个,我不知道我做错了什么。
private fun getPreferredLocaleList(): List<Locale> {
val adjustedLocaleListCompat = LocaleListCompat.getAdjustedDefault()
val preferredLocaleList = mutableListOf<Locale>()
for (index in 0 until adjustedLocaleListCompat.size()) {
adjustedLocaleListCompat.get(index)?.let { preferredLocaleList.add(it) }
}
return preferredLocaleList
}
fun showLanguageSelectorDialog(view: View){
// AlertDialog builder instance to build the alert dialog
val alertDialog = AlertDialog.Builder(view.context)
// set the custom icon to the alert dialog
//alertDialog.setIcon(R.drawable.image_logo)
// title of the alert dialog
alertDialog.setTitle(getString(R.string.language_selector))
// list of the items to be displayed to the user in the form of list so that user can select the item from
val listItems: MutableList<String> = emptyList<String>().toMutableList()
getPreferredLocaleList().forEach { locale -> listItems.add(locale.language) }
**var arrayOfListItems = listItems.toTypedArray()
var currentLocale = getCurrentLanguage()
var indexCurrentLocale: Int = arrayOfListItems.indexOf(currentLocale)**
// the function setSingleChoiceItems is the function which builds the alert dialog with the single item selection
alertDialog.setSingleChoiceItems(arrayOfListItems, indexCurrentLocale) { dialog, which ->
// update the selected item which is selected by the user so that it should be selected
// when user opens the dialog next time and pass the instance to setSingleChoiceItems method
indexCurrentLocale = which
// when selected an item the dialog should be closed with the dismiss method
dialog.dismiss()
}
// set the negative button if the user is not interested to select or change already selected item
alertDialog.setNegativeButton(getString(R.string.cancel)) { dialog: DialogInterface?, which: Int -> dialog?.dismiss() }
// create and build the AlertDialog instance with the AlertDialog builder instance
val customAlertDialog = alertDialog.create()
// show the alert dialog when the button is clicked
customAlertDialog.show()
}
谢谢
我没有找到执行此操作的内置方法。 我刚刚通过getResources().getXml(R.xml.locales_config)
解析了locales_config.xml
。
private String[] getLocalesFromResources() {
final List<String> items = new ArrayList<>();
XmlPullParser xpp = getResources().getXml(R.xml.locales_config);
try {
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
String tagName = xpp.getName();
if (xpp.getEventType() == XmlPullParser.START_TAG) {
if ("locale".equals(tagName) && xpp.getAttributeCount() > 0 && xpp.getAttributeName(0).equals("name")) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
return items.toArray(new String[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.