fix(i18n): force context recreation on locale change and fix hardcoded strings
All checks were successful
Update Wiki Documentation / generate-docs (push) Successful in 2m12s
All checks were successful
Update Wiki Documentation / generate-docs (push) Successful in 2m12s
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
package com.example.retroha
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import com.example.retroha.i18n.LocaleHelper
|
||||
|
||||
abstract class BaseActivity : Activity() {
|
||||
override fun attachBaseContext(newBase: Context) {
|
||||
super.attachBaseContext(LocaleHelper.setLocale(newBase))
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
LocaleHelper.setLocale(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,12 @@ import android.widget.TextView
|
||||
import com.example.retroha.data.Prefs
|
||||
import com.example.retroha.i18n.LocaleHelper
|
||||
import com.example.retroha.theme.Colors
|
||||
class LanguageActivity : Activity() {
|
||||
class LanguageActivity : BaseActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val isFirstLaunch = Prefs.getLanguage(this) == null
|
||||
val fromSettings = intent.getBooleanExtra("from_settings", false)
|
||||
if (!isFirstLaunch && !fromSettings) {
|
||||
LocaleHelper.setLocale(this)
|
||||
startMainActivity()
|
||||
return
|
||||
}
|
||||
@@ -72,12 +71,10 @@ class LanguageActivity : Activity() {
|
||||
}
|
||||
root.addView(createBauhausButton("POLSKI", Colors.BLUE) {
|
||||
Prefs.setLanguage(this, "pl")
|
||||
LocaleHelper.setLocale(this)
|
||||
startMainActivity()
|
||||
})
|
||||
root.addView(createBauhausButton("ENGLISH", Colors.YELLOW) {
|
||||
Prefs.setLanguage(this, "en")
|
||||
LocaleHelper.setLocale(this)
|
||||
startMainActivity()
|
||||
})
|
||||
setContentView(root)
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
package com.example.retroha.i18n
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.os.Build
|
||||
import com.example.retroha.data.Prefs
|
||||
import java.util.Locale
|
||||
|
||||
object LocaleHelper {
|
||||
fun setLocale(context: Context) {
|
||||
val lang = Prefs.getLanguage(context) ?: return
|
||||
val locale = Locale(lang)
|
||||
|
||||
fun setLocale(context: Context): Context {
|
||||
val lang = Prefs.getLanguage(context) ?: "pl"
|
||||
return updateResources(context, lang)
|
||||
}
|
||||
|
||||
private fun updateResources(context: Context, language: String): Context {
|
||||
val locale = Locale(language)
|
||||
Locale.setDefault(locale)
|
||||
val resources = context.resources
|
||||
val config = Configuration(resources.configuration)
|
||||
config.locale = locale
|
||||
resources.updateConfiguration(config, resources.displayMetrics)
|
||||
|
||||
val configuration = Configuration(context.resources.configuration)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
configuration.setLocale(locale)
|
||||
return context.createConfigurationContext(configuration)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
configuration.locale = locale
|
||||
@Suppress("DEPRECATION")
|
||||
context.resources.updateConfiguration(configuration, context.resources.displayMetrics)
|
||||
return context
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,20 +43,25 @@ class LightControlDialog(
|
||||
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(2))
|
||||
setBackgroundColor(Colors.BLACK)
|
||||
})
|
||||
|
||||
val brightnessLabel = context.getString(com.example.retroha.R.string.dialog_brightness)
|
||||
|
||||
val tvBrightness = TextView(context).apply {
|
||||
text = "JASNOŚĆ: ${(initialBrightness * 100 / 255)}%"
|
||||
text = "$brightnessLabel: ${(initialBrightness * 100 / 255)}%"
|
||||
typeface = android.graphics.Typeface.MONOSPACE
|
||||
textSize = 14f
|
||||
setTextColor(Colors.BLACK)
|
||||
setPadding(0, dp(24), 0, dp(8))
|
||||
}
|
||||
root.addView(tvBrightness)
|
||||
|
||||
// SeekBar
|
||||
val seekBar = SeekBar(context).apply {
|
||||
max = 255
|
||||
progress = initialBrightness
|
||||
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||
override fun onProgressChanged(s: SeekBar?, p: Int, fromUser: Boolean) {
|
||||
tvBrightness.text = "JASNOŚĆ: ${(p * 100 / 255)}%"
|
||||
tvBrightness.text = "$brightnessLabel: ${(p * 100 / 255)}%"
|
||||
}
|
||||
override fun onStartTrackingTouch(s: SeekBar?) {}
|
||||
override fun onStopTrackingTouch(s: SeekBar?) {
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="USTAWIENIA"
|
||||
android:text="@string/btn_settings"
|
||||
android:paddingLeft="12dp"
|
||||
android:paddingRight="12dp"
|
||||
android:paddingTop="7dp"
|
||||
@@ -73,7 +73,7 @@
|
||||
android:id="@+id/btnSettings"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="USTAWIENIA"
|
||||
android:text="@string/btn_settings"
|
||||
android:paddingLeft="12dp"
|
||||
android:paddingRight="12dp"
|
||||
android:paddingTop="7dp"
|
||||
|
||||
Reference in New Issue
Block a user