Default language option
This commit is contained in:
parent
bfc241f0b2
commit
d40188f04e
15 changed files with 40 additions and 52 deletions
|
@ -49,7 +49,7 @@ import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicato
|
||||||
import info.nightscout.androidaps.setupwizard.SetupWizardActivity
|
import info.nightscout.androidaps.setupwizard.SetupWizardActivity
|
||||||
import info.nightscout.androidaps.utils.AndroidPermission
|
import info.nightscout.androidaps.utils.AndroidPermission
|
||||||
import info.nightscout.androidaps.utils.FabricPrivacy
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
||||||
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
||||||
import info.nightscout.androidaps.utils.extensions.isRunningRealPumpTest
|
import info.nightscout.androidaps.utils.extensions.isRunningRealPumpTest
|
||||||
|
|
|
@ -37,7 +37,7 @@ import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
||||||
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
||||||
import info.nightscout.androidaps.services.Intents;
|
import info.nightscout.androidaps.services.Intents;
|
||||||
import info.nightscout.androidaps.utils.ActivityMonitor;
|
import info.nightscout.androidaps.utils.ActivityMonitor;
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper;
|
import info.nightscout.androidaps.utils.locale.LocaleHelper;
|
||||||
import info.nightscout.androidaps.utils.sharedPreferences.SP;
|
import info.nightscout.androidaps.utils.sharedPreferences.SP;
|
||||||
|
|
||||||
public class MainApp extends DaggerApplication {
|
public class MainApp extends DaggerApplication {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import android.os.Bundle
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
import androidx.preference.PreferenceScreen
|
import androidx.preference.PreferenceScreen
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import dagger.android.support.DaggerAppCompatActivity
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase
|
import info.nightscout.androidaps.interfaces.PluginBase
|
||||||
import info.nightscout.androidaps.plugins.configBuilder.PluginStore
|
import info.nightscout.androidaps.plugins.configBuilder.PluginStore
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
import info.nightscout.androidaps.utils.protection.ProtectionCheck
|
import info.nightscout.androidaps.utils.protection.ProtectionCheck
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ import info.nightscout.androidaps.plugins.general.maintenance.PrefsFileContract
|
||||||
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsFormatsHandler
|
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsFormatsHandler
|
||||||
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsMetadataKey
|
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsMetadataKey
|
||||||
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsStatus
|
import info.nightscout.androidaps.plugins.general.maintenance.formats.PrefsStatus
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
import kotlinx.android.synthetic.main.maintenance_importlist_activity.*
|
import kotlinx.android.synthetic.main.maintenance_importlist_activity.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
|
@ -34,7 +34,7 @@ import info.nightscout.androidaps.setupwizard.elements.*
|
||||||
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
|
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
|
||||||
import info.nightscout.androidaps.utils.AndroidPermission
|
import info.nightscout.androidaps.utils.AndroidPermission
|
||||||
import info.nightscout.androidaps.utils.CryptoUtil
|
import info.nightscout.androidaps.utils.CryptoUtil
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper.update
|
import info.nightscout.androidaps.utils.locale.LocaleHelper.update
|
||||||
import info.nightscout.androidaps.utils.extensions.isRunningTest
|
import info.nightscout.androidaps.utils.extensions.isRunningTest
|
||||||
import info.nightscout.androidaps.utils.protection.ProtectionCheck
|
import info.nightscout.androidaps.utils.protection.ProtectionCheck
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
|
|
|
@ -20,7 +20,7 @@ import info.nightscout.androidaps.setupwizard.elements.SWItem
|
||||||
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
|
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
|
||||||
import info.nightscout.androidaps.utils.AndroidPermission
|
import info.nightscout.androidaps.utils.AndroidPermission
|
||||||
import info.nightscout.androidaps.utils.FabricPrivacy
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper.update
|
import info.nightscout.androidaps.utils.locale.LocaleHelper.update
|
||||||
import info.nightscout.androidaps.utils.alertDialogs.OKDialog.show
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog.show
|
||||||
import info.nightscout.androidaps.utils.alertDialogs.OKDialog.showConfirmation
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog.showConfirmation
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package info.nightscout.androidaps.utils;
|
package info.nightscout.androidaps.utils;
|
||||||
|
|
||||||
import android.os.Build;
|
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
|
|
||||||
|
@ -8,7 +7,6 @@ import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
@ -24,33 +22,24 @@ public class JSONFormatter {
|
||||||
final JsonVisitor visitor = new JsonVisitor(1, '\t');
|
final JsonVisitor visitor = new JsonVisitor(1, '\t');
|
||||||
try {
|
try {
|
||||||
if (jsonString.equals("undefined"))
|
if (jsonString.equals("undefined"))
|
||||||
return Html.fromHtml("undefined");
|
return HtmlHelper.INSTANCE.fromHtml("undefined");
|
||||||
else if (jsonString.getBytes()[0] == '[')
|
else if (jsonString.getBytes()[0] == '[')
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(new JSONArray(jsonString), 0));
|
||||||
return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0), Html.FROM_HTML_MODE_COMPACT);
|
else
|
||||||
} else {
|
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(new JSONObject(jsonString), 0));
|
||||||
return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0), Html.FROM_HTML_MODE_COMPACT);
|
|
||||||
} else {
|
|
||||||
return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.error("Unhandled exception", e);
|
log.error("Unhandled exception", e);
|
||||||
return Html.fromHtml("");
|
return HtmlHelper.INSTANCE.fromHtml("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Spanned format(final JSONObject object) {
|
public static Spanned format(final JSONObject object) {
|
||||||
final JsonVisitor visitor = new JsonVisitor(1, '\t');
|
final JsonVisitor visitor = new JsonVisitor(1, '\t');
|
||||||
try {
|
try {
|
||||||
return Html.fromHtml(visitor.visit(object, 0));
|
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(object, 0));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.error("Unhandled exception", e);
|
log.error("Unhandled exception", e);
|
||||||
return Html.fromHtml("");
|
return HtmlHelper.INSTANCE.fromHtml("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string-array name="languagesArray">
|
<string-array name="languagesArray">
|
||||||
|
<item>@string/default_lang</item>
|
||||||
<item>@string/en_lang</item>
|
<item>@string/en_lang</item>
|
||||||
<item>@string/af_lang</item>
|
<item>@string/af_lang</item>
|
||||||
<item>@string/bg_lang</item>
|
<item>@string/bg_lang</item>
|
||||||
|
@ -45,6 +46,7 @@
|
||||||
<item>@string/zh_lang</item>
|
<item>@string/zh_lang</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
<string-array name="languagesValues" translatable="false">
|
<string-array name="languagesValues" translatable="false">
|
||||||
|
<item>default</item>
|
||||||
<item>en</item>
|
<item>en</item>
|
||||||
<item>af</item>
|
<item>af</item>
|
||||||
<item>bg</item>
|
<item>bg</item>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<resources>
|
<resources>
|
||||||
|
<string name="default_lang" translatable="false">System default</string>
|
||||||
<string name="en_lang" translatable="false">English</string>
|
<string name="en_lang" translatable="false">English</string>
|
||||||
<string name="cs_lang" translatable="false">Czech</string>
|
<string name="cs_lang" translatable="false">Czech</string>
|
||||||
<string name="af_lang" translatable="false">Afrikaans</string>
|
<string name="af_lang" translatable="false">Afrikaans</string>
|
||||||
|
|
|
@ -2,7 +2,7 @@ package info.nightscout.androidaps.activities
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import dagger.android.support.DaggerAppCompatActivity
|
import dagger.android.support.DaggerAppCompatActivity
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
|
|
||||||
open class DialogAppCompatActivity : DaggerAppCompatActivity() {
|
open class DialogAppCompatActivity : DaggerAppCompatActivity() {
|
||||||
public override fun attachBaseContext(newBase: Context) {
|
public override fun attachBaseContext(newBase: Context) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import android.content.Context
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import dagger.android.support.DaggerAppCompatActivity
|
import dagger.android.support.DaggerAppCompatActivity
|
||||||
import info.nightscout.androidaps.core.R
|
import info.nightscout.androidaps.core.R
|
||||||
import info.nightscout.androidaps.utils.LocaleHelper
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
||||||
|
|
||||||
//@Suppress("registered")
|
//@Suppress("registered")
|
||||||
open class NoSplashAppCompatActivity : DaggerAppCompatActivity() {
|
open class NoSplashAppCompatActivity : DaggerAppCompatActivity() {
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package info.nightscout.androidaps.events
|
package info.nightscout.androidaps.events
|
||||||
|
|
||||||
class EventRebuildTabs @JvmOverloads constructor(var recreate: Boolean = false) : Event()
|
class EventRebuildTabs constructor(var recreate: Boolean = false) : Event()
|
||||||
|
|
|
@ -6,11 +6,6 @@ import android.text.Spanned
|
||||||
|
|
||||||
object HtmlHelper {
|
object HtmlHelper {
|
||||||
fun fromHtml(source: String): Spanned {
|
fun fromHtml(source: String): Spanned {
|
||||||
// API level 24 to replace call
|
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY)
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
return when {
|
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY)
|
|
||||||
else -> Html.fromHtml(source)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,22 +1,23 @@
|
||||||
package info.nightscout.androidaps.utils
|
package info.nightscout.androidaps.utils.locale
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.ContextWrapper
|
import android.content.ContextWrapper
|
||||||
import android.os.Build
|
|
||||||
import android.os.LocaleList
|
import android.os.LocaleList
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import info.nightscout.androidaps.core.R
|
import info.nightscout.androidaps.core.R
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object LocaleHelper {
|
object LocaleHelper {
|
||||||
private fun currentLanguage(context: Context): String =
|
private fun selectedLanguage(context: Context): String =
|
||||||
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "en")
|
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "default")
|
||||||
?: "en"
|
?: "default"
|
||||||
// injection not possible because of use in attachBaseContext
|
// injection not possible because of use in attachBaseContext
|
||||||
//SP.getString(R.string.key_language, Locale.getDefault().language)
|
//SP.getString(R.string.key_language, Locale.getDefault().language)
|
||||||
|
|
||||||
private fun currentLocale(context: Context): Locale {
|
private fun currentLocale(context: Context): Locale {
|
||||||
val language = currentLanguage(context)
|
val language = selectedLanguage(context)
|
||||||
|
if (language == "default") return Locale.getDefault()
|
||||||
|
|
||||||
var locale = Locale(language)
|
var locale = Locale(language)
|
||||||
if (language.contains("_")) {
|
if (language.contains("_")) {
|
||||||
// language with country like pt_BR defined in arrays.xml
|
// language with country like pt_BR defined in arrays.xml
|
||||||
|
@ -27,8 +28,10 @@ object LocaleHelper {
|
||||||
return locale
|
return locale
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
fun update(context: Context) {
|
fun update(context: Context) {
|
||||||
|
// no action for system default language
|
||||||
|
if (selectedLanguage(context) == "default") return
|
||||||
|
|
||||||
val locale = currentLocale(context)
|
val locale = currentLocale(context)
|
||||||
Locale.setDefault(locale)
|
Locale.setDefault(locale)
|
||||||
val resources = context.resources
|
val resources = context.resources
|
||||||
|
@ -39,20 +42,18 @@ object LocaleHelper {
|
||||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun wrap(ctx: Context): ContextWrapper {
|
fun wrap(ctx: Context): Context {
|
||||||
|
// no action for system default language
|
||||||
|
if (selectedLanguage(ctx) == "default") return ctx
|
||||||
|
|
||||||
val res = ctx.resources
|
val res = ctx.resources
|
||||||
val configuration = res.configuration
|
val configuration = res.configuration
|
||||||
val newLocale = currentLocale(ctx)
|
val newLocale = currentLocale(ctx)
|
||||||
val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
configuration.setLocale(newLocale)
|
||||||
configuration.setLocale(newLocale)
|
val localeList = LocaleList(newLocale)
|
||||||
val localeList = LocaleList(newLocale)
|
LocaleList.setDefault(localeList)
|
||||||
LocaleList.setDefault(localeList)
|
configuration.locales = localeList
|
||||||
configuration.locales = localeList
|
val context = ctx.createConfigurationContext(configuration)
|
||||||
ctx.createConfigurationContext(configuration)
|
|
||||||
} else {
|
|
||||||
configuration.setLocale(newLocale)
|
|
||||||
ctx.createConfigurationContext(configuration)
|
|
||||||
}
|
|
||||||
return ContextWrapper(context)
|
return ContextWrapper(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue