diff options
author | Arne Schwabe <arne@rfc2549.org> | 2021-10-13 03:33:28 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2021-10-13 03:34:30 +0200 |
commit | d54b7f0a3528307f4218412037b017f37d924b00 (patch) | |
tree | 7d781298502eb2adc437d591dd9e28ef4fb13a71 /main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java | |
parent | cbb44807587e7e51acd3813099059a94448a6de4 (diff) |
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.
Diffstat (limited to 'main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java')
-rw-r--r-- | main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java | 72 |
1 files changed, 72 insertions, 0 deletions
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()); + } +} |