diff --git a/app/src/main/java/free/rm/skytube/gui/fragments/preferences/VideoPlayerPreferenceFragment.java b/app/src/main/java/free/rm/skytube/gui/fragments/preferences/VideoPlayerPreferenceFragment.java index 67dff8298..5fff26ab3 100644 --- a/app/src/main/java/free/rm/skytube/gui/fragments/preferences/VideoPlayerPreferenceFragment.java +++ b/app/src/main/java/free/rm/skytube/gui/fragments/preferences/VideoPlayerPreferenceFragment.java @@ -23,11 +23,9 @@ import androidx.appcompat.app.AppCompatDelegate; import androidx.core.os.LocaleListCompat; +import androidx.core.util.Pair; import androidx.preference.ListPreference; import androidx.preference.Preference; -import androidx.preference.PreferenceFragmentCompat; - -import com.google.common.base.Joiner; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -35,6 +33,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -74,41 +73,51 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { configureLanguageSelector(); } - private List getLanguages() { - List result = new ArrayList<>(); - try { - XmlResourceParser xpp = getResources().getXml(R.xml._generated_res_locale_config); - while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) { - if (xpp.getEventType() == XmlPullParser.START_TAG) { - if ("locale".equals(xpp.getName()) && xpp.getAttributeCount() > 0 && "name".equals(xpp.getAttributeName(0))) { - result.add(xpp.getAttributeValue(0)); - } - } - xpp.next(); - } - Logger.i(this, "Language list:"+result); - } catch(XmlPullParserException|IOException e) { - Logger.e(this, "Unable to parse locale config: "+e.getMessage(), e); - } - return result; - } - - private void configureLanguageSelector() { - ListPreference languageSelector = findPreference(getString(R.string.pref_key_app_language)); - - List languages = getLanguages(); - LocaleListCompat localeList = LocaleListCompat.forLanguageTags(Joiner.on(",").join(languages)); - int size = localeList.size(); - String[] localeCodes = new String[size]; - String[] localeNames = new String[size]; - for (int i = 0;i getLanguages() { + List result = new ArrayList<>(); + try { + XmlResourceParser xpp = getResources().getXml(R.xml._generated_res_locale_config); + while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) { + if (xpp.getEventType() == XmlPullParser.START_TAG) { + if ("locale".equals(xpp.getName()) && xpp.getAttributeCount() > 0 && "name".equals(xpp.getAttributeName(0))) { + result.add(xpp.getAttributeValue(0)); + } + } + xpp.next(); + } + Logger.i(this, "Language list:"+result); + } catch(XmlPullParserException|IOException e) { + Logger.e(this, "Unable to parse locale config: "+e.getMessage(), e); + } + return result; + } + + private static Comparator> ENGLISH_COMPARATOR = Comparator.comparing(pair -> "en".equals(pair.first) ? 0 : 1); + private static Comparator> COMPARATOR = ENGLISH_COMPARATOR.thenComparing(pair -> pair.second); + + private void configureLanguageSelector() { + ListPreference languageSelector = findPreference(getString(R.string.pref_key_app_language)); + + List languages = getLanguages(); + LocaleListCompat localeListCompat = AppCompatDelegate.getApplicationLocales(); + Locale defaultLocale = localeListCompat.isEmpty() ? Locale.getDefault() : localeListCompat.get(0); + Logger.i(this, "Default locale: " + defaultLocale + " -> language:" + defaultLocale.getLanguage()); + List> languageWithCodes = languages.stream() + .map(code -> Pair.create(code, new Locale(code).getDisplayLanguage(defaultLocale))) + .sorted(COMPARATOR) + .collect(Collectors.toList()); + int size = languageWithCodes.size(); + String[] localeCodes = new String[size]; + String[] localeNames = new String[size]; + for (int i = 0;i locale = languageWithCodes.get(i); + localeNames[i] = locale.second; + localeCodes[i] = locale.first; + } + languageSelector.setEntries(localeNames); + languageSelector.setEntryValues(localeCodes); + languageSelector.setValue(defaultLocale.getLanguage()); + } private void configureCountrySelector() { ListPreference countrySelector = findPreference(getString(R.string.pref_key_default_content_country)); diff --git a/app/src/main/res/values/strings_preferences.xml b/app/src/main/res/values/strings_preferences.xml index 17e4ab357..b8ceac7a7 100644 --- a/app/src/main/res/values/strings_preferences.xml +++ b/app/src/main/res/values/strings_preferences.xml @@ -749,7 +749,7 @@ pref_key_app_language Application Language - The language used for all the labels in the application + The language used for all the labels in the application: %s