Files
windroost/p2_godot_sample/scripts/ui_text.gd
T
wushenghua 4f4b7d918a fix(ui): polish skill bar, victory modal, 360px underlines and raw keys
- battle manual actions now share one skill bar backing with accent rules and
  dividers; button rects, mouse_filter and callbacks unchanged
- victory/dialog panel is opaque with a darker dim so battle chrome no longer
  shows through; action labels and 44px hit areas unchanged
- idle underline and tab baselines brightened/thickened for 360px readability
- UiText.node_name() falls back to localized chapter theme + node number, so
  full-game nodes no longer render [content.node.*] raw keys; locale keys intact

Evidence: 9ba98ef-polish before/after runtime captures, strict import and
focused touch/UI regressions all PASS. No merge, push or release.
2026-11-29 08:23:08 +08:00

123 lines
3.8 KiB
GDScript

class_name UiText
extends RefCounted
## Runtime string table for the Windroost UI.
##
## Every visible string comes from a key in `data/ui_text.<locale>.json`.
## Seven-locale schema is fixed on day one (ADR-009); only zh_CN and en_US ship
## in the vertical slice (ADR-016). A missing key in the active locale falls
## back to zh_CN and then to the key itself, so a missing translation degrades
## to a visible marker instead of a crash.
const DATA_DIR := "res://data/"
const FALLBACK_LOCALE := "zh_CN"
const SUPPORTED_LOCALES := ["zh_CN", "en_US", "es", "pt-BR", "ja", "fr", "de"]
static var _tables: Dictionary = {}
static var _missing: Dictionary = {}
static func available_locales() -> Array[String]:
var locales: Array[String] = []
for path in _locale_paths():
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
continue
var parsed = JSON.parse_string(file.get_as_text())
file.close()
if parsed is Dictionary:
locales.append(str(parsed.get("locale", "")))
return locales
static func _locale_paths() -> Array[String]:
var paths: Array[String] = []
for locale in SUPPORTED_LOCALES:
paths.append(DATA_DIR + "ui_text.%s.json" % locale)
return paths
static func _load_table(locale: String) -> Dictionary:
if _tables.has(locale):
return _tables[locale]
var path := DATA_DIR + "ui_text.%s.json" % locale
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
push_warning("UI text table missing: %s" % path)
_tables[locale] = {}
return {}
var parsed = JSON.parse_string(file.get_as_text())
file.close()
var table: Dictionary = {}
if parsed is Dictionary:
table = parsed.get("strings", {})
_tables[locale] = table
return table
static func t(key: String, locale: String = FALLBACK_LOCALE) -> String:
var table := _load_table(locale)
if table.has(key):
return str(table[key])
if locale != FALLBACK_LOCALE:
var fallback := _load_table(FALLBACK_LOCALE)
if fallback.has(key):
return str(fallback[key])
_missing[key] = true
return "[%s]" % key
## Display name for a world node. Only the launch slice has authored
## `content.node.<id>` strings; full-game chapters fall back to the chapter's
## existing localized theme name plus the node number. No locale key is added,
## renamed or removed, and a bracketed raw key never reaches a label.
static func node_name(node_id: String, locale: String = FALLBACK_LOCALE) -> String:
var key := "content.node.%s" % node_id
if has_key(key, locale):
return t(key, locale)
var parts := node_id.split("-")
var chapter := node_id
if parts.size() >= 2:
chapter = "%s-%s" % [parts[0], parts[1]]
var theme := t("world.theme.%s" % chapter, locale)
if theme.begins_with("[") and theme.ends_with("]"):
theme = chapter
var number := ""
if parts.size() >= 3:
var tail := str(parts[2])
if tail.begins_with("N"):
number = tail.substr(1)
if number.is_empty():
return theme
return "%s %s" % [theme, number]
static func tf(key: String, args: Dictionary, locale: String = FALLBACK_LOCALE) -> String:
var template := t(key, locale)
var result := template
for arg_key in args.keys():
result = result.replace("{%s}" % str(arg_key), str(args[arg_key]))
return result
## Return the first key that exists. Used for optional variants such as a
## short display name falling back to the full name.
static func t_any(keys: Array, locale: String = FALLBACK_LOCALE) -> String:
for key in keys:
var candidate := str(key)
if has_key(candidate, locale):
return t(candidate, locale)
return t(str(keys[0]) if not keys.is_empty() else "", locale)
static func missing_keys() -> Array[String]:
var keys: Array[String] = []
for key in _missing.keys():
keys.append(str(key))
keys.sort()
return keys
static func has_key(key: String, locale: String = FALLBACK_LOCALE) -> bool:
return _load_table(locale).has(key)