From b0adc9efc7eb74f3885b37d5dd8f7842de9d79f7 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 15 Mar 2024 01:45:35 +0900 Subject: [PATCH] Lang: get-from-config tags --- assets/mods/basegame/locales/en/itemdesc.json | 2 +- .../mods/basegame/locales/koKR/itemdesc.json | 2 +- src/net/torvald/terrarum/App.java | 2 +- src/net/torvald/terrarum/langpack/Lang.kt | 70 +++++++++++++++---- .../terrarum/ui/UIItemInventoryElemSimple.kt | 2 +- 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/assets/mods/basegame/locales/en/itemdesc.json b/assets/mods/basegame/locales/en/itemdesc.json index 7a4fa86bf..c11573660 100644 --- a/assets/mods/basegame/locales/en/itemdesc.json +++ b/assets/mods/basegame/locales/en/itemdesc.json @@ -19,7 +19,7 @@ "TOOLTIP_item@basegame:37": "“I am sworn to keep your burdens.”", /* skyrim/lydia reference */ "TOOLTIP_item@basegame:38": "“I am sworn to keep your burdens.”", /* skyrim/lydia reference */ "TOOLTIP_item@basegame:39": "“I am sworn to keep your burdens.”", /* skyrim/lydia reference */ - "TOOLTIP_item@basegame:9": "An electricians’ best friend", + "TOOLTIP_item@basegame:9": "Push {MOUSE:config_mousesecondary} to change the colour", "TOOLTIP_item@basegame:10": "“Who needs a Book and Quill when you’ve got this?”", /* a jab on the Minecraft item Book and Quill */ "TOOLTIP_item@basegame:11": "Tells what day it is", "TOOLTIP_item@basegame:12": "Breaks down walls", diff --git a/assets/mods/basegame/locales/koKR/itemdesc.json b/assets/mods/basegame/locales/koKR/itemdesc.json index 9944ba82e..c7a1f82de 100644 --- a/assets/mods/basegame/locales/koKR/itemdesc.json +++ b/assets/mods/basegame/locales/koKR/itemdesc.json @@ -19,7 +19,7 @@ "TOOLTIP_item@basegame:37": "“짐을 맡아두기로 한 몸이니까요.”", /* skyrim/lydia reference */ "TOOLTIP_item@basegame:38": "“짐을 맡아두기로 한 몸이니까요.”", /* skyrim/lydia reference */ "TOOLTIP_item@basegame:39": "“짐을 맡아두기로 한 몸이니까요.”", /* skyrim/lydia reference */ - "TOOLTIP_item@basegame:9": "전기공의 친한 친구", + "TOOLTIP_item@basegame:9": "{MOUSE:config_mousesecondary} 버튼을 눌러 색깔 변경", "TOOLTIP_item@basegame:10": "“이게 있는데 책과 깃펜이 왜 필요하죠?”", /* a jab on the Minecraft item Book and Quill */ "TOOLTIP_item@basegame:11": "오늘이 무슨 날인지 알려줍니다", "TOOLTIP_item@basegame:12": "벽을 부숩니다", diff --git a/src/net/torvald/terrarum/App.java b/src/net/torvald/terrarum/App.java index 89a4c5fe7..81118fdf8 100644 --- a/src/net/torvald/terrarum/App.java +++ b/src/net/torvald/terrarum/App.java @@ -1726,7 +1726,7 @@ public class App implements ApplicationListener { return DefaultConfig.INSTANCE.getHashMap(); } - private static Object getConfigMaster(String key1) { + public static Object getConfigMaster(String key1) { String key = key1.toLowerCase(); Object config; diff --git a/src/net/torvald/terrarum/langpack/Lang.kt b/src/net/torvald/terrarum/langpack/Lang.kt index df37294ea..2a07c1a2e 100644 --- a/src/net/torvald/terrarum/langpack/Lang.kt +++ b/src/net/torvald/terrarum/langpack/Lang.kt @@ -2,9 +2,13 @@ package net.torvald.terrarum.langpack import net.torvald.terrarum.App import net.torvald.terrarum.App.printdbg +import net.torvald.terrarum.tail import net.torvald.terrarum.utils.JsonFetcher +import net.torvald.unicode.getKeycapPC +import net.torvald.unicode.getMouseButton import java.io.File import java.util.* +import kotlin.collections.HashMap class LangObject(val key: String, val fromLang: Boolean) { fun get() = if (fromLang) Lang[key] else key @@ -137,21 +141,33 @@ object Lang { operator fun get(key: String, capitalise: Boolean = false): String { fun getstr(s: String) = getByLocale(s, App.GAME_LOCALE, capitalise) ?: getByLocale(s, FALLBACK_LANG_CODE, capitalise) ?: "$$s" + decodeCache[App.GAME_LOCALE]?.get("$key+$capitalise").let { + if (it != null) { + return it + } + else { + val args = key.split(bindOp).filter { it.isNotBlank() }.map { it.trim() } + if (args.isEmpty()) return "" - val args = key.split(bindOp).filter { it.isNotBlank() }.map { it.trim() } - if (args.isEmpty()) return "" + val sb = StringBuilder() + val formatter = Formatter(sb) - val sb = StringBuilder() - val formatter = Formatter(sb) + sb.append(getstr(args[0])) + args.subList(1, args.size).forEach { + val oldstr = sb.toString() + sb.clear() + formatter.format(getstr(it), oldstr) + } - sb.append(getstr(args[0])) - args.subList(1, args.size).forEach { - val oldstr = sb.toString() - sb.clear() - formatter.format(getstr(it), oldstr) + if (decodeCache[App.GAME_LOCALE] == null) { + decodeCache[App.GAME_LOCALE] = HashMap() + } + decodeCache[App.GAME_LOCALE]!!["$key+$capitalise"] = sb.toString() + + return sb.toString() + } } - return sb.toString() } fun getAndUseTemplate(key: String, capitalise: Boolean = false, vararg arguments: Any?): String { @@ -174,6 +190,7 @@ object Lang { } private val capCache = HashMap>() + private val decodeCache = HashMap>() private fun CAP(key: String, locale: String): String? { val ret = langpack["${key}_$locale"] ?: return null @@ -191,20 +208,49 @@ object Lang { private fun NOCAP(key: String, locale: String): String? { return langpack["${key}_$locale"] ?: return null } + + + private val tagRegex = Regex("""\{[A-Z]+:[0-9A-Za-z_\- ]+\}""") + /** - * Does NOT parse the operators + * Does NOT parse the bind operators, but DO parse the precomposed tags */ fun getByLocale(key: String, locale: String, capitalise: Boolean = false): String? { val s = if (capitalise) CAP(key, locale) else NOCAP(key, locale) if (s == null) return null - return if (locale.startsWith("bg")) + val fetchedS = if (locale.startsWith("bg")) "${App.fontGame.charsetOverrideBulgarian}$s${App.fontGame.charsetOverrideDefault}" else if (locale.startsWith("sr")) "${App.fontGame.charsetOverrideSerbian}$s${App.fontGame.charsetOverrideDefault}" else s + + // apply {DOMAIN:argument} form of template + var ret = "$fetchedS" // make copy of the str + tagRegex.findAll(fetchedS).forEach { + val matched0 = it.groupValues[0] + val matched = matched0.substring(1 until matched0.lastIndex) // strip off the brackets + val mode = matched.substringBefore(':') + val key = matched.substringAfter(':') + + val resolved = when (mode) { + "MOUSE" -> { // cognates to gdx.Input.Button + getMouseButton(App.getConfigInt(key)).toString() + } + "KEYCAP" -> { // cognates to gdx.Input.Keys + getKeycapPC(App.getConfigInt(key)).toString() + } + "CONFIG" -> { + App.getConfigMaster(key).toString() + } + else -> matched0 + } + + ret = ret.replace(matched0, resolved) + } + return ret } private fun String.getEndTag() = this.split("_").last() diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt index 937e0cea5..af83ac398 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt @@ -139,7 +139,7 @@ class UIItemInventoryElemSimple( val itemIDstr = "\n$grey(${item?.originalID}${if (item?.isCurrentlyDynamic == true) "/${item?.dynamicID}" else ""})" val nameStr0 = if (item?.nameSecondary?.isNotBlank() == true) "${item?.name}\n$grey${item?.nameSecondary}" else "${item?.name}" val nameStr = if (Gdx.input.isKeyPressed(Input.Keys.ALT_LEFT)) nameStr0 + itemIDstr else nameStr0 - val descStr = Lang.getOrNull("TOOLTIP_${item?.originalID}")?.replace("\n","\n$grey") + val descStr = Lang.getAndUseTemplate("TOOLTIP_${item?.originalID}")?.replace("\n","\n$grey") val finalStr = if (descStr != null) "$nameStr\n$grey$descStr" else nameStr