Skip to content

feat(core)!: support arrays in locale files - #179

Open
0froq wants to merge 11 commits into
mainfrom
fix/json-array-messages
Open

0froq wants to merge 11 commits into
mainfrom
fix/json-array-messages

Conversation

@0froq

@0froq 0froq commented Nov 27, 2026

Copy link
Copy Markdown
Member

This description was drafted by an agent from the diff and the commands it ran. froQ has reviewed it.

Summary

tm('home.benefits.list') on an array in a JSON locale file reported missing-key. The JSON reader skipped arrays, so the message tree had no array nodes for a usage to match.

The message tree now holds arrays, and json and jsonc read and write them by the shape the file has. An item has the key of its index (list.0, steps.0.title).

  • Deleting an unused item follows one rule, planArrayDelete, shared by the tree operations and the in-place JSON edit. An item in the middle is emptied so the other indexes stay. The last item is cut off together with the empty items next to it. When nothing is left, the array is removed.
  • A locale that lacks an array gets one built like the source locale's. A new item goes right after the last one. A key that would leave a gap, or a name that is not an index and would replace the array, is skipped with a warning. Rename keeps arrays, and an item moves by the same delete and add rules.
  • A call that takes the subtree counts everything below its key as used: vue-i18n tm and $tm, and i18next t(key, { returnObjects }). A call that names an array counts the whole array, since the items of an array have no names to read them by.
  • vue-i18n paths written with brackets, such as list[0] and a["b"], match the keys list.0 and a.b.
  • The language server offers no quick fix that would change nothing.

Related issue

Closes #172

Evidence

The new tests group by behavior.

  • Tree and format: arrays are read by shape at any depth, an object with the keys "0" and "1" stays an object, and every delete case gives the same result through the in-place edit and through the tree operations. Deleting the last item of ["", "B", "", "D"] leaves ["", "B"].
  • Planner: vue-i18n projects with JSON files, edited in place and written in full (sorted), cover Delete Unused Keys, adding translations, sorting, rename and the skipped writes above.
  • Server and CLI: the language server over JSON-RPC and the built x18n binary cover diagnostics, hover, annotations, quick fixes, check --fix, delete --unused and set on arrays. The scenario from the issue is pinned by check finds no missing key and no unused item for tm() and a bracket path in packages/cli/test/arrays.test.ts.

The same commands on the base (da68e6c7) and on this branch, in a temporary project with vue-i18n 11 and home.benefits.list: ["Fast", "Small"] in en.json:

x18n check              (code has tm('home.benefits.list'))
base      ERROR "home.benefits.list" is not defined in en.  missing-key   exit 1
branch    Found no errors.                                                exit 0

x18n set home.benefits.list.0 --locale de --value Schnell
x18n set home.benefits.list.1 --locale de --value Klein      (de.json had no list)
base      "list": { "0": "Schnell", "1": "Klein" }
branch    "list": [ "Schnell", "Klein" ]

x18n set home.benefits.list.9 --locale en --value X --dry-run
base      - "list": ["Fast", "Small"]   + "list": { "9": "X" }
branch    warning: "home.benefits.list.9" is not added: it would leave a gap in an array in src/locales/en.json. An item is added right after the last one.

Nothing was run in VS Code or the web UI. The editor behavior is covered by the language server tests.

Merge Danger

Door: two-way

Blast Radius: core, JSON locale files, vue-i18n and i18next projects

The first commit is marked breaking, and the break is narrow. The values of a MessageTree can be arrays, so code that reads those values has to check for an object with isMessageObject before it reads fields. Code that only builds trees or passes them on is not affected. Only JSON files produce arrays. The PR adds MessageNode, ArrayDeletePlan, planArrayDelete, isMessageObject, the shape field of set ops and CallSpec.subtree (a boolean, or { option }).

Arrays in JSON files were skipped before and are keys now. A project that reads an array in a way x18n cannot follow will see its items as unused, for example t('home', options) on the parent object, or returnObjects set in the init code. x18n delete --unused --dry-run lists them before anything is deleted.

@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
x18n Error Error Nov 27, 2026 1:37am UTC

Request Review

@0froq

0froq commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

@antfubot Can you please review this PR? (don't know if mentioning you will work :P

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.

Support array values in locale files (vue-i18n tm() returning arrays)

1 participant