Brasil language, better working Locale setting

This commit is contained in:
Milos Kozak 2019-10-25 11:38:23 +02:00
parent 776e1361df
commit f399f8bb1f
11 changed files with 60 additions and 102 deletions

View file

@ -78,7 +78,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
super.onCreate(savedInstanceState);
Iconify.with(new FontAwesomeModule());
LocaleHelper.onCreate(this, "en");
LocaleHelper.INSTANCE.update(getApplicationContext());
setContentView(R.layout.activity_main);
setSupportActionBar(findViewById(R.id.toolbar));
@ -117,24 +117,6 @@ public class MainActivity extends NoSplashAppCompatActivity {
VersionCheckerUtilsKt.triggerCheckVersion();
FabricPrivacy.setUserStats();
}
private void checkPluginPreferences(ViewPager viewPager) {
if (pluginPreferencesMenuItem == null) return;
if (((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem()).getPreferencesId() != -1)
pluginPreferencesMenuItem.setEnabled(true);
else pluginPreferencesMenuItem.setEnabled(false);
}
@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
actionBarDrawerToggle.syncState();
}
@Override
protected void onResume() {
super.onResume();
setupTabs();
setupViews();
@ -143,8 +125,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
.toObservable(EventRebuildTabs.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
String lang = SP.getString(R.string.key_language, "en");
LocaleHelper.setLocale(getApplicationContext(), lang);
LocaleHelper.INSTANCE.update(getApplicationContext());
if (event.getRecreate()) {
recreate();
} else {
@ -175,9 +156,22 @@ public class MainActivity extends NoSplashAppCompatActivity {
}
}
private void checkPluginPreferences(ViewPager viewPager) {
if (pluginPreferencesMenuItem == null) return;
if (((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem()).getPreferencesId() != -1)
pluginPreferencesMenuItem.setEnabled(true);
else pluginPreferencesMenuItem.setEnabled(false);
}
@Override
public void onPause() {
super.onPause();
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
actionBarDrawerToggle.syncState();
}
@Override
public void onDestroy() {
super.onDestroy();
disposable.clear();
}

View file

@ -35,7 +35,9 @@ import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.constraints.dstHelper.DstHelperPlugin;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin;
import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin;
import info.nightscout.androidaps.plugins.constraints.storage.StorageConstraintPlugin;
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerPlugin;
import info.nightscout.androidaps.plugins.general.actions.ActionsPlugin;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin;
@ -48,9 +50,7 @@ import info.nightscout.androidaps.plugins.general.nsclient.receivers.AckAlarmRec
import info.nightscout.androidaps.plugins.general.nsclient.receivers.DBAccessReceiver;
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin;
import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin;
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerPlugin;
import info.nightscout.androidaps.plugins.general.wear.WearPlugin;
import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin;
import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin;
@ -88,6 +88,7 @@ import info.nightscout.androidaps.receivers.NSAlarmReceiver;
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
import info.nightscout.androidaps.services.Intents;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.LocaleHelper;
import io.fabric.sdk.android.Fabric;
import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion;
@ -124,6 +125,7 @@ public class MainApp extends Application {
log.debug("onCreate");
sInstance = this;
sResources = getResources();
LocaleHelper.INSTANCE.update(this);
sConstraintsChecker = new ConstraintChecker();
sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);

View file

@ -70,8 +70,6 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
RxBus.INSTANCE.send(new EventPreferenceChange(key));
if (key.equals("language")) {
String lang = sharedPreferences.getString("language", "en");
LocaleHelper.setLocale(getApplicationContext(), lang);
RxBus.INSTANCE.send(new EventRebuildTabs(true));
//recreate() does not update language so better close settings
finish();

View file

@ -17,7 +17,7 @@ class EventTidepoolStatus(val status: String) : Event() {
log.debug("New status: $status")
}
private var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.getLocale())
private var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.currentLocale())
fun toPreparedHtml(): StringBuilder {
val stringBuilder = StringBuilder()

View file

@ -91,8 +91,7 @@ public class SWDefinition {
.preferenceId(R.string.key_language).label(R.string.language)
.comment(R.string.setupwizard_language_prompt))
.validator(() -> {
String lang = SP.getString("language", "en");
LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang);
LocaleHelper.INSTANCE.update(MainApp.instance().getApplicationContext());
return SP.contains(R.string.key_language);
});

View file

@ -51,7 +51,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocaleHelper.onCreate(this, "en");
LocaleHelper.INSTANCE.update(getApplicationContext());
setContentView(R.layout.activity_setupwizard);
scrollView = (ScrollView) findViewById(R.id.sw_scrollview);

View file

@ -124,7 +124,7 @@ public class FabricPrivacy {
.replace(".net/", ":");
MainApp.getFirebaseAnalytics().setUserProperty("Mode", BuildConfig.APPLICATION_ID + "-" + closedLoopEnabled);
MainApp.getFirebaseAnalytics().setUserProperty("Language", LocaleHelper.getLanguage());
MainApp.getFirebaseAnalytics().setUserProperty("Language", LocaleHelper.INSTANCE.currentLanguage());
MainApp.getFirebaseAnalytics().setUserProperty("Version", BuildConfig.VERSION);
MainApp.getFirebaseAnalytics().setUserProperty("HEAD", BuildConfig.HEAD);
MainApp.getFirebaseAnalytics().setUserProperty("Remote", remote);

View file

@ -1,69 +0,0 @@
package info.nightscout.androidaps.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import java.util.Locale;
/**
* This class is used to change your application locale and persist this change for the next time
* that your app is going to be used.
* <p/>
* You can also change the locale of your application on the fly by using the setLocale method.
* <p/>
* Created by gunhansancar on 07/10/15.
*/
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static void onCreate(Context context) {
String lang = getPersistedData(Locale.getDefault().getLanguage());
setLocale(context, lang);
}
public static void onCreate(Context context, String defaultLanguage) {
String lang = getPersistedData(defaultLanguage);
setLocale(context, lang);
}
public static String getLanguage() {
return getPersistedData(Locale.getDefault().getLanguage());
}
public static void setLocale(Context context, String language) {
persist(context, language);
updateResources(context, language);
}
private static String getPersistedData(String defaultLanguage) {
return SP.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
private static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
public static Locale getLocale() {
return new Locale(getPersistedData("en"));
}
}

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.utils
import android.content.Context
import info.nightscout.androidaps.R
import java.util.*
object LocaleHelper {
fun update(context: Context) =
updateResources(context, currentLanguage())
fun currentLanguage(): String =
SP.getString(R.string.key_language, Locale.getDefault().language)
fun currentLocale(): Locale =
Locale(SP.getString(R.string.key_language, Locale.getDefault().language))
@Suppress("DEPRECATION")
private fun updateResources(context: Context, language: String) {
var locale = Locale(language)
if (language.contains("_")) {
// language with country like pt_BR defined in arrays.xml
val lang = language.substring(0, 2)
val country = language.substring(3, 5)
locale = Locale(lang, country)
}
Locale.setDefault(locale)
val resources = context.resources
resources.configuration.setLocale(locale)
resources.updateConfiguration(resources.configuration, resources.displayMetrics)
}
}

View file

@ -24,6 +24,7 @@
<item>@string/ko_lang</item>
<item>@string/lt_lang</item>
<item>@string/pt_lang</item>
<item>@string/pt_BR_lang</item>
<item>@string/ro_lang</item>
<item>@string/ru_lang</item>
<item>@string/sk_lang</item>
@ -46,6 +47,7 @@
<item>ko</item>
<item>lt</item>
<item>pt</item>
<item>pt_BR</item>
<item>ro</item>
<item>ru</item>
<item>sk</item>

View file

@ -17,7 +17,7 @@
<string name="lt_lang" translatable="false">Lithuanian</string>
<string name="pl_lang" translatable="false">Polish</string>
<string name="pt_lang" translatable="false">Portuguese</string>
<!-- <string name="pt_BR_lang" translatable="false">Portuguese, Brazilian</string> -->
<string name="pt_BR_lang" translatable="false">Portuguese, Brazilian</string>
<string name="ro_lang" translatable="false">Romanian</string>
<string name="ru_lang" translatable="false">Russian</string>
<string name="sk_lang" translatable="false">Slovak</string>