Modul:Entity/Message
Aus Star Citizen Wiki
Weitere Optionen
Die Dokumentation für dieses Modul kann unter Modul:Entity/Message/doc erstellt werden
require('strict')
--- @module Entity/Message
--- Localizes the display strings the Entity framework renders. The English
--- string is the key; Module:Entity/i18n.json holds German overrides only, and
--- a key absent from the dataset (or a failed dataset load) renders the English
--- argument itself, so fallback is free and no string can go missing. Strings
--- deliberately kept English (Pledge, Warbond, ...) are simply
--- absent from the dataset and render via that fallback. Only display strings
--- pass through msg(); machine tokens (bucket fields, ports category keys,
--- Registry kind names) never do, because translating them would break reads
--- and writes that agree on English. $1..$n substitution is plain string.gsub
--- rather than mw.message because the repo test runner provides no mw.message;
--- the semantics match Scribunto's $n substitution.
local p = {}
p._internal = {}
--- Dataset page: a flat map, English display string -> German display string.
local DATA_PAGE = 'Module:Entity/i18n.json'
--- Memoized dataset; nil = not loaded yet.
--- @type table<string, string>|nil
local overrides
--- The override map, loaded once per parse. loadJsonData raises on a missing or
--- moved page; the failure memoizes {} so every string falls back to English
--- instead of erroring the render.
--- @return table<string, string>
local function dataset()
if overrides == nil then
local ok, loaded = pcall(mw.loadJsonData, DATA_PAGE)
overrides = ok and type(loaded) == 'table' and loaded or {}
end
return overrides
end
--- Substitutes $1..$n from `params`, keyed by number. The replacer is a
--- function, so a param containing '%' lands literally; a string replacement
--- would be reparsed as a gsub replacement pattern. An absent param stays as
--- literal $n text.
--- @param s string
--- @param params table<number, any>
--- @return string
local function substitute(s, params)
if type(params) ~= 'table' then
return s
end
return (
string.gsub(s, '%$(%d+)', function(n)
local value = params[tonumber(n)]
if value == nil then
return '$' .. n
end
return tostring(value)
end)
)
end
--- Pure lookup: `overrides[s]` when that is a non-empty string, else `s`.
--- Non-string input passes through unchanged, so callers never need a type
--- guard. Reads only: the on-wiki table is a read-only loadJsonData proxy.
--- @param overrides table<string, string> English string -> localized string
--- @param s any
--- @return any
function p.lookup(overrides, s)
if type(s) ~= 'string' then
return s
end
local override = type(overrides) == 'table' and overrides[s] or nil
-- An empty value is a deliberate suppression (a suffix the target
-- language does not have), not a missing key; only nil falls back.
if type(override) == 'string' then
return override
end
return s
end
--- Localizes a display string: dataset lookup, then $1..$n substitution when
--- `params` is given. Returns anything but a non-empty string unchanged, and a
--- missing or malformed dataset degrades to English, because msg() wraps every
--- display string the framework renders and must never break a page.
--- @param s any The English display string.
--- @param params table<number, any>|nil Values for $1..$n.
--- @return any The localized string, or the input unchanged.
function p.msg(s, params)
if type(s) ~= 'string' or s == '' then
return s
end
return substitute(p.lookup(dataset(), s), params)
end
--- Localized noun for mid-sentence use: a dataset hit keeps the German word's
--- capitals (German nouns stay capitalized wherever they sit), while the
--- English fallback lowercases to the sentence case the short descriptions
--- compose ("S2 missile rack by BEHR" -> "S2 Raketenwerfer von BEHR").
--- @param s any
--- @return any
function p.noun(s)
if type(s) ~= 'string' or s == '' then
return s
end
local override = p.lookup(dataset(), s)
if override ~= s then
return override
end
return s:lower()
end
--- Clears the memoized dataset; test hook for the load-failure path.
function p._internal.reset()
overrides = nil
end
return p