-
Notifications
You must be signed in to change notification settings - Fork 180
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 #726 from myoshita/fix/language-settings
Fix language settings
- Loading branch information
Showing
16 changed files
with
149 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,6 @@ data class MultiLangText( | |
enTitle | ||
} | ||
} | ||
|
||
companion object | ||
} |
28 changes: 28 additions & 0 deletions
28
...n/java/io/github/droidkaigi/feeder/core/language/LangSettingProvidableCompositionLocal.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,28 @@ | ||
package io.github.droidkaigi.feeder.core.language | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.compositionLocalOf | ||
import io.github.droidkaigi.feeder.Lang | ||
|
||
val LocalLangSetting = LangSettingProvidableCompositionLocal() | ||
|
||
@Composable | ||
fun ProvideLangSetting(language: Lang?, content: @Composable () -> Unit) { | ||
CompositionLocalProvider(LocalLangSetting provides language) { | ||
content() | ||
} | ||
} | ||
|
||
@JvmInline | ||
value class LangSettingProvidableCompositionLocal internal constructor( | ||
private val delegate: ProvidableCompositionLocal<Lang?> = compositionLocalOf { null }, | ||
) { | ||
val current: Lang | ||
@Composable get() = delegate.current ?: Lang.SYSTEM | ||
|
||
infix fun provides(value: Lang?) = delegate provides value | ||
|
||
infix fun providesDefault(value: Lang?) = delegate providesDefault value | ||
} |
17 changes: 11 additions & 6 deletions
17
...ent-compose/core/src/main/java/io/github/droidkaigi/feeder/core/language/LanguageTitle.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 |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package io.github.droidkaigi.feeder.core.language | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import io.github.droidkaigi.feeder.Lang | ||
import io.github.droidkaigi.feeder.MultiLangText | ||
import io.github.droidkaigi.feeder.core.R | ||
|
||
fun Lang?.getTitle(context: Context): String = when (this) { | ||
Lang.SYSTEM -> context.getString(R.string.system) | ||
Lang.JA -> context.getString(R.string.ja) | ||
Lang.EN -> context.getString(R.string.en) | ||
else -> context.getString(R.string.system) | ||
@Composable | ||
fun Lang?.getTitle(): String { | ||
val res = when (this) { | ||
Lang.SYSTEM -> R.string.system | ||
Lang.JA -> R.string.ja | ||
Lang.EN -> R.string.en | ||
else -> R.string.system | ||
} | ||
return MultiLangText.from(res).getTextWithSetting() | ||
} |
19 changes: 13 additions & 6 deletions
19
uicomponent-compose/core/src/main/java/io/github/droidkaigi/feeder/core/theme/ThemeTitle.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 |
---|---|---|
@@ -1,12 +1,19 @@ | ||
package io.github.droidkaigi.feeder.core.theme | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import io.github.droidkaigi.feeder.MultiLangText | ||
import io.github.droidkaigi.feeder.Theme | ||
import io.github.droidkaigi.feeder.core.R | ||
import io.github.droidkaigi.feeder.core.language.from | ||
import io.github.droidkaigi.feeder.core.language.getTextWithSetting | ||
|
||
fun Theme?.getTitle(context: Context): String = when (this) { | ||
Theme.SYSTEM -> context.getString(R.string.system) | ||
Theme.DARK -> context.getString(R.string.dark) | ||
Theme.LIGHT -> context.getString(R.string.light) | ||
else -> context.getString(R.string.system) | ||
@Composable | ||
fun Theme?.getTitle(): String { | ||
val res = when (this) { | ||
Theme.SYSTEM -> R.string.system | ||
Theme.DARK -> R.string.dark | ||
Theme.LIGHT -> R.string.light | ||
else -> R.string.system | ||
} | ||
return MultiLangText.from(res).getTextWithSetting() | ||
} |
34 changes: 34 additions & 0 deletions
34
...nent-compose/core/src/main/java/io/github/droidkaigi/feeder/core/util/MultiLangTextExt.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,34 @@ | ||
package io.github.droidkaigi.feeder.core.language | ||
|
||
import android.content.res.Configuration | ||
import androidx.annotation.StringRes | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
import io.github.droidkaigi.feeder.Lang | ||
import io.github.droidkaigi.feeder.MultiLangText | ||
import java.util.Locale | ||
|
||
@Composable | ||
fun MultiLangText.getTextWithSetting(): String { | ||
return when (val lang = LocalLangSetting.current) { | ||
Lang.SYSTEM -> currentLangTitle | ||
else -> getByLang(lang) | ||
} | ||
} | ||
|
||
@Composable | ||
fun MultiLangText.Companion.from( | ||
@StringRes res: Int, | ||
): MultiLangText { | ||
val context = LocalContext.current | ||
val jaConfiguration = Configuration(context.resources.configuration).apply { | ||
setLocale(Locale.JAPAN) | ||
} | ||
val enConfiguration = Configuration(context.resources.configuration).apply { | ||
setLocale(Locale.ENGLISH) | ||
} | ||
return MultiLangText( | ||
jaTitle = context.createConfigurationContext(jaConfiguration).getString(res), | ||
enTitle = context.createConfigurationContext(enConfiguration).getString(res), | ||
) | ||
} |
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
Oops, something went wrong.