Skip to content

Commit

Permalink
Internal language selector
Browse files Browse the repository at this point in the history
Instead of relying on the device language, add ability to set the
language in the app.
  • Loading branch information
gzsombor committed Aug 28, 2024
1 parent 2cf6029 commit f0283ea
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ android {
viewBinding true
buildConfig true
}
androidResources {
generateLocaleConfig true
}
lint {
abortOnError false
checkReleaseBuilds false
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@
<meta-data
android:name="com.samsung.android.multidisplay.keep_process_alive"
android:value="true" />
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
package free.rm.skytube.gui.fragments.preferences;

import android.content.SharedPreferences;
import android.content.res.XmlResourceParser;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.os.LocaleListCompat;
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;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import free.rm.skytube.BuildConfig;
import free.rm.skytube.R;
Expand All @@ -44,7 +57,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// if we are running an OSS version, then remove the last option (i.e. the "official" player
// option)
if (BuildConfig.FLAVOR.equals("oss")) {
final ListPreference videoPlayersListPref = (ListPreference) getPreferenceManager().findPreference(getString(R.string.pref_key_choose_player));
final ListPreference videoPlayersListPref = getPreferenceManager().findPreference(getString(R.string.pref_key_choose_player));
final CharSequence[] videoPlayersList = videoPlayersListPref.getEntries();
CharSequence[] modifiedVideoPlayersList = Arrays.copyOf(videoPlayersList, videoPlayersList.length - 1);

Expand All @@ -58,6 +71,43 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
});

configureCountrySelector();
configureLanguageSelector();
}

private List<String> getLanguages() {
List<String> 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<String> 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<size;i++) {
Locale locale = localeList.get(i);
localeNames[i] = locale.getDisplayName();
localeCodes[i] = locale.getLanguage();
}
languageSelector.setEntries(localeNames);
languageSelector.setEntryValues(localeCodes);
}

private void configureCountrySelector() {
Expand All @@ -79,5 +129,10 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
NewPipeService.setCountry(newCountry);
EventBus.getInstance().notifyMainTabChanged(EventBus.SettingChange.CONTENT_COUNTRY);
}
if (getString(R.string.pref_key_app_language).equals(key)) {
String newLanguage = sharedPreferences.getString(key, null);
LocaleListCompat appLocale = LocaleListCompat.forLanguageTags(newLanguage);
AppCompatDelegate.setApplicationLocales(appLocale);
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/resources.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unqualifiedResLocale=en
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@
<string name="pref_key_default_content_country" translatable="false">pref_key_default_content_country</string>
<string name="pref_title_default_content_country">Default content country</string>
<string name="pref_summary_default_content_country">Show content intended for this country: %s</string>

<string name="pref_key_app_language" translatable="false">pref_key_app_language</string>
<string name="pref_title_app_language">Application Language</string>
<string name="pref_summary_app_language">The language used for all the labels in the application</string>

<!--
SPONSORBLOCK
-->
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preference_video_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
app:entryValues="@array/country_codes"
app:defaultValue=""
app:iconSpaceReserved="false" />
<ListPreference
app:key="@string/pref_key_app_language"
app:title="@string/pref_title_app_language"
app:summary="@string/pref_summary_app_language"
app:defaultValue=""
app:iconSpaceReserved="false"/>

<CheckBoxPreference
app:key="@string/pref_key_use_newer_formats"
Expand Down

0 comments on commit f0283ea

Please sign in to comment.