-
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.
Merge pull request #3181 from CruGlobal/appLanguageFixes
- Loading branch information
Showing
19 changed files
with
151 additions
and
90 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
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
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
23 changes: 23 additions & 0 deletions
23
library/base/src/main/kotlin/org/cru/godtools/base/AppLanguage.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,23 @@ | ||
package org.cru.godtools.base | ||
|
||
import android.content.Context | ||
import androidx.appcompat.app.AppCompatDelegate | ||
import java.util.Locale | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.flow | ||
import org.ccci.gto.android.common.androidx.core.content.localizeIfPossible | ||
|
||
val Context.appLanguage: Locale | ||
get() = localizeIfPossible(AppCompatDelegate.getApplicationLocales()) | ||
.getString(R.string.normalized_app_language) | ||
.let { Locale.forLanguageTag(it) } | ||
|
||
fun Context.getAppLanguageFlow(): Flow<Locale> = flow { | ||
// TODO: is there a way to actively listen for changes? | ||
while (true) { | ||
emit(appLanguage) | ||
delay(1_000 / 60) | ||
} | ||
}.distinctUntilChanged() |
24 changes: 24 additions & 0 deletions
24
library/base/src/main/kotlin/org/cru/godtools/base/LocalAppLanguage.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,24 @@ | ||
package org.cru.godtools.base | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.platform.LocalContext | ||
import java.util.Locale | ||
|
||
object LocalAppLanguage { | ||
private val LocalComposition = staticCompositionLocalOf<Locale?> { null } | ||
|
||
/** | ||
* Returns current App Language value | ||
*/ | ||
val current: Locale | ||
@Composable | ||
get() = LocalComposition.current | ||
?: LocalContext.current.let { it.getAppLanguageFlow().collectAsState(it.appLanguage).value } | ||
|
||
/** | ||
* Associates a [LocalAppLanguage] key to a value in a call to [CompositionLocalProvider]. | ||
*/ | ||
infix fun provides(locale: Locale) = LocalComposition.provides(locale) | ||
} |
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.