diff --git a/assets/Parabaik.ttf b/assets/Parabaik.ttf new file mode 100644 index 000000000..ecf1661a1 Binary files /dev/null and b/assets/Parabaik.ttf differ diff --git a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java index e082b7532..511d7ad4a 100644 --- a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java +++ b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java @@ -133,19 +133,7 @@ public class KiwixMobileActivity extends FragmentActivity implements ActionBar.T // Reset the Locale and change the font of all TextViews and its subclasses, if necessary private void handleLocaleCheck() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - String language = prefs.getString("pref_language_chooser", ""); - - if (language.isEmpty()) { - return; - } - - Locale locale = new Locale(language); - Locale.setDefault(locale); - Configuration config = new Configuration(); - config.locale = locale; - getResources().updateConfiguration(config, getResources().getDisplayMetrics()); - + LanguageUtils.handleLocaleChange(this); new LanguageUtils(this).changeFont(getLayoutInflater()); } diff --git a/src/org/kiwix/kiwixmobile/KiwixSettings.java b/src/org/kiwix/kiwixmobile/KiwixSettings.java index 529d03a95..0763a15b7 100644 --- a/src/org/kiwix/kiwixmobile/KiwixSettings.java +++ b/src/org/kiwix/kiwixmobile/KiwixSettings.java @@ -1,6 +1,7 @@ package org.kiwix.kiwixmobile; import android.app.Activity; +import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.preference.EditTextPreference; @@ -8,6 +9,7 @@ import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceFragment; +import android.util.Log; import java.util.Locale; @@ -44,10 +46,12 @@ public class KiwixSettings extends Activity { prefList.setDefaultValue(prefList.getEntryValues()[0]); String summary = prefList.getValue(); + if (summary == null) { prefList.setValue((String) prefList.getEntryValues()[0]); summary = prefList.getValue(); } + prefList.setSummary(prefList.getEntries()[prefList.findIndexOfValue(summary)]); prefList.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @@ -69,6 +73,8 @@ public class KiwixSettings extends Activity { LanguageUtils languageUtils = new LanguageUtils(getActivity()); + languageList.setTitle(Locale.getDefault().getDisplayLanguage()); + languageList.setEntries(languageUtils.getValues().toArray(new String[0])); languageList.setEntryValues(languageUtils.getKeys().toArray(new String[0])); @@ -79,10 +85,13 @@ public class KiwixSettings extends Activity { public boolean onPreferenceChange(Preference preference, Object newValue) { if (!newValue.equals(Locale.getDefault().toString())) { + + LanguageUtils.handleLocaleChange(getActivity(), newValue.toString()); // Request a restart when the user returns to the Activity, that called this Activity setResult(RESULT_RESTART); + finish(); + startActivity(new Intent(getActivity(), KiwixSettings.class)); } - return true; } }); diff --git a/src/org/kiwix/kiwixmobile/LanguageUtils.java b/src/org/kiwix/kiwixmobile/LanguageUtils.java index 27abe5d37..6b937e7c4 100644 --- a/src/org/kiwix/kiwixmobile/LanguageUtils.java +++ b/src/org/kiwix/kiwixmobile/LanguageUtils.java @@ -2,12 +2,13 @@ package org.kiwix.kiwixmobile; import android.content.Context; import android.content.SharedPreferences; -import android.graphics.Color; +import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Handler; import android.preference.PreferenceManager; import android.util.AttributeSet; import android.util.Log; +import android.util.TypedValue; import android.view.InflateException; import android.view.LayoutInflater; import android.view.View; @@ -17,6 +18,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -43,6 +45,27 @@ public class LanguageUtils { sortLanguageList(); } + public static void handleLocaleChange(Context context) { + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String language = prefs.getString("pref_language_chooser", ""); + + if (language.isEmpty()) { + return; + } + + handleLocaleChange(context, language); + } + + public static void handleLocaleChange(Context context, String language) { + + Locale locale = new Locale(language); + Locale.setDefault(locale); + Configuration config = new Configuration(); + config.locale = locale; + context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); + } + // Read the language codes, that are supported in this app from the locales.txt file private void getLanguageCodesFromAssets() { @@ -126,6 +149,11 @@ public class LanguageUtils { } } + private boolean isLanguageSet() { + + return false; + } + // Get a list of all the language names public List getValues() { @@ -177,8 +205,14 @@ public class LanguageUtils { final View view = inflater.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { - ((TextView) view).setTypeface(Typeface.createFromAsset( - mContext.getAssets(), "DejaVuSansCondensed.ttf")); + TextView textView = ((TextView) view); + + // Set the custom typeface + textView.setTypeface(Typeface.createFromAsset( + mContext.getAssets(), getTypeface())); + + // Reduce the text size + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getTextSize() - 3f); } }); @@ -192,6 +226,25 @@ public class LanguageUtils { } return null; } + + // This method will determine which font will be applied to the not-supported-locale. + // You can define exceptions to the default DejaVu font in the 'exceptions' Hashmap: + // Key: the language code; Value: the name of the font. + // The font has to be placed in the assets folder. + private String getTypeface() { + + // Define the exceptions to the rule + HashMap exceptions = new HashMap(); + exceptions.put("my", "Parabaik.ttf"); + + // Check, if an exception applies to our current locale + if (exceptions.containsKey(Locale.getDefault().getLanguage())) { + return exceptions.get(Locale.getDefault().getLanguage()); + } + + // Return the default font + return "DejaVuSansCondensed.ttf"; + } } private class LanguageContainer { @@ -205,7 +258,13 @@ public class LanguageUtils { // possible incompatibilities, since not all language names are available in all languages. private LanguageContainer(String languageCode) { mLanguageCode = languageCode; - mLanguageName = new Locale(languageCode).getDisplayLanguage(new Locale("en")); + mLanguageName = new Locale(languageCode).getDisplayLanguage(); + + // Use the English name of the language, if the language name is not + // available in the current Locale + if (mLanguageName.length() == 2) { + mLanguageName = new Locale(languageCode).getDisplayLanguage(new Locale("en")); + } } public String getLanguageCode() {