-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add language selection in debug mode
Added the ability to select the language of the app. - Added a new dialog to select the language. - Added a new preference to store the selected language. - Added a new helper class to change the locale of the app. - Updated the worker to use the selected language. - Updated the settings screen to add a new option to open the language picker dialog.
- Loading branch information
1 parent
140a497
commit 9c9013d
Showing
9 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/LocaleHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.lorenzovainigli.foodexpirationdates.model | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.res.Configuration | ||
import com.lorenzovainigli.foodexpirationdates.view.MainActivity | ||
import java.util.Locale | ||
import kotlin.jvm.java | ||
|
||
enum class Language(val code: String, val label: String) { | ||
SYSTEM("system", "System"), | ||
ARABIC("ar", "العربية"), | ||
CHINESE_TRADITIONAL("zh-TW", "繁體中文"), | ||
ENGLISH("en", "English"), | ||
FRENCH("fr", "Français"), | ||
GERMAN("de", "Deutsch"), | ||
HINDI("hi", "हिन्दी"), | ||
INDONESIAN("id", "Bahasa Indonesia"), | ||
ITALIAN("it", "Italiano"), | ||
JAPANESE("ja", "日本語"), | ||
POLISH("pl", "Polski"), | ||
RUSSIAN("ru", "Русский"), | ||
SPANISH("es", "Español"), | ||
TAMIL("ta", "தமிழ்"), | ||
TURKISH("tr", "Türkçe"), | ||
VIETNAMESE("vi", "Tiếng Việt"); | ||
|
||
companion object { | ||
fun fromCode(code: String): Language { | ||
return Language.entries.find { it.code == code } ?: SYSTEM | ||
} | ||
} | ||
} | ||
|
||
object LocaleHelper { | ||
|
||
fun setLocale(context: Context, language: String): Context { | ||
val locale = if (language == Language.SYSTEM.name) { | ||
Locale.getDefault() | ||
} else { | ||
Locale(language) | ||
} | ||
Locale.setDefault(locale) | ||
|
||
val config = Configuration(context.resources.configuration) | ||
config.setLocale(locale) | ||
config.setLayoutDirection(locale) | ||
|
||
return context.createConfigurationContext(config) | ||
} | ||
|
||
fun changeLanguage(context: Context, newLanguage: String) { | ||
setLocale(context, newLanguage) | ||
val intent = Intent(context, MainActivity::class.java) // or current activity | ||
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK | ||
context.startActivity(intent) | ||
(context as Activity).finish() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...main/java/com/lorenzovainigli/foodexpirationdates/view/composable/LanguagePickerDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.lorenzovainigli.foodexpirationdates.view.composable | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.CardDefaults | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.RadioButton | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.PreviewLightDark | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import com.lorenzovainigli.foodexpirationdates.R | ||
import com.lorenzovainigli.foodexpirationdates.model.Language | ||
import com.lorenzovainigli.foodexpirationdates.model.LocaleHelper | ||
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository | ||
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme | ||
|
||
@Composable | ||
fun LanguagePickerDialog( | ||
isDialogOpen: Boolean = true, | ||
onDismiss: () -> Unit = {} | ||
) { | ||
if (isDialogOpen) { | ||
val context = LocalContext.current | ||
val storedLanguage = PreferencesRepository.getLanguage(context) | ||
var selectedLanguage = remember { | ||
mutableStateOf(storedLanguage) | ||
} | ||
Dialog( | ||
onDismissRequest = onDismiss | ||
) { | ||
Card( | ||
shape = RoundedCornerShape(10.dp), | ||
elevation = CardDefaults.cardElevation( | ||
defaultElevation = 8.dp | ||
) | ||
) { | ||
Column( | ||
modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp) | ||
) { | ||
Text( | ||
modifier = Modifier.padding(4.dp), | ||
text = stringResource(R.string.select_language), | ||
style = MaterialTheme.typography.titleLarge, | ||
color = MaterialTheme.colorScheme.onSurface | ||
) | ||
Column( | ||
modifier = Modifier | ||
.height(480.dp) | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
Language.entries.forEach { language -> | ||
Row( | ||
modifier = Modifier.fillMaxWidth(1f).clickable( | ||
onClick = { | ||
selectedLanguage.value = language.code | ||
}), | ||
horizontalArrangement = Arrangement.Start, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
RadioButton( | ||
selected = selectedLanguage.value == language.code, | ||
onClick = { | ||
selectedLanguage.value = language.code | ||
} | ||
) | ||
Text( | ||
text = language.label, | ||
color = if (selectedLanguage.value == language.code) | ||
MaterialTheme.colorScheme.primary | ||
else Color.Unspecified | ||
) | ||
} | ||
} | ||
} | ||
Button( | ||
modifier = Modifier.align(Alignment.End), | ||
onClick = { | ||
LocaleHelper.changeLanguage(context, selectedLanguage.value) | ||
PreferencesRepository.setLanguage( | ||
context, | ||
language = selectedLanguage.value | ||
) | ||
} | ||
) { | ||
Text(stringResource(R.string.apply)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@PreviewLightDark | ||
@Composable | ||
fun LanguagePickerDialogPreview() { | ||
FoodExpirationDatesTheme { | ||
Surface { | ||
LanguagePickerDialog() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.