Background
I have created two translations - values-en
and values-fr
- for my app. I want to let user change app language in UI. So I created and filled a spinner with available locales of system, using Locale.getAvailableLocales();
method:
Spinner languageSpinner = (Spinner)findViewById(R.id.spinnerLanguage);
Locale[] locales = Locale.getAvailableLocales();
String[] langs = new String[locales.length];
for (int i = 0 ; i < locales.length ; i++ ){
// display a more readable language name like "English (USA)"
langs[i] = locales[i].getDisplayName();
}
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,langs);
languageSpinner.setAdapter(adapter);
Above code will give me all of available locales in current device. Since this is a long list and might be confusing for my users, I want to display Locales that I have provided with translation.
EDIT: A similar question is asked here, comments suggest that there is no direct way.
Although I can provide hard-coded data to my spinner, it would be a better approach to make whole process dynamic.
Question
Is there a way to get available translations in run-time?
An ideal method example would be: getTranslationList();
which eventually return an array of available translated languages in resources, something like: {"en","fr"}
or {"values", "values-en", "values-fr"}
.