-
-
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.
Merge branch 'lorenzovngl:main' into search-feature
- Loading branch information
Showing
20 changed files
with
583 additions
and
43 deletions.
There are no files selected for viewing
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
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.code) { | ||
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
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.