Lang: get-from-config tags

This commit is contained in:
minjaesong
2024-03-15 01:45:35 +09:00
parent c0a8118717
commit b0adc9efc7
5 changed files with 62 additions and 16 deletions

View File

@@ -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 youve 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",

View File

@@ -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": "벽을 부숩니다",

View File

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

View File

@@ -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<String/*Locale*/, HashMap<String/*Key*/, String/*Text*/>>()
private val decodeCache = HashMap<String/*Locale*/, HashMap<String/*Key*/, String/*Text*/>>()
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()

View File

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