language selector in menu
This commit is contained in:
parent
877b760aef
commit
aa6c5764c4
4 changed files with 83 additions and 0 deletions
|
@ -34,6 +34,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
|
|||
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
|
||||
import info.nightscout.androidaps.tabs.*;
|
||||
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
|
||||
import info.nightscout.utils.LocaleHelper;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static Logger log = LoggerFactory.getLogger(MainActivity.class);
|
||||
|
@ -118,6 +119,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
case R.id.nav_resetdb:
|
||||
MainApp.getDbHelper().resetDatabases();
|
||||
break;
|
||||
case R.id.en_lang: {
|
||||
LocaleHelper.setLocale(this, "en");
|
||||
break;
|
||||
}
|
||||
case R.id.cs_lang: {
|
||||
LocaleHelper.setLocale(this, "cs");
|
||||
break;
|
||||
}
|
||||
case R.id.nav_exit:
|
||||
log.debug("Exiting");
|
||||
//chancelAlarmManager();
|
||||
|
|
66
app/src/main/java/info/nightscout/utils/LocaleHelper.java
Normal file
66
app/src/main/java/info/nightscout/utils/LocaleHelper.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
package info.nightscout.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(context, Locale.getDefault().getLanguage());
|
||||
setLocale(context, lang);
|
||||
}
|
||||
|
||||
public static void onCreate(Context context, String defaultLanguage) {
|
||||
String lang = getPersistedData(context, defaultLanguage);
|
||||
setLocale(context, lang);
|
||||
}
|
||||
|
||||
public static String getLanguage(Context context) {
|
||||
return getPersistedData(context, Locale.getDefault().getLanguage());
|
||||
}
|
||||
|
||||
public static void setLocale(Context context, String language) {
|
||||
persist(context, language);
|
||||
updateResources(context, language);
|
||||
}
|
||||
|
||||
private static String getPersistedData(Context context, String defaultLanguage) {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return preferences.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());
|
||||
}
|
||||
}
|
|
@ -13,6 +13,12 @@
|
|||
<item
|
||||
android:id="@+id/nav_test_alarm"
|
||||
android:title="@string/nav_test_alarm" />
|
||||
<item
|
||||
android:id="@+id/en_lang"
|
||||
android:title="@string/en_lang" />
|
||||
<item
|
||||
android:id="@+id/cs_lang"
|
||||
android:title="@string/cs_lang" />
|
||||
<item
|
||||
android:id="@+id/nav_exit"
|
||||
android:title="@string/nav_exit" />
|
||||
|
|
|
@ -154,5 +154,7 @@
|
|||
<string name="pumpstatusavailableinns">Pump status available in NS</string>
|
||||
<string name="manualenacts">Manual enacts</string>
|
||||
<string name="loopdisabled">LOOP DISABLED BY CONSTRAINTS</string>
|
||||
<string name="cs_lang">Czech language</string>
|
||||
<string name="en_lang">English language</string>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue