Translate Helper
Translation utility with caching and nested translation pattern resolution.
Translation utility with caching and nested translation pattern resolution.
Overview
translateHelper wraps the engine's translateLanguage function with a cache layer and handles nested translation patterns commonly found in BeamNG content strings.
Module path: lua/ge/extensions/ui/gridSelectorUtils/translateHelper.lua (loaded via require)
Exports (M)
| Function | Signature | Description |
|---|---|---|
translate | (key) | Translates a key, resolves nested patterns, caches results. |
Internals
Basic Translation
local function translate(key)
if not key or type(key) ~= "string" then
return key or "Missing Translation Key!"
end
if translateCache[key] then return translateCache[key] end
local translated = translateLanguage(key, key, true)
translated = processNestedTranslations(translated)
translateCache[key] = translated
return translated
endNested Translation Patterns
Handles Angular-style patterns found in BeamNG translation strings:
| Pattern | Example | Behavior |
|---|---|---|
{{ 'key' | translate}} | {{ 'ui.menu.title' | translate}} | Quoted key with pipe |
{{:: 'key' | translate}} | {{:: 'ui.label' | translate}} | One-time binding syntax |
{{ key | translate}} | {{ ui.button | translate}} | Unquoted key with pipe |
{{key}} | {{ui.name}} | Simple variable substitution |
Recursive Resolution
Nested patterns are resolved iteratively (up to 10 iterations) to handle translations that contain further translation keys:
local changed = true
local iterations = 0
while changed and iterations < maxIterations do
changed = false
iterations = iterations + 1
-- Process each pattern type...
endCaching
Results are cached in translateCache keyed by the original input string. The cache persists for the module's lifetime.
Usage in Grid Selectors
local translate = require("ge/extensions/ui/gridSelectorUtils/translateHelper").translate
-- Translate item names
item.name = translate(campaign.title)
item.level = translate(core_levels.getLevelTitle(item.level))
-- Translate specification labels
{label = translate("ui.common.property.difficulty"), value = translate(difficultyValue)}How It Works
- Caller passes a translation key (e.g.,
"ui.menu.start") - Cache is checked first for instant return
translateLanguage(key, key, true)queries the engine's localization system- Result is scanned for nested
{{ ... | translate}}patterns - Each nested pattern is recursively translated (up to 10 iterations)
- Final result is cached and returned
Additional Exports
The following exports are available but not yet documented in detail:
M.translate