-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9eeef73
commit 76830a5
Showing
83 changed files
with
718 additions
and
422 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Binary file not shown.
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
105 changes: 105 additions & 0 deletions
105
app/src/main/java/io/zoemeow/dutschedule/di/LocaleService.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,105 @@ | ||
package io.zoemeow.dutschedule.di | ||
|
||
import android.annotation.TargetApi | ||
import android.app.LocaleManager | ||
import android.content.Context | ||
import android.content.res.Configuration | ||
import android.os.Build | ||
import android.os.Build.VERSION_CODES | ||
import android.os.LocaleList | ||
import android.util.Log | ||
import io.zoemeow.dutschedule.R | ||
import java.io.File | ||
import java.util.Locale | ||
|
||
object LocaleService { | ||
fun generateContextFromLocale(context: Context, tag: String): Context { | ||
val locale = Locale(tag) | ||
Locale.setDefault(locale) | ||
val configuration = Configuration(context.resources.configuration) | ||
configuration.setLocale(locale) | ||
configuration.setLayoutDirection(locale) | ||
return context.createConfigurationContext(configuration) | ||
} | ||
|
||
fun getCurrentLocaleTag(context: Context, oldLangSelector: Boolean = false): String { | ||
// Android 13 or later have built-in language selector. | ||
val langTag = if (Build.VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !oldLangSelector) { | ||
getLocaleSystem() | ||
} | ||
// Android 12 or lower, or, we force that. | ||
else { | ||
if (getLocaleFromPrefs(context).compareTo("auto") == 0) getLocaleSystem(true) | ||
else getLocaleFromPrefs(context) | ||
} | ||
Log.d("Info", String.format("Selected language tag: %s", langTag)) | ||
return langTag | ||
} | ||
|
||
fun getCurrentLocaleDisplayName(context: Context): String { | ||
// Android 13 or later have built-in language selector. | ||
val langTag = if (Build.VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) { | ||
Locale.getDefault().displayName | ||
} | ||
// Android 12 or lower | ||
else { | ||
if (getLocaleFromPrefs(context).compareTo("auto") == 0) | ||
context.getString(R.string.settings_applanguage_yoursystemlang) | ||
else Locale(getCurrentLocaleTag(context)).displayName | ||
} | ||
Log.d("Info", String.format("Selected language tag: %s", langTag)) | ||
return langTag | ||
} | ||
|
||
|
||
fun getSettingsLocaleTag(context: Context, oldLangSelector: Boolean = false): String { | ||
// Android 13 or later have built-in language selector. | ||
val langTag = if (Build.VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !oldLangSelector) { | ||
getLocaleSystem() | ||
} | ||
// Android 12 or lower, or, we force that. | ||
else { | ||
getLocaleFromPrefs(context) | ||
} | ||
return langTag | ||
} | ||
|
||
private fun getLocaleFromPrefs(context: Context): String { | ||
val langPath = "${context.filesDir.path}/language.settings" | ||
val file = File(langPath) | ||
try { | ||
if (file.isFile) { | ||
val lang = file.readText().trim() | ||
return lang | ||
} else throw Exception("File not found: language.settings.") | ||
} catch (ex: Exception) { | ||
ex.printStackTrace() | ||
return "auto" | ||
} | ||
} | ||
|
||
private fun getLocaleSystem(iso639Format: Boolean = false): String { | ||
return when (iso639Format) { | ||
false -> Locale.getDefault().toLanguageTag() | ||
true -> Locale.getDefault().language | ||
} | ||
} | ||
|
||
@TargetApi(VERSION_CODES.TIRAMISU) | ||
fun setLocaleA13(context: Context, tag: String) { | ||
context.getSystemService(LocaleManager::class.java) | ||
.applicationLocales = LocaleList.forLanguageTags(tag) | ||
} | ||
|
||
fun saveLocale(context: Context, langTag: String) { | ||
val langPath = "${context.filesDir.path}/language.settings" | ||
val file = File(langPath) | ||
try { | ||
val locale = Locale(langTag) | ||
Locale.setDefault(locale) | ||
file.writeText(langTag) | ||
} catch (ex: Exception) { | ||
ex.printStackTrace() | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...tschedule/ui/component/base/ButtonBase.kt → ...ow/dutschedule/ui/component/ButtonBase.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
2 changes: 1 addition & 1 deletion
2
...edule/ui/component/base/CheckboxOption.kt → ...utschedule/ui/component/CheckboxOption.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
2 changes: 1 addition & 1 deletion
2
...chedule/ui/component/base/DataAdjuster.kt → .../dutschedule/ui/component/DataAdjuster.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
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
2 changes: 1 addition & 1 deletion
2
...ui/component/base/DialogCheckboxButton.kt → ...dule/ui/component/DialogCheckboxButton.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
2 changes: 1 addition & 1 deletion
2
...le/ui/component/base/DialogRadioButton.kt → ...chedule/ui/component/DialogRadioButton.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
2 changes: 1 addition & 1 deletion
2
...schedule/ui/component/base/DividerItem.kt → ...w/dutschedule/ui/component/DividerItem.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
2 changes: 1 addition & 1 deletion
2
...e/ui/component/base/OptionCheckBoxItem.kt → ...hedule/ui/component/OptionCheckBoxItem.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
Oops, something went wrong.