[英]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.