Default language option

This commit is contained in:
Milos Kozak 2020-05-23 15:39:05 +02:00
parent bfc241f0b2
commit d40188f04e
15 changed files with 40 additions and 52 deletions

View file

@ -49,7 +49,7 @@ import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicato
import info.nightscout.androidaps.setupwizard.SetupWizardActivity
import info.nightscout.androidaps.utils.AndroidPermission
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.buildHelper.BuildHelper
import info.nightscout.androidaps.utils.extensions.isRunningRealPumpTest

View file

@ -37,7 +37,7 @@ import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
import info.nightscout.androidaps.services.Intents;
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;
public class MainApp extends DaggerApplication {

View file

@ -5,7 +5,7 @@ import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
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 javax.inject.Inject

View file

@ -9,7 +9,7 @@ import dagger.android.support.DaggerAppCompatActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.PluginBase
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 javax.inject.Inject

View file

@ -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.PrefsMetadataKey
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 kotlinx.android.synthetic.main.maintenance_importlist_activity.*
import javax.inject.Inject

View file

@ -34,7 +34,7 @@ import info.nightscout.androidaps.setupwizard.elements.*
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.AndroidPermission
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.protection.ProtectionCheck
import info.nightscout.androidaps.utils.resources.ResourceHelper

View file

@ -20,7 +20,7 @@ import info.nightscout.androidaps.setupwizard.elements.SWItem
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.AndroidPermission
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.showConfirmation
import info.nightscout.androidaps.utils.resources.ResourceHelper

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.utils;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
@ -8,7 +7,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
@ -24,33 +22,24 @@ public class JSONFormatter {
final JsonVisitor visitor = new JsonVisitor(1, '\t');
try {
if (jsonString.equals("undefined"))
return Html.fromHtml("undefined");
return HtmlHelper.INSTANCE.fromHtml("undefined");
else if (jsonString.getBytes()[0] == '[')
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0), Html.FROM_HTML_MODE_COMPACT);
} else {
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));
}
}
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(new JSONArray(jsonString), 0));
else
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(new JSONObject(jsonString), 0));
} catch (JSONException e) {
log.error("Unhandled exception", e);
return Html.fromHtml("");
return HtmlHelper.INSTANCE.fromHtml("");
}
}
public static Spanned format(final JSONObject object) {
final JsonVisitor visitor = new JsonVisitor(1, '\t');
try {
return Html.fromHtml(visitor.visit(object, 0));
return HtmlHelper.INSTANCE.fromHtml(visitor.visit(object, 0));
} catch (JSONException e) {
log.error("Unhandled exception", e);
return Html.fromHtml("");
return HtmlHelper.INSTANCE.fromHtml("");
}
}

View file

@ -22,6 +22,7 @@
</string-array>
<string-array name="languagesArray">
<item>@string/default_lang</item>
<item>@string/en_lang</item>
<item>@string/af_lang</item>
<item>@string/bg_lang</item>
@ -45,6 +46,7 @@
<item>@string/zh_lang</item>
</string-array>
<string-array name="languagesValues" translatable="false">
<item>default</item>
<item>en</item>
<item>af</item>
<item>bg</item>

View file

@ -1,4 +1,5 @@
<resources>
<string name="default_lang" translatable="false">System default</string>
<string name="en_lang" translatable="false">English</string>
<string name="cs_lang" translatable="false">Czech</string>
<string name="af_lang" translatable="false">Afrikaans</string>

View file

@ -2,7 +2,7 @@ package info.nightscout.androidaps.activities
import android.content.Context
import dagger.android.support.DaggerAppCompatActivity
import info.nightscout.androidaps.utils.LocaleHelper
import info.nightscout.androidaps.utils.locale.LocaleHelper
open class DialogAppCompatActivity : DaggerAppCompatActivity() {
public override fun attachBaseContext(newBase: Context) {

View file

@ -4,7 +4,7 @@ import android.content.Context
import android.os.Bundle
import dagger.android.support.DaggerAppCompatActivity
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.utils.LocaleHelper
import info.nightscout.androidaps.utils.locale.LocaleHelper
//@Suppress("registered")
open class NoSplashAppCompatActivity : DaggerAppCompatActivity() {

View file

@ -1,3 +1,3 @@
package info.nightscout.androidaps.events
class EventRebuildTabs @JvmOverloads constructor(var recreate: Boolean = false) : Event()
class EventRebuildTabs constructor(var recreate: Boolean = false) : Event()

View file

@ -6,11 +6,6 @@ import android.text.Spanned
object HtmlHelper {
fun fromHtml(source: String): Spanned {
// API level 24 to replace call
@Suppress("DEPRECATION")
return when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY)
else -> Html.fromHtml(source)
}
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY)
}
}

View file

@ -1,22 +1,23 @@
package info.nightscout.androidaps.utils
package info.nightscout.androidaps.utils.locale
import android.content.Context
import android.content.ContextWrapper
import android.os.Build
import android.os.LocaleList
import androidx.preference.PreferenceManager
import info.nightscout.androidaps.core.R
import java.util.*
object LocaleHelper {
private fun currentLanguage(context: Context): String =
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "en")
?: "en"
private fun selectedLanguage(context: Context): String =
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "default")
?: "default"
// injection not possible because of use in attachBaseContext
//SP.getString(R.string.key_language, Locale.getDefault().language)
private fun currentLocale(context: Context): Locale {
val language = currentLanguage(context)
val language = selectedLanguage(context)
if (language == "default") return Locale.getDefault()
var locale = Locale(language)
if (language.contains("_")) {
// language with country like pt_BR defined in arrays.xml
@ -27,8 +28,10 @@ object LocaleHelper {
return locale
}
@Suppress("DEPRECATION")
fun update(context: Context) {
// no action for system default language
if (selectedLanguage(context) == "default") return
val locale = currentLocale(context)
Locale.setDefault(locale)
val resources = context.resources
@ -39,20 +42,18 @@ object LocaleHelper {
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 configuration = res.configuration
val newLocale = currentLocale(ctx)
val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration.locales = localeList
ctx.createConfigurationContext(configuration)
} else {
configuration.setLocale(newLocale)
ctx.createConfigurationContext(configuration)
}
configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration.locales = localeList
val context = ctx.createConfigurationContext(configuration)
return ContextWrapper(context)
}
}