我正在开发一个具有多种语言支持的 Kotlin Compose 多平台应用程序,包括 LTR 和 RTL 语言。当用户将应用程序语言更改为从右到左的希伯来语时,应用程序布局无法完全转换为正确显示 RTL。
我确认本地化文件已正确配置。
它在 Android 上运行良好,因此似乎是处理动态 RTL 布局更改的 iOS 问题。
关于可能导致此问题的原因或如何在语言更改时动态正确触发从右到左的布局转换有什么想法吗?谢谢!
所以,最终我做了什么:
共同点主要:
expect val platformLanguage: String?
在 androidMain 中:
actual val platformLanguage:String?
get() = Locale.getDefault().language
在 iosMain 中:
actual val platformLanguage:String?
get() = NSLocale.currentLocale.languageCode
我创建了这个包装:
@Composable
fun LayoutDirectionWrapper(content: @Composable () -> Unit) {
val layoutDirection = when (platformLanguage) {
"en" -> LayoutDirection.Ltr
"iw", "he" -> LayoutDirection.Rtl
else -> LayoutDirection.Ltr
}
CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {
content()
}
}
然后在主应用程序功能中:
@Composable
fun App() {
LayoutDirectionWrapper {
MySharedComposeContent()
}
}
在 Android 和 iOS 中都具有魅力