manager: support locales for online template(2)

This commit is contained in:
weishu
2023-10-21 16:28:08 +08:00
parent e828053439
commit 9e058c48a6
2 changed files with 17 additions and 2 deletions

View File

@@ -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

View File

@@ -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()