Make language change stick properly

As seen in https://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself.

Fixes #458.
This commit is contained in:
Daniel Martí 2015-10-22 00:07:36 +02:00
parent 8307015d8d
commit 2ceeda40b7
3 changed files with 36 additions and 19 deletions

View File

@ -91,7 +91,7 @@
<activity <activity
android:name=".FDroid" android:name=".FDroid"
android:launchMode="singleTop" android:launchMode="singleTop"
android:configChanges="keyboardHidden|orientation|screenSize" > android:configChanges="layoutDirection|locale|keyboardHidden|orientation|screenSize" >
<!-- App URLs --> <!-- App URLs -->
<intent-filter> <intent-filter>
@ -300,7 +300,8 @@
<activity <activity
android:name=".privileged.views.InstallConfirmActivity" android:name=".privileged.views.InstallConfirmActivity"
android:label="@string/menu_install" android:label="@string/menu_install"
android:parentActivityName=".FDroid"> android:parentActivityName=".FDroid"
android:configChanges="layoutDirection|locale" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".FDroid" /> android:value=".FDroid" />
@ -309,7 +310,8 @@
android:name=".views.ManageReposActivity" android:name=".views.ManageReposActivity"
android:label="@string/app_name" android:label="@string/app_name"
android:launchMode="singleTask" android:launchMode="singleTask"
android:parentActivityName=".FDroid" > android:parentActivityName=".FDroid"
android:configChanges="layoutDirection|locale" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".FDroid" /> android:value=".FDroid" />
@ -323,12 +325,14 @@
</activity> </activity>
<activity <activity
android:name=".NfcNotEnabledActivity" android:name=".NfcNotEnabledActivity"
android:noHistory="true" /> android:noHistory="true"
android:configChanges="layoutDirection|locale" />
<activity <activity
android:name=".views.RepoDetailsActivity" android:name=".views.RepoDetailsActivity"
android:label="@string/repo_details" android:label="@string/repo_details"
android:parentActivityName=".views.ManageReposActivity" android:parentActivityName=".views.ManageReposActivity"
android:windowSoftInputMode="stateHidden"> android:windowSoftInputMode="stateHidden"
android:configChanges="layoutDirection|locale" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".views.ManageReposActivity" /> android:value=".views.ManageReposActivity" />
@ -338,7 +342,8 @@
android:name=".AppDetails" android:name=".AppDetails"
android:label="@string/app_details" android:label="@string/app_details"
android:exported="true" android:exported="true"
android:parentActivityName=".FDroid" > android:parentActivityName=".FDroid"
android:configChanges="layoutDirection|locale" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".FDroid" /> android:value=".FDroid" />
@ -378,7 +383,8 @@
android:label="@string/search_results" android:label="@string/search_results"
android:exported="true" android:exported="true"
android:launchMode="singleTop" android:launchMode="singleTop"
android:parentActivityName=".FDroid" > android:parentActivityName=".FDroid"
android:configChanges="layoutDirection|locale" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".FDroid" /> android:value=".FDroid" />

View File

@ -66,6 +66,8 @@ public class FDroidApp extends Application {
private static final String TAG = "FDroidApp"; private static final String TAG = "FDroidApp";
private static Locale locale = null;
// for the local repo on this device, all static since there is only one // for the local repo on this device, all static since there is only one
public static int port; public static int port;
public static String ipAddressString; public static String ipAddressString;
@ -145,18 +147,25 @@ public class FDroidApp extends Application {
bssid = ""; bssid = "";
} }
public static void updateLanguage(Context c) { public void updateLanguage() {
SharedPreferences prefs = PreferenceManager Context ctx = getBaseContext();
.getDefaultSharedPreferences(c); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String lang = prefs.getString("language", ""); String lang = prefs.getString("language", "");
updateLanguage(c, lang); locale = Utils.getLocaleFromAndroidLangTag(lang);
applyLanguage();
} }
public static void updateLanguage(Context c, String lang) { private void applyLanguage() {
final Configuration cfg = new Configuration(); Context ctx = getBaseContext();
final Locale newLocale = Utils.getLocaleFromAndroidLangTag(lang); Configuration cfg = new Configuration();
cfg.locale = newLocale == null ? Locale.getDefault() : newLocale; cfg.locale = locale == null ? Locale.getDefault() : locale;
c.getResources().updateConfiguration(cfg, null); ctx.getResources().updateConfiguration(cfg, null);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
applyLanguage();
} }
@TargetApi(9) @TargetApi(9)
@ -172,7 +181,7 @@ public class FDroidApp extends Application {
.penaltyLog() .penaltyLog()
.build()); .build());
} }
updateLanguage(this); updateLanguage();
super.onCreate(); super.onCreate();
// Needs to be setup before anything else tries to access it. // Needs to be setup before anything else tries to access it.

View File

@ -1,5 +1,6 @@
package org.fdroid.fdroid.views.fragments; package org.fdroid.fdroid.views.fragments;
import android.app.Activity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -130,8 +131,9 @@ public class PreferencesFragment extends PreferenceFragment
entrySummary(key); entrySummary(key);
if (changing) { if (changing) {
result |= PreferencesActivity.RESULT_RESTART; result |= PreferencesActivity.RESULT_RESTART;
getActivity().setResult(result); Activity activity = getActivity();
FDroidApp.updateLanguage(this.getActivity()); activity.setResult(result);
((FDroidApp) activity.getApplication()).updateLanguage();
} }
break; break;