oficina / docs Public

Docs native: style menu shows Heading 1–3, not string keys open

On branch fix/native-interpolated-string-keys

Paul's screenshot (Docs Mac 0.1.9): the block style menu lists "docs.editor.toolbar.block.heading1/2/3" instead of "Heading 1/2/3". Cause: EditorToolbar.swift:18 built the key as LocalizedStringKey("docs.editor.toolbar.block.heading\(level)"); SwiftUI turns the interpolation into a format placeholder, looks up "…heading%lld", finds nothing, shows the raw key. The translations exist in all 12 languages. Fix: build the key as a plain String (EditorToolbar.headingKey) first. New EditorToolbarStringsTests checks every heading key resolves (and isn't a format) in every shipped language — it guards the keys and translations; the interpolation trap itself is guarded by the helper (a SwiftUI lookup isn't observable in a unit test). Grep found no other interpolated localisation keys in native/apple/Sources. Requesting Fable review.

Review by Claude Fable (model id claude-fable-5-2), read-only on 21b4b0f over main 2e21630.

Verdict: APPROVE

Diagnosis: right, confirmed by running it. On this Mac (macOS 27.0, Swift 6.4.2) I reflected the two keys:

  • LocalizedStringKey("docs.editor.toolbar.block.heading\(level)") gives key docs.editor.toolbar.block.heading%lld, hasFormatting=true, one argument.
  • LocalizedStringKey("docs.editor.toolbar.block.heading" + String(level)) gives key docs.editor.toolbar.block.heading2, hasFormatting=false, no arguments.

No table has "…heading%lld", so SwiftUI showed the key.

Fix: correct on macOS and iOS. LocalizedStringKey.init(_ value: String) with a non-literal String uses it verbatim as the key (even a % in it is not treated as a format). That is the same SwiftUI initialiser on both platforms, and the file already relies on it at lines 38, 45, 67 and 70. The call must never go back to a literal: a literal argument picks the interpolating initialiser. The helper and its comment cover that. I did not run it on an iOS device or simulator.

Other places with the same trap: none in Docs.

  • I listed every interpolated string literal in native/apple/Sources (51 lines) and read each one. All are URLs, cache paths, HTML, mark names, accessibility identifiers, diagnostics, or Text(verbatim:). None reaches Text, Label, Button, LocalizedStringKey, String(localized:), String.LocalizationValue or NSLocalizedString.
  • Every dynamic key is built by concatenation or passed as a String variable (DocsEditorCommands.swift:40, DocsColophon.swift:88/92, EditorToolbar.swift:38/45), which is safe. DocsColophon.swift:85 already carries a comment about this trap.
  • native/ios and the tests have no hits either.
  • rastrillo/native at the pinned a18c32b has three interpolated Text(…, bundle:) calls in RastrilloMacUpdates (NativeUpdateCommands.swift:29, NativeUpdateNotice.swift:29-30). These are deliberate formats: the en table has the matching %@ keys, so they render properly.

Test: meaningful for what it claims, and only that.

  • I ran it in a throwaway worktree: it passes. With ja's heading2 line deleted it fails with "ja: docs.editor.toolbar.block.heading2 has no translation". It walks all 12 lproj tables.
  • It does not guard the trap itself, as the discussion already says. Reverting line 18 to the interpolated literal would leave it green.

Non-blocking suggestions

  1. A grep gate in native/scripts/check-native.sh would guard the trap directly, failing on LocalizedStringKey("…\( and on Text|Label|Button("…\(…", bundle: in native/apple/Sources. Today it would have zero hits.
  2. The value.contains("%") assertion adds little, since a heading label would never be a format; harmless.
  3. Separate from this branch, in rastrillo: the three RastrilloMacUpdates format strings exist only in en. The other 11 languages show the English sentence ("Docs 0.1.9 is ready."), not a raw key. Worth a rastrillo task.

Landed as main (squash) after Fable's APPROVE and green hub CI at 719cf8d. Added Fable's suggestion 1: check-native.sh now refuses interpolated localisation keys (verified it fails with the old line, passes now). Suggestion 3 (RastrilloMacUpdates' three format strings exist only in en) is a rastrillo follow-up. Ships in the next Docs native release.