From d54b7f0a3528307f4218412037b017f37d924b00 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 13 Oct 2021 03:33:28 +0200 Subject: Make a toggle to allow community translation (closes #681) Since it is hard for me to check languages other than the ones that I speak myself, languages other than English/German default to off. This might change if I have someone trusted to review other languages. --- .../blinkt/openvpn/core/ICSOpenVPNApplication.java | 17 +++++ .../java/de/blinkt/openvpn/core/LocaleHelper.java | 72 ++++++++++++++++++++++ .../openvpn/core/OpenVpnManagementThread.java | 2 +- main/src/main/res/values-de/bools.xml | 8 +++ main/src/main/res/values/bools.xml | 1 + main/src/main/res/values/strings.xml | 2 + .../de/blinkt/openvpn/activities/BaseActivity.java | 13 ++-- main/src/ui/res/xml/general_settings.xml | 7 +++ 8 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java create mode 100644 main/src/main/res/values-de/bools.xml diff --git a/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java b/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java index 92bfb61f..b8e3d646 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java @@ -10,12 +10,14 @@ import android.app.Application; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Color; import android.os.Build; import android.os.StrictMode; import android.os.strictmode.Violation; +import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import java.util.concurrent.Executors; @@ -35,7 +37,9 @@ public class ICSOpenVPNApplication extends Application { if("robolectric".equals(Build.FINGERPRINT)) return; + LocaleHelper.setDesiredLocale(this); super.onCreate(); + PRNGFixes.apply(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) @@ -48,6 +52,13 @@ public class ICSOpenVPNApplication extends Application { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { AppRestrictions.getInstance(this).checkRestrictions(this); } + + + } + + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(LocaleHelper.updateResources(base)); } private void enableStrictModes() { @@ -74,6 +85,12 @@ public class ICSOpenVPNApplication extends Application { } + @Override + public void onConfigurationChanged(@NonNull Configuration newConfig) { + super.onConfigurationChanged(newConfig); + LocaleHelper.onConfigurationChange(this); + } + @RequiresApi(api = Build.VERSION_CODES.P) public void logViolation(Violation v) { String name = Application.getProcessName(); diff --git a/main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java b/main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java new file mode 100644 index 00000000..2b0c1975 --- /dev/null +++ b/main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2012-2021 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ + +package de.blinkt.openvpn.core; + +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; + +import java.util.Locale; + +public class LocaleHelper { + static private Locale desiredLocale = null; + + public static void setDesiredLocale(Context c) + { + Locale current = Locale.getDefault(); + boolean defForce = true; + if (current.getLanguage().equals(new Locale("de").getLanguage())) + defForce = false; + + boolean allow_translation = Preferences.getDefaultSharedPreferences(c).getBoolean("allow_translation", defForce); + + if (!allow_translation) + desiredLocale = new Locale("en", current.getCountry()); + } + + public static Context updateResources(Context context) { + if (desiredLocale == null) + return context; + + Locale.setDefault(desiredLocale); + + Resources res = context.getResources(); + Configuration config = new Configuration(res.getConfiguration()); + if (Build.VERSION.SDK_INT >= 17) { + config.setLocale(desiredLocale); + context = context.createConfigurationContext(config); + } else { + config.locale = desiredLocale; + res.updateConfiguration(config, res.getDisplayMetrics()); + } + return context; + } + + public static void onConfigurationChange(Context context) + { + Resources res = context.getResources(); + + Locale current; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + current = res.getConfiguration().getLocales().get(0); + else + current = res.getConfiguration().locale; + + + if (current == desiredLocale) + return; + + Configuration config = new Configuration(res.getConfiguration()); + + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) + config.setLocale(desiredLocale); + else + config.locale = desiredLocale; + + res.updateConfiguration(config, res.getDisplayMetrics()); + } +} diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java index e4123838..f72c759f 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVpnManagementThread.java @@ -490,7 +490,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNManagement { if (proxyType == Connection.ProxyType.ORBOT) { VpnStatus.updateStateString("WAIT_ORBOT", "Waiting for Orbot to start", R.string.state_waitorbot, ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET); OrbotHelper orbotHelper = OrbotHelper.get(mOpenVPNService); - if (!orbotHelper.checkTorReceier(mOpenVPNService)) + if (!OrbotHelper.checkTorReceier(mOpenVPNService)) VpnStatus.logError("Orbot does not seem to be installed!"); mResumeHandler.postDelayed(orbotStatusTimeOutRunnable, ORBOT_TIMEOUT_MS); diff --git a/main/src/main/res/values-de/bools.xml b/main/src/main/res/values-de/bools.xml new file mode 100644 index 00000000..4b03945d --- /dev/null +++ b/main/src/main/res/values-de/bools.xml @@ -0,0 +1,8 @@ + + + + true + \ No newline at end of file diff --git a/main/src/main/res/values/bools.xml b/main/src/main/res/values/bools.xml index d38f0c84..9cab9760 100644 --- a/main/src/main/res/values/bools.xml +++ b/main/src/main/res/values/bools.xml @@ -6,4 +6,5 @@ true + false \ No newline at end of file diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index 0fa36c05..3d91c58b 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -510,5 +510,7 @@ Compatibility mode Load OpenSSL legacy provider Profiles uses BF-CBC which depends on OpenSSL legacy provider (not enabled). + Allow community contributed translations + Allows the English to be translated with translations contributed by the community. Requires a restart of the app to activate. diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java b/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java index 0e143042..cca8b155 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java +++ b/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java @@ -6,12 +6,16 @@ package de.blinkt.openvpn.activities; import android.app.UiModeManager; +import android.content.Context; import android.content.res.Configuration; import android.os.Bundle; import android.view.Window; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import de.blinkt.openvpn.core.LocaleHelper; + public abstract class BaseActivity extends AppCompatActivity { boolean isAndroidTV() { final UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE); @@ -29,12 +33,13 @@ public abstract class BaseActivity extends AppCompatActivity { } @Override - protected void onResume() { - super.onResume(); + protected void attachBaseContext(Context base) { + super.attachBaseContext(LocaleHelper.updateResources(base)); } @Override - protected void onPause() { - super.onPause(); + public void onConfigurationChanged(@NonNull Configuration newConfig) { + super.onConfigurationChanged(newConfig); + LocaleHelper.onConfigurationChange(this); } } diff --git a/main/src/ui/res/xml/general_settings.xml b/main/src/ui/res/xml/general_settings.xml index 5c2ff8aa..dcaafde2 100644 --- a/main/src/ui/res/xml/general_settings.xml +++ b/main/src/ui/res/xml/general_settings.xml @@ -13,6 +13,13 @@ android:summary="@string/show_log_summary" android:title="@string/show_log_window"/> + +