Try fixing the localization issues

This commit is contained in:
Sebastian Kürten
2021-02-12 16:57:14 +01:00
parent 9d96ce6db0
commit 8a94a1708f
4 changed files with 32 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy; import android.os.StrictMode.ThreadPolicy;
import android.os.StrictMode.VmPolicy; import android.os.StrictMode.VmPolicy;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log;
import com.vanniktech.emoji.EmojiManager; import com.vanniktech.emoji.EmojiManager;
import com.vanniktech.emoji.google.GoogleEmojiProvider; import com.vanniktech.emoji.google.GoogleEmojiProvider;
@@ -96,6 +97,7 @@ public class BriarApplicationImpl extends Application
Localizer.initialize(prefs); Localizer.initialize(prefs);
super.attachBaseContext( super.attachBaseContext(
Localizer.getInstance().setLocale(base)); Localizer.getInstance().setLocale(base));
Localizer.getInstance().setLocale(this);
setTheme(base, prefs); setTheme(base, prefs);
ACRA.init(this); ACRA.init(this);
} }
@@ -145,6 +147,7 @@ public class BriarApplicationImpl extends Application
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
Log.d("language", "BriarApplicationImpl#onConfigurationChanged()");
Localizer.getInstance().setLocale(this); Localizer.getInstance().setLocale(this);
} }

View File

@@ -13,6 +13,7 @@ import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import org.briarproject.bramble.api.account.AccountManager; import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
@@ -160,7 +161,9 @@ public class BriarService extends Service {
@Override @Override
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
Log.d("language", "BriarService#attachBaseContext()");
super.attachBaseContext(Localizer.getInstance().setLocale(base)); super.attachBaseContext(Localizer.getInstance().setLocale(base));
Localizer.getInstance().setLocale(this);
} }
private void showStartupFailureNotification(StartResult result) { private void showStartupFailureNotification(StartResult result) {

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.util.Log;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -26,22 +27,29 @@ public class Localizer {
private Localizer(SharedPreferences sharedPreferences) { private Localizer(SharedPreferences sharedPreferences) {
this(Locale.getDefault(), getLocaleFromTag( this(Locale.getDefault(), getLocaleFromTag(
sharedPreferences.getString(LANGUAGE, "default"))); sharedPreferences.getString(LANGUAGE, "default")));
Log.d("language", "preference: " +
sharedPreferences.getString(LANGUAGE, "default"));
} }
private Localizer(Locale systemLocale, @Nullable Locale userLocale) { private Localizer(Locale systemLocale, @Nullable Locale userLocale) {
this.systemLocale = systemLocale; this.systemLocale = systemLocale;
if (userLocale == null) locale = systemLocale; if (userLocale == null) locale = systemLocale;
else locale = userLocale; else locale = userLocale;
Log.d("language", "Localizer() system locale: " + systemLocale);
Log.d("language", "Localizer() current locale: " + locale);
} }
// Instantiate the Localizer. // Instantiate the Localizer.
public static synchronized void initialize(SharedPreferences prefs) { public static synchronized void initialize(SharedPreferences prefs) {
Log.d("language", "Localizer#initialize()");
Log.d("language", "sdk: " + SDK_INT);
if (INSTANCE == null) if (INSTANCE == null)
INSTANCE = new Localizer(prefs); INSTANCE = new Localizer(prefs);
} }
// Reinstantiate the Localizer with the system locale // Reinstantiate the Localizer with the system locale
public static synchronized void reinitialize() { public static synchronized void reinitialize() {
Log.d("language", "Localizer#reinitialize()");
if (INSTANCE != null) if (INSTANCE != null)
INSTANCE = new Localizer(INSTANCE.systemLocale, null); INSTANCE = new Localizer(INSTANCE.systemLocale, null);
} }
@@ -72,17 +80,20 @@ public class Localizer {
public Context setLocale(Context context) { public Context setLocale(Context context) {
Resources res = context.getResources(); Resources res = context.getResources();
Configuration conf = res.getConfiguration(); Configuration conf = res.getConfiguration();
Locale currentLocale; Locale currentLocale;
if (SDK_INT >= 24) { if (SDK_INT >= 24) {
currentLocale = conf.getLocales().get(0); currentLocale = conf.getLocales().get(0);
} else } else
currentLocale = conf.locale; currentLocale = conf.locale;
Log.d("language", "current locale: " + currentLocale);
if (locale.equals(currentLocale)) if (locale.equals(currentLocale))
return context; return context;
Log.d("language", "set locale: " + locale);
Locale.setDefault(locale); Locale.setDefault(locale);
if (SDK_INT >= 17) { if (SDK_INT >= 17) {
conf.setLocale(locale); conf.setLocale(locale);
context.createConfigurationContext(conf); context = context.createConfigurationContext(conf);
} else } else
conf.locale = locale; conf.locale = locale;
//noinspection deprecation //noinspection deprecation

View File

@@ -2,6 +2,7 @@ package org.briarproject.briar.android.activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams;
@@ -36,6 +37,7 @@ import javax.inject.Inject;
import androidx.annotation.LayoutRes; import androidx.annotation.LayoutRes;
import androidx.annotation.UiThread; import androidx.annotation.UiThread;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@@ -106,12 +108,24 @@ public abstract class BaseActivity extends AppCompatActivity
for (ActivityLifecycleController alc : lifecycleControllers) { for (ActivityLifecycleController alc : lifecycleControllers) {
alc.onActivityCreate(this); alc.onActivityCreate(this);
} }
Log.d("language", "BaseActivity#onCreate()");
Context baseContext = getBaseContext();
Log.d("language", "Context: " + getBaseContext().toString());
if (baseContext instanceof ContextThemeWrapper) {
ContextThemeWrapper wrapper = (ContextThemeWrapper) baseContext;
Context wrapped = wrapper.getBaseContext();
Log.d("language", "Wrapped: " + wrapped.toString());
}
} }
@Override @Override
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
Log.d("language", "BaseActivity#attachBaseContext()");
Log.d("language", "Context: " + base.toString());
super.attachBaseContext( super.attachBaseContext(
Localizer.getInstance().setLocale(base)); Localizer.getInstance().setLocale(base));
Localizer.getInstance().setLocale(this);
} }
public ActivityComponent getActivityComponent() { public ActivityComponent getActivityComponent() {