Skip to content

Commit

Permalink
#21: Added more inline doc for localisation. Backport from k3b/ToGoZip#6
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Jul 18, 2017
1 parent c8d1581 commit d703685
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/de/k3b/android/androFotoFinder/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Global {
/** LOG_CONTEXT is used as logging source for filtering logging messages that belong to this */
public static final String LOG_CONTEXT = "k3bFoto";

/** local settings: which language should the gui use */
public static final String PREF_KEY_USER_LOCALE = "user_locale";

/**
Expand Down Expand Up @@ -133,6 +134,7 @@ public static void debugMemory(String modul, String message) {
}
}

/** Remember ininial language settings. This allows setting "switch back to device language" after changing app locale */
public static Locale systemLocale = Locale.getDefault();

/** move some pre-defined menu-actions into the "more..." submenue */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
public class SettingsActivity extends PreferenceActivity {
private static Boolean sOldEnableNonStandardIptcMediaScanner = null;
private SharedPreferences prefsInstance = null;
private ListPreference defaultLocalePreference;
private ListPreference defaultLocalePreference; // #21: Support to change locale at runtime
private ListPreference mediaUpdateStrategyPreference;

private int INSTALL_REQUEST_CODE = 1927;

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixLocale(this);
LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)){
Expand All @@ -74,9 +74,9 @@ protected void onCreate(final Bundle savedInstanceState) {
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);

defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down Expand Up @@ -119,6 +119,8 @@ public boolean onPreferenceClick(Preference preference) {
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}

Expand Down Expand Up @@ -350,6 +352,8 @@ public static void show(Activity parent) {
Intent intent = new Intent(parent, SettingsActivity.class);
parent.startActivity(intent);
}

// #21: Support to change locale at runtime
// This is used to show the status of some preference in the description
private void updateSummary() {
final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, "");
Expand All @@ -359,6 +363,7 @@ private void updateSummary() {
about.setTitle(AboutDialogPreference.getAboutTitle(this));
}

// #21: Support to change locale at runtime
private void setLanguage(String languageKey) {
setPref(languageKey, defaultLocalePreference, R.array.pref_locale_names);
}
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/de/k3b/android/widget/LocalizedActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2015 by k3b.
* Copyright (c) 2015-2017 by k3b.
*
* This file is part of AndroFotoFinder.
* This file is part of AndroFotoFinder and of ToGoZip.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
Expand All @@ -19,12 +19,13 @@

package de.k3b.android.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;

Expand All @@ -39,7 +40,6 @@
*
* Created by k3b on 07.01.2016.
*/

public abstract class LocalizedActivity extends Activity {
/** if myLocale != Locale.Default : activity must be recreated in on resume */
private Locale myLocale = null;
Expand All @@ -57,7 +57,7 @@ protected void onResume() {
// Locale has changed by other Activity ?
if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) {
myLocale = null;
recreate();
recreate(LocalizedActivity.this);
}
}

Expand All @@ -71,7 +71,7 @@ public static void fixLocale(Context context)
.getDefaultSharedPreferences(context);
String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
Locale locale = Global.systemLocale; // in case that setting=="use android-locale"
if ((language != null) && (!language.isEmpty())) {
if ((language != null) && (language.length() > 0)) {
locale = new Locale(language); // overwrite "use android-locale"
}

Expand All @@ -93,9 +93,15 @@ public static void fixLocale(Context context)
public static void recreate(Activity child) {
Activity context = child;
while (context != null) {
context.recreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
context.recreate();
} else {
// https://stackoverflow.com/questions/11495130/android-recreate-functions-in-api-7
context.startActivity(new Intent(context, context.getClass()));
context.finish();
}
context = context.getParent();
}

}

}
2 changes: 1 addition & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<item>XC</item>
</string-array>

<!-- which gui language to use ? -->
<!-- #21: which gui language to use ? pref_locale_names must correspond with pref_locale_values -->
<string-array name="pref_locale_names" translatable="false">
<item>@string/settings_locale_os_language</item>
<item>العربية (Arabic)</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:title="@string/settings_title"
android:key="pref_category_first">

<!-- #21: Support to change locale at runtime -->
<ListPreference
android:key="user_locale"
android:title="@string/settings_locale_title"
Expand Down

0 comments on commit d703685

Please sign in to comment.