Skip to content

Commit

Permalink
feat: 支持自动识别国际话语言并切换
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiaoyong committed Mar 30, 2024
1 parent add995e commit 0868c40
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ APK 文件。
- [ ] 优化签名配置
- [ ] 美化主题
- [ ] 添加查看日志功能
- [ ] 支持国际化语言 🚧进行中

## 界面预览

Expand Down
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
plugins {
kotlin("jvm")
id("org.jetbrains.compose")
id("com.google.devtools.ksp")
}

group = "io.github.jixiaoyong"
Expand All @@ -15,6 +16,7 @@ repositories {
maven("https://jitpack.io")
}
val multiplatformSettings = "1.1.1"
val lyricist = "1.6.2-1.8.20"
dependencies {
// Note, if you develop a library, you should use compose.desktop.common.
// compose.desktop.currentOs should be used in launcher-sourceSet
Expand All @@ -26,6 +28,11 @@ dependencies {
implementation("com.russhwolf:multiplatform-settings-serialization:$multiplatformSettings")
implementation("com.russhwolf:multiplatform-settings-coroutines:$multiplatformSettings")
implementation("com.github.Dansoftowner:jSystemThemeDetector:3.6")
implementation("com.google.devtools.ksp:symbol-processing-api:1.9.21-1.0.15")
// Required
implementation("cafe.adriel.lyricist:lyricist:$lyricist")
// If you want to use @LyricistStrings to generate code for you
ksp("cafe.adriel.lyricist:lyricist-processor:$lyricist")
}

compose.desktop {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
kotlin.code.style=official
kotlin.version=1.8.20
compose.version=1.5.0
ksp.version=1.8.20-1.0.10
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

plugins {
kotlin("jvm").version(extra["kotlin.version"] as String)
id("org.jetbrains.compose").version(extra["compose.version"] as String)
id("com.google.devtools.ksp").version(extra["ksp.version"] as String)
}
}

Expand Down
26 changes: 18 additions & 8 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import cafe.adriel.lyricist.Lyricist
import cafe.adriel.lyricist.rememberStrings
import io.github.jixiaoyong.beans.AppState
import io.github.jixiaoyong.i18n.EnStrings
import io.github.jixiaoyong.i18n.Locales
import io.github.jixiaoyong.i18n.Strings
import io.github.jixiaoyong.i18n.ZhStrings
import io.github.jixiaoyong.pages.App
import io.github.jixiaoyong.utils.AppProcessUtil
import io.github.jixiaoyong.utils.SettingsTool
Expand All @@ -28,6 +34,7 @@ import kotlin.system.exitProcess

val LocalWindow = compositionLocalOf<ComposeWindow> { error("No Window provided") }
val LocalSettings = compositionLocalOf<SettingsTool> { error("No SettingsTool provided") }
val LocalStrings = compositionLocalOf<Lyricist<Strings>> { error("No SettingsTool provided") }

fun main() =
application {
Expand All @@ -41,19 +48,21 @@ fun main() =
var appState by remember { mutableStateOf<AppState>(AppState.Idle) }
var checkDualRunning by remember { mutableStateOf(true) }

val lyricist = rememberStrings(mapOf(Locales.ZH to ZhStrings, Locales.EN to EnStrings))

LaunchedEffect(Unit) {
window.minimumSize = window.size
}
CompositionLocalProvider(
LocalWindow provides window,
LocalSettings provides SettingsTool(scope = rememberCoroutineScope()),
LocalStrings provides lyricist
) {
LaunchedEffect(checkDualRunning) {
appState = AppState.Loading
val isAppRunning =
withContext(Dispatchers.IO) {
AppProcessUtil.isDualAppRunning("ApkSigner")
}
val isAppRunning = withContext(Dispatchers.IO) {
AppProcessUtil.isDualAppRunning("ApkSigner")
}
appState =
if (isAppRunning) {
AppState.AlreadyExists
Expand Down Expand Up @@ -89,12 +98,13 @@ fun LoadingPage() {
verticalArrangement = Arrangement.Center,
) {
CircularProgressIndicator()
Text("Loading...", Modifier.padding(top = 10.dp))
Text(LocalStrings.current.strings.loading, Modifier.padding(top = 10.dp))
}
}

@Composable
fun AlreadyExistsPage(tryAgainFunc: () -> Unit) {
val i18nString = LocalStrings.current.strings
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
Expand All @@ -114,13 +124,13 @@ fun AlreadyExistsPage(tryAgainFunc: () -> Unit) {
contentDescription = "already exists",
modifier = Modifier.size(50.dp),
)
Text("ApkSigner已经启动了,请不要重复启动", Modifier.padding(vertical = 20.dp))
Text(i18nString.alreadyRunning, Modifier.padding(vertical = 20.dp))
Row {
ButtonWidget(onClick = { exitProcess(0) }) {
Text("退出")
Text(i18nString.exit)
}
ButtonWidget(onClick = tryAgainFunc) {
Text("重试")
Text(i18nString.retry)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/io/github/jixiaoyong/i18n/Locales.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.jixiaoyong.i18n

/**
* @author : jixiaoyong
* @description :TODO
*
* @email : jixiaoyong1995@gmail.com
* @date : 30/3/2024
*/
object Locales {
const val EN = "en"
const val ZH = "zh"
}
46 changes: 46 additions & 0 deletions src/main/kotlin/io/github/jixiaoyong/i18n/StringEn.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.jixiaoyong.i18n

import cafe.adriel.lyricist.LyricistStrings

/**
* @author : jixiaoyong
* @description :英文翻译
*
* @email : jixiaoyong1995@gmail.com
* @date : 30/3/2024
*/
@LyricistStrings(languageTag = Locales.EN)
internal val EnStrings = Strings(
loading = "Loading...",
alreadyRunning = "Apk Signer has been started, please do not start it again.",
exit = "Quit",
retry = "Retry",
signConfig = "Signature information",
signApp = "Signature APP",
settingsConfig = "Settings Config"
)

//internal val EnStrings = Strings(
// simple = "Hello Compose!",
//
// annotated = buildAnnotatedString {
// withStyle(SpanStyle(color = Color.Red)) { append("Hello ") }
// withStyle(SpanStyle(fontWeight = FontWeight.Light)) { append("Compose!") }
// },
//
// parameter = { locale ->
// "Current locale: $locale"
// },
//
// plural = { count ->
// val value = when (count) {
// 0 -> "no"
// 1, 2 -> "a few"
// in 3..10 -> "a bunch of"
// else -> "a lot of"
// }
// "I have $value apples"
// },
//
// list = listOf("Avocado", "Pineapple", "Plum")
//)
21 changes: 21 additions & 0 deletions src/main/kotlin/io/github/jixiaoyong/i18n/StringZh.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.jixiaoyong.i18n

import cafe.adriel.lyricist.LyricistStrings

/**
* @author : jixiaoyong
* @description :中文翻译
*
* @email : jixiaoyong1995@gmail.com
* @date : 30/3/2024
*/
@LyricistStrings(languageTag = Locales.ZH, default = true)
internal val ZhStrings = Strings(
loading = "加载中……",
alreadyRunning = "ApkSigner已经启动了,请不要重复启动",
exit = "退出",
retry = "重试",
signConfig = "签名信息",
signApp = "签名APP",
settingsConfig = "设置信息"
)
18 changes: 18 additions & 0 deletions src/main/kotlin/io/github/jixiaoyong/i18n/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.jixiaoyong.i18n

/**
* @author : jixiaoyong
* @description :APP 用到的所有字符抽象类
*
* @email : jixiaoyong1995@gmail.com
* @date : 30/3/2024
*/
data class Strings(
val loading: String,
val alreadyRunning: String,
val exit: String,
val retry: String,
val signConfig: String,
val signApp: String,
val settingsConfig: String,
)
5 changes: 3 additions & 2 deletions src/main/kotlin/io/github/jixiaoyong/pages/MainPage.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jixiaoyong.pages

import LocalSettings
import LocalStrings
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -35,9 +36,10 @@ import kotlinx.coroutines.launch
@Composable
fun App() {
val settings = LocalSettings.current
val i18nString = LocalStrings.current

val scope = rememberCoroutineScope()
val viewModel = viewModel { MainViewModel(settings) }
val viewModel = viewModel { MainViewModel(settings, i18nString.strings) }

var isDarkTheme by viewModel.isDarkTheme
val pageIndex by viewModel.currentIndex.collectAsState()
Expand All @@ -57,7 +59,6 @@ fun App() {
detector?.registerListener(listener)
detector?.isDark?.let {
isDarkTheme = it

}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/io/github/jixiaoyong/pages/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.mutableStateOf
import io.github.jixiaoyong.base.BaseViewModel
import io.github.jixiaoyong.i18n.Strings
import io.github.jixiaoyong.utils.SettingsTool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -20,12 +21,12 @@ import kotlinx.coroutines.launch
* @email : jixiaoyong1995@gmail.com
* @date : 25/3/2024
*/
class MainViewModel(private val settings: SettingsTool) : BaseViewModel() {
class MainViewModel(private val settings: SettingsTool, i18nString: Strings) : BaseViewModel() {
val routes =
listOf(
Triple(Icons.Default.List, "签名信息", Routes.SignInfo),
Triple(Icons.Default.Lock, "签名APP", Routes.SignApp),
Triple(Icons.Default.Settings, "设置信息", Routes.SettingInfo),
Triple(Icons.Default.List, i18nString.signConfig, Routes.SignInfo),
Triple(Icons.Default.Lock, i18nString.signApp, Routes.SignApp),
Triple(Icons.Default.Settings, i18nString.settingsConfig, Routes.SettingInfo),
)

val currentIndex = MutableStateFlow(Routes.SignInfo)
Expand Down

0 comments on commit 0868c40

Please sign in to comment.