From 9e058c48a6d9d2609d3d55dcabdd5ce48cbec0d7 Mon Sep 17 00:00:00 2001 From: weishu Date: Sat, 21 Oct 2023 16:28:08 +0800 Subject: [PATCH] manager: support locales for online template(2) --- .../me/weishu/kernelsu/ui/screen/Template.kt | 3 +++ .../kernelsu/ui/viewmodel/TemplateViewModel.kt | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Template.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Template.kt index 09dc1f5af285..94743d9ddecb 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Template.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Template.kt @@ -1,5 +1,6 @@ package me.weishu.kernelsu.ui.screen +import android.util.Log import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -29,6 +30,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.lifecycle.viewmodel.compose.viewModel import com.ramcosta.composedestinations.annotation.Destination @@ -37,6 +39,7 @@ import kotlinx.coroutines.launch import me.weishu.kernelsu.R import me.weishu.kernelsu.ui.screen.destinations.TemplateEditorScreenDestination import me.weishu.kernelsu.ui.viewmodel.TemplateViewModel +import java.util.Locale /** * @author weishu diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt index b30c6e3da30c..948c523352d8 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt @@ -157,6 +157,18 @@ fun getTemplateInfoById(id: String): TemplateViewModel.TemplateInfo? { }.getOrNull() } +private fun getLocaleString(json: JSONObject, key: String): String { + val fallback = json.getString(key) + val locale = Locale.getDefault() + val localeKey = "${locale.language}_${locale.country}" + json.optJSONObject("locales")?.let { locale -> + locale.optJSONObject(localeKey)?.let { + return it.optString(key, fallback) + } + } + return fallback +} + private fun fromJSON(templateJson: JSONObject): TemplateViewModel.TemplateInfo? { return runCatching { val groupsJsonArray = templateJson.getJSONArray("groups") @@ -164,9 +176,9 @@ private fun fromJSON(templateJson: JSONObject): TemplateViewModel.TemplateInfo? val rulesJsonArray = templateJson.optJSONArray("rules") val templateInfo = TemplateViewModel.TemplateInfo( id = templateJson.getString("id"), - name = templateJson.getString("name"), + name = getLocaleString(templateJson, "name"), + description = getLocaleString(templateJson, "description"), author = templateJson.optString("author"), - description = templateJson.getString("description"), local = templateJson.optBoolean("local"), namespace = Natives.Profile.Namespace.valueOf( templateJson.getString("namespace").uppercase()