1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-21 20:02:07 +01:00

Make use of opoc/LanguagePreference (fixes #169)

This commit is contained in:
Gregor Santner 2017-09-15 19:56:10 +02:00
parent 4ba1b8671d
commit 184de1931e
No known key found for this signature in database
GPG key ID: 7E83A7834AECB009
3 changed files with 38 additions and 17 deletions

View file

@ -32,6 +32,7 @@ android {
buildConfigField "boolean", "IS_TEST_BUILD", "false"
buildConfigField "boolean", "IS_GPLAY_BUILD", "false"
buildConfigField("String[]", "APPLICATION_LANGUAGES", '{' + getUsedAndroidLanguages().collect {"\"${it}\""}.join(",") + '}')
vectorDrawables.useSupportLibrary = true
}
@ -92,6 +93,23 @@ dependencies {
annotationProcessor "com.jakewharton:butterknife-compiler:${version_lib.butterknife}"
}
@SuppressWarnings(["UnnecessaryQualifiedReference", "SpellCheckingInspection"])
static String[] getUsedAndroidLanguages() {
Set<String> langs = new HashSet<>()
new File('.').eachFileRecurse(groovy.io.FileType.DIRECTORIES) {
final foldername = it.name
if (foldername.startsWith('values-') && !it.canonicalPath.contains("build" + File.separator + "intermediates")) {
new File(it.toString()).eachFileRecurse(groovy.io.FileType.FILES) {
if (it.name.toLowerCase().endsWith(".xml") && it.getCanonicalFile().getText('UTF-8').contains("<string")) {
langs.add(foldername.replace("values-", ""))
}
}
}
}
return langs.toArray(new String[langs.size()])
}
// #####################
// Groovy Coding Area
// #####################

View file

@ -39,10 +39,8 @@ static String[] getUsedAndroidLanguages() {
* Summary: Change language of this app. Restart app for changes to take effect
* Define element in Preferences-XML:
<!--suppress AndroidDomInspection -->
<net.gsantner.opoc.ui.LanguagePreference
android:icon="@drawable/ic_language_black_24dp"
android:defaultValue=""
android:key="@string/pref_key__language"
android:summary="@string/pref_desc__language"
android:title="@string/pref_title__language"/>
@ -55,18 +53,25 @@ import android.os.Build;
import android.preference.ListPreference;
import android.util.AttributeSet;
import net.gsantner.opoc.util.ContextUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import net.gsantner.opoc.util.ContextUtils;
/**
* A {@link android.preference.ListPreference} that displays a list of languages to select from
*/
@SuppressWarnings({"unused", "SpellCheckingInspection"})
@SuppressWarnings({"unused", "SpellCheckingInspection", "WeakerAccess"})
public class LanguagePreference extends ListPreference {
private static final String SYSTEM_LANGUAGE_CODE = "";
public static String SYSTEM_LANGUAGE_NAME = "System";
// The language of res/values/ -> (usually English)
public static String DEFAULT_LANGUAGE_NAME = "English";
public static String DEFAULT_LANGUAGE_CODE = "en";
public LanguagePreference(Context context) {
super(context);
init(context, null);
@ -99,13 +104,15 @@ public class LanguagePreference extends ListPreference {
}
private void init(Context context, AttributeSet attrs) {
setDefaultValue(SYSTEM_LANGUAGE_CODE);
// Fetch readable details
ContextUtils ContextUtils = new ContextUtils(context);
ContextUtils contextUtils = new ContextUtils(context);
List<String> languages = new ArrayList<>();
Object bcof = ContextUtils.getBuildConfigValue("APPLICATION_LANGUAGES");
Object bcof = contextUtils.getBuildConfigValue("APPLICATION_LANGUAGES");
if (bcof instanceof String[]) {
for (String langId : (String[]) bcof) {
Locale locale = ContextUtils.getLocaleByAndroidCode(langId);
Locale locale = contextUtils.getLocaleByAndroidCode(langId);
languages.add(summarizeLocale(locale) + ";" + langId);
}
}
@ -120,10 +127,10 @@ public class LanguagePreference extends ListPreference {
entries[i + 2] = languages.get(i).split(";")[0];
entryval[i + 2] = languages.get(i).split(";")[1];
}
entries[0] = "System";
entryval[0] = "";
entries[1] = "English";
entryval[1] = "en";
entries[0] = SYSTEM_LANGUAGE_NAME;
entryval[0] = SYSTEM_LANGUAGE_CODE;
entries[1] = DEFAULT_LANGUAGE_NAME;
entryval[1] = DEFAULT_LANGUAGE_CODE;
setEntries(entries);
setEntryValues(entryval);

View file

@ -17,16 +17,12 @@
android:summary="@string/pref_desc__sub_nav_slider"
android:title="@string/pref_title__sub_nav_slider"/>
<ListPreference
android:defaultValue=""
android:entries="@array/pref_arrdisp__language"
android:entryValues="@array/pref_arrkeys__language"
<net.gsantner.opoc.ui.LanguagePreference
android:icon="@drawable/ic_language_black_48px"
android:key="@string/pref_key__language"
android:summary="@string/pref_desc__language"
android:title="@string/pref_title__language"/>
<ListPreference
android:dialogTitle="@string/pref_title__font_size"
android:entries="@array/pref_entries__font_size"