Skip to content

Commit

Permalink
manager: support locales for online template(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Oct 21, 2023
1 parent e828053 commit 9e058c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,28 @@ 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")
val capabilitiesJsonArray = templateJson.getJSONArray("capabilities")
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()
Expand Down

0 comments on commit 9e058c4

Please sign in to comment.