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
|
package com.example.retroha
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import com.example.retroha.i18n.LocaleHelper
|
import com.example.retroha.i18n.LocaleHelper
|
||||||
|
|
||||||
abstract class BaseActivity : Activity() {
|
abstract class BaseActivity : Activity() {
|
||||||
|
override fun attachBaseContext(newBase: Context) {
|
||||||
|
super.attachBaseContext(LocaleHelper.setLocale(newBase))
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
LocaleHelper.setLocale(this)
|
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,12 @@ import android.widget.TextView
|
|||||||
import com.example.retroha.data.Prefs
|
import com.example.retroha.data.Prefs
|
||||||
import com.example.retroha.i18n.LocaleHelper
|
import com.example.retroha.i18n.LocaleHelper
|
||||||
import com.example.retroha.theme.Colors
|
import com.example.retroha.theme.Colors
|
||||||
class LanguageActivity : Activity() {
|
class LanguageActivity : BaseActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
val isFirstLaunch = Prefs.getLanguage(this) == null
|
val isFirstLaunch = Prefs.getLanguage(this) == null
|
||||||
val fromSettings = intent.getBooleanExtra("from_settings", false)
|
val fromSettings = intent.getBooleanExtra("from_settings", false)
|
||||||
if (!isFirstLaunch && !fromSettings) {
|
if (!isFirstLaunch && !fromSettings) {
|
||||||
LocaleHelper.setLocale(this)
|
|
||||||
startMainActivity()
|
startMainActivity()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -72,12 +71,10 @@ class LanguageActivity : Activity() {
|
|||||||
}
|
}
|
||||||
root.addView(createBauhausButton("POLSKI", Colors.BLUE) {
|
root.addView(createBauhausButton("POLSKI", Colors.BLUE) {
|
||||||
Prefs.setLanguage(this, "pl")
|
Prefs.setLanguage(this, "pl")
|
||||||
LocaleHelper.setLocale(this)
|
|
||||||
startMainActivity()
|
startMainActivity()
|
||||||
})
|
})
|
||||||
root.addView(createBauhausButton("ENGLISH", Colors.YELLOW) {
|
root.addView(createBauhausButton("ENGLISH", Colors.YELLOW) {
|
||||||
Prefs.setLanguage(this, "en")
|
Prefs.setLanguage(this, "en")
|
||||||
LocaleHelper.setLocale(this)
|
|
||||||
startMainActivity()
|
startMainActivity()
|
||||||
})
|
})
|
||||||
setContentView(root)
|
setContentView(root)
|
||||||
|
|||||||
@@ -1,16 +1,32 @@
|
|||||||
package com.example.retroha.i18n
|
package com.example.retroha.i18n
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
|
import android.os.Build
|
||||||
import com.example.retroha.data.Prefs
|
import com.example.retroha.data.Prefs
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
object LocaleHelper {
|
object LocaleHelper {
|
||||||
fun setLocale(context: Context) {
|
|
||||||
val lang = Prefs.getLanguage(context) ?: return
|
fun setLocale(context: Context): Context {
|
||||||
val locale = Locale(lang)
|
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)
|
Locale.setDefault(locale)
|
||||||
val resources = context.resources
|
|
||||||
val config = Configuration(resources.configuration)
|
val configuration = Configuration(context.resources.configuration)
|
||||||
config.locale = locale
|
|
||||||
resources.updateConfiguration(config, resources.displayMetrics)
|
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))
|
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(2))
|
||||||
setBackgroundColor(Colors.BLACK)
|
setBackgroundColor(Colors.BLACK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
val brightnessLabel = context.getString(com.example.retroha.R.string.dialog_brightness)
|
||||||
|
|
||||||
val tvBrightness = TextView(context).apply {
|
val tvBrightness = TextView(context).apply {
|
||||||
text = "JASNOŚĆ: ${(initialBrightness * 100 / 255)}%"
|
text = "$brightnessLabel: ${(initialBrightness * 100 / 255)}%"
|
||||||
typeface = android.graphics.Typeface.MONOSPACE
|
typeface = android.graphics.Typeface.MONOSPACE
|
||||||
textSize = 14f
|
textSize = 14f
|
||||||
setTextColor(Colors.BLACK)
|
setTextColor(Colors.BLACK)
|
||||||
setPadding(0, dp(24), 0, dp(8))
|
setPadding(0, dp(24), 0, dp(8))
|
||||||
}
|
}
|
||||||
root.addView(tvBrightness)
|
root.addView(tvBrightness)
|
||||||
|
|
||||||
|
// SeekBar
|
||||||
val seekBar = SeekBar(context).apply {
|
val seekBar = SeekBar(context).apply {
|
||||||
max = 255
|
max = 255
|
||||||
progress = initialBrightness
|
progress = initialBrightness
|
||||||
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||||
override fun onProgressChanged(s: SeekBar?, p: Int, fromUser: Boolean) {
|
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 onStartTrackingTouch(s: SeekBar?) {}
|
||||||
override fun onStopTrackingTouch(s: SeekBar?) {
|
override fun onStopTrackingTouch(s: SeekBar?) {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="USTAWIENIA"
|
android:text="@string/btn_settings"
|
||||||
android:paddingLeft="12dp"
|
android:paddingLeft="12dp"
|
||||||
android:paddingRight="12dp"
|
android:paddingRight="12dp"
|
||||||
android:paddingTop="7dp"
|
android:paddingTop="7dp"
|
||||||
@@ -73,7 +73,7 @@
|
|||||||
android:id="@+id/btnSettings"
|
android:id="@+id/btnSettings"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="USTAWIENIA"
|
android:text="@string/btn_settings"
|
||||||
android:paddingLeft="12dp"
|
android:paddingLeft="12dp"
|
||||||
android:paddingRight="12dp"
|
android:paddingRight="12dp"
|
||||||
android:paddingTop="7dp"
|
android:paddingTop="7dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user