Match platform language resolution behavior

And add non-Compose accessor for Strings.
This commit is contained in:
FooIbar committed 2024-12-11 00:06:07 +08:00
1 parent 0e608d169e
commit b3a212c7fd
19 files changed
+66 -51

No files matched your search

@@ -7,8 +7,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import com.hippo.ehviewer.ui.i18n.LocalTranslations
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.LocalStrings
import com.hippo.ehviewer.ui.i18n.Strings
import com.hippo.ehviewer.ui.screen.implicit
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.ui.tools.LocalDialogState
@@ -22,12 +22,12 @@ import kotlinx.coroutines.CoroutineScope
@Composable
inline fun <R> AnimatedVisibilityScope.composing(
navigator: DestinationsNavigator,
block: @Composable context(Translations, MainActivity, SnackbarHostState, DialogState, SharedTransitionScope, TransitionsVisibilityScope, DestinationsNavigator, CoroutineScope)
block: @Composable context(Strings, MainActivity, SnackbarHostState, DialogState, SharedTransitionScope, TransitionsVisibilityScope, DestinationsNavigator, CoroutineScope)
() -> R,
) = with(NoopTransitionsVisibilityScope) {
togetherWith(implicit<AnimatedVisibilityScope>()) {
block(
LocalTranslations.current,
LocalStrings.current,
with(LocalContext.current) { remember { findActivity() } },
LocalSnackBarHostState.current,
LocalDialogState.current,
@@ -30,7 +30,7 @@ import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.thumbUrl
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.Strings
import com.hippo.ehviewer.ui.screen.navWithUrl
import com.hippo.ehviewer.util.addTextToClipboard
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@@ -38,7 +38,7 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
private const val INDEX_URL = 2
private const val INDEX_PARENT = 9
context(Translations)
context(Strings)
fun GalleryDetail.content() = arrayOf(
keyGid to "$gid",
keyToken to token,
@@ -63,7 +63,7 @@ fun GalleryDetail.content() = arrayOf(
keyFavoriteName to favoriteName,
)
context(Context, DestinationsNavigator, Translations)
context(Context, DestinationsNavigator, Strings)
@Composable
fun GalleryInfoBottomSheet(detail: GalleryDetail) {
Column(modifier = Modifier.fillMaxWidth()) {
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object DeTranslations : Translations by EnTranslations {
object DeStrings : Strings by EnStrings {
override val homepage = "Startseite"
override val subscription = "Gefolgte Etiketten"
override val whatsHot = "Beliebt"
@@ -2,10 +2,10 @@ package com.hippo.ehviewer.ui.i18n
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.withStyle
object EnTranslations : Translations {
object EnStrings : Strings {
override val appName = "EhViewer"
override val siteE = "e-hentai"
override val siteEx = "exhentai"
@@ -360,7 +360,7 @@ object EnTranslations : Translations {
override val settingsAboutDeclarationSummary = "EhViewer is not affiliated with E-Hentai.org in any way"
override val settingsAboutAuthor = "Author"
override val settingsAboutAuthorSummary = buildAnnotatedString {
withStyle(SpanStyle(textDecoration = TextDecoration.Underline)) {
withStyle(SpanStyle(fontStyle = FontStyle.Italic)) {
appendLine("Hippo")
appendLine("NekoInverter")
appendLine("飛鳥澪")
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object EsTranslations : Translations by EnTranslations {
object EsStrings : Strings by EnStrings {
override val homepage = "Página de inicio"
override val subscription = "Suscripción"
override val whatsHot = "Destacado"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object FrTranslations : Translations by EnTranslations {
object FrStrings : Strings by EnStrings {
override val homepage = "Page d'acceuil"
override val subscription = "Tags suivis"
override val whatsHot = "Plus vus"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object JaTranslations : Translations by EnTranslations {
object JaStrings : Strings by EnStrings {
override val homepage = "ホーム"
override val subscription = "購読"
override val whatsHot = "人気"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object KoTranslations : Translations by EnTranslations {
object KoStrings : Strings by EnStrings {
override val homepage = "홈"
override val whatsHot = "인기 갤러리"
override val favourite = "즐겨찾기"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object NbNoTranslations : Translations by EnTranslations {
object NbNoStrings : Strings by EnStrings {
override val textIsEmpty = "Tekst tom"
override val waring = "Advarsel"
override val clipboardGalleryUrlSnackAction = "Vis"
@@ -10,7 +10,7 @@ import androidx.compose.ui.text.intl.Locale
import cafe.adriel.lyricist.ProvideStrings
import cafe.adriel.lyricist.rememberStrings
interface Translations {
interface Strings {
val appName: String
val siteE: String
val siteEx: String
@@ -612,30 +612,45 @@ object Locales {
const val ZhTw = "zh-TW"
}
val LocalTranslations: ProvidableCompositionLocal<Translations> =
staticCompositionLocalOf { EnTranslations }
val Translations = mapOf(
Locales.En to EnStrings,
Locales.De to DeStrings,
Locales.Es to EsStrings,
Locales.Fr to FrStrings,
Locales.Ja to JaStrings,
Locales.Ko to KoStrings,
Locales.NbNo to NbNoStrings,
Locales.Th to ThStrings,
Locales.Tr to TrStrings,
Locales.ZhCn to ZhCnStrings,
Locales.ZhHk to ZhHkStrings,
Locales.ZhTw to ZhTwStrings,
)
val LocalStrings: ProvidableCompositionLocal<Strings> =
staticCompositionLocalOf { EnStrings }
@Composable
fun ProvideTranslations(content: @Composable () -> Unit) = ProvideStrings(
rememberStrings(
translations = mapOf(
Locales.En to EnTranslations,
Locales.De to DeTranslations,
Locales.Es to EsTranslations,
Locales.Fr to FrTranslations,
Locales.Ja to JaTranslations,
Locales.Ko to KoTranslations,
Locales.NbNo to NbNoTranslations,
Locales.Th to ThTranslations,
Locales.Tr to TrTranslations,
Locales.ZhCn to ZhCnTranslations,
Locales.ZhHk to ZhHkTranslations,
Locales.ZhTw to ZhTwTranslations,
),
currentLanguageTag = with(Locale.current) {
listOf(language, region).mapNotNull { it.ifBlank { null } }.joinToString("-")
},
translations = Translations,
currentLanguageTag = Locale.current.resolveLanguageTag(),
),
provider = LocalTranslations,
provider = LocalStrings,
content = content,
)
fun getStrings(locale: Locale = Locale.current) = Translations[locale.resolveLanguageTag()] ?: EnStrings
fun Locale.resolveLanguageTag() = when (val language = language) {
"zh" -> when (script) {
"Hans" -> Locales.ZhCn
else -> when (region) {
"HK" -> Locales.ZhHk
"MO", "TW" -> Locales.ZhTw
else -> Locales.ZhCn
}
}
"nb" -> Locales.NbNo
else -> language
}
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object ThTranslations : Translations by EnTranslations {
object ThStrings : Strings by EnStrings {
override val homepage = "หน้าหลัก"
override val whatsHot = "เรื่องที่กำลังมาแรงมากๆ"
override val favourite = "รายการโปรด"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object TrTranslations : Translations by EnTranslations {
object TrStrings : Strings by EnStrings {
override val galleryListSearchBarHintExhentai = "ExHentai'de Arayın"
override val homepage = "Ana sayfa"
override val subscription = "Abonelikler"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object ZhCnTranslations : Translations by EnTranslations {
object ZhCnStrings : Strings by EnStrings {
override val homepage = "主页"
override val subscription = "订阅"
override val whatsHot = "热门"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object ZhHkTranslations : Translations by EnTranslations {
object ZhHkStrings : Strings by ZhTwStrings {
override val homepage = "主頁"
override val subscription = "訂閲"
override val whatsHot = "熱門"
@@ -1,6 +1,6 @@
package com.hippo.ehviewer.ui.i18n
object ZhTwTranslations : Translations by EnTranslations {
object ZhTwStrings : Strings by EnStrings {
override val homepage = "首頁"
override val subscription = "訂閱"
override val whatsHot = "熱門"
@@ -21,9 +21,9 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.hippo.ehviewer.client.parser.Archive
import com.hippo.ehviewer.client.parser.Funds
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.Strings
context(Translations)
context(Strings)
@Composable
fun ArchiveList(
funds: Funds,
@@ -46,7 +46,7 @@ import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.collectAsState
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.Strings
import com.hippo.ehviewer.ui.login.refreshAccountInfo
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.util.displayString
@@ -81,7 +81,7 @@ private val limitFlow: StateFlow<Result> = refreshEvent.conflate()
.let { src -> merge(src, invalidateEvent.map { none() }) }
.stateIn(limitScope, SharingStarted.Eagerly, none())
context(CoroutineScope, DialogState, SnackbarHostState, DestinationsNavigator, Translations)
context(CoroutineScope, DialogState, SnackbarHostState, DestinationsNavigator, Strings)
@Composable
fun AvatarIcon() {
val hasSignedIn by Settings.hasSignedIn.collectAsState()
@@ -23,9 +23,9 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hippo.ehviewer.client.parser.Torrent
import com.hippo.ehviewer.client.parser.format
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.Strings
context(Translations)
context(Strings)
@Composable
fun TorrentList(
items: List<Torrent>,
@@ -103,7 +103,7 @@ import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.ui.confirmRemoveDownload
import com.hippo.ehviewer.ui.destinations.GalleryCommentsScreenDestination
import com.hippo.ehviewer.ui.getFavoriteIcon
import com.hippo.ehviewer.ui.i18n.Translations
import com.hippo.ehviewer.ui.i18n.Strings
import com.hippo.ehviewer.ui.jumpToReaderByPage
import com.hippo.ehviewer.ui.main.ArchiveList
import com.hippo.ehviewer.ui.main.EhPreviewItem
@@ -154,7 +154,7 @@ import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
import moe.tarsin.coroutines.runSwallowingWithUI
context(CoroutineScope, DestinationsNavigator, DialogState, MainActivity, SnackbarHostState, SharedTransitionScope, TransitionsVisibilityScope, Translations)
context(CoroutineScope, DestinationsNavigator, DialogState, MainActivity, SnackbarHostState, SharedTransitionScope, TransitionsVisibilityScope, Strings)
@Composable
fun GalleryDetailContent(
galleryInfo: GalleryInfo,
@@ -379,7 +379,7 @@ fun GalleryDetailContent(
}
}
context(Context, CoroutineScope, DestinationsNavigator, DialogState, SnackbarHostState, Translations)
context(Context, CoroutineScope, DestinationsNavigator, DialogState, SnackbarHostState, Strings)
@Composable
fun BelowHeader(galleryDetail: GalleryDetail, voteTag: VoteTag) {
@Composable