Skip to content

feat(locale): support nested key references in phrase interpolation#766

Merged
thelindat merged 1 commit into
overextended:mainfrom
Kenshiin13:locale
May 2, 2026
Merged

feat(locale): support nested key references in phrase interpolation#766
thelindat merged 1 commit into
overextended:mainfrom
Kenshiin13:locale

Conversation

@Kenshiin13
Copy link
Copy Markdown
Contributor

Description

Locale files can already contain nested objects, and locale('parent.child') resolves them correctly because flattenDict runs at load time. Phrase interpolation with ${...} is the only place this breaks down: the resolver looks names up in the un-flattened nested table, so ${parent.child} silently fails to substitute even though the same key is reachable via a direct locale() call.

for k, v in pairs(flattenDict(locales, {})) do        -- iterates flat keys
    if type(v) == 'string' then
        for var in v:gmatch('${[%w%s%p]-}') do
            local locale = locales[var:sub(3, -2)]    -- looks up un-flattened tree

This patch threads the already-flattened dict through the resolver so both lookup paths behave the same. Top-level references keep working; nested references start working as a side effect.

Example

locales/en.json:

{
  "greeting": "Hello",
  "user": { "name": "Kenshin13" },
  "msg": "${greeting}, ${user.name}!"
}
print(locale('msg'))
-- before: Hello, ${user.name}!
-- after:  Hello, Kenshin13!

@thelindat thelindat merged commit a548ec2 into overextended:main May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants