Skip to content

Ship translations with the plugin, starting with German - #111

Merged
dnnsjsk merged 4 commits into
inline0:7.7.2from
Contributolo:feat/bundled-translations
Oct 15, 2026
Merged

dnnsjsk merged 4 commits into
inline0:7.7.2from
Contributolo:feat/bundled-translations

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #110: the first commit here is that PR, so please merge it first.

Since #100 the editor strings are translatable, but no translation reaches anyone: Blockstudio is not on WordPress.org, so WordPress never downloads a language pack for the blockstudio text domain. This PR ships translations with the plugin, adds German, and gives everyone a simple way to add or fix a language.

Runtime

  • New Translations class. Translations::load() runs on init and change_locale and loads wp-content/languages/plugins/blockstudio-{locale}.mo first, then the bundled file. For a string in both, WordPress uses the file loaded first, so a site can still override single strings. load_textdomain() also picks up the .l10n.php file (WordPress 6.5+).
  • blockstudio-blocks, blockstudio-admin-page, blockstudio-canvas and blockstudio-devtools now pass a path to wp_set_script_translations(). With a path, load_script_textdomain() first looks for {domain}-{locale}-{handle}.json in it, which does not depend on where Blockstudio is installed. A matching file in wp-content/languages/plugins takes precedence here as well (Translations::script_path()).
  • All paths come from BLOCKSTUDIO_DIR, so plugin, must-use plugin and Composer installs work the same.

Tooling

npm run i18n runs scripts/i18n.ts (no new dependencies). It

  • extracts strings from PHP (the WordPress gettext functions with the blockstudio domain, translator comments, php-format) and from the TypeScript sources of each script bundle (@/utils/__ and @wordpress/i18n), following the imports from the webpack entries,
  • writes languages/blockstudio.pot and updates every .po file,
  • compiles each .po into .mo, .l10n.php and one .json per script handle.

wp i18n make-pot does not fit here: it parses only .js and .jsx, and the @/utils/__ wrapper hides the text domain. The generated .mo files are byte-identical to msgfmt --no-hash. The .l10n.php and .json files have the structure of WP_Translation_File_PHP::export() and WP-CLI make-json.

npm run i18n:check is added to npm run check. It fails when a string is assembled at runtime, when a translation drops a placeholder, or when languages/ is out of date. A .po file reformatted by an editor such as Poedit still passes, it only has to contain the same strings. The POT references have no line numbers, so unrelated code changes do not make it stale. If you prefer to keep it out of check, removing it there is enough.

German

de_DE and de_DE_formal, all 127 strings. The formal file differs where a text addresses the reader. I am happy to maintain the German translation.

Docs

New page general/translations (using, overriding and contributing translations), docs count raised to 73, LLM index rebuilt with npm run build:llm, one row in the CLAUDE.md command table. The new class says @since 7.8.0, please adjust to the release it lands in.

Tests

  • tests/unit/i18n/BundledTranslationsTest.php: PHP strings in de_DE and de_DE_formal, English for an unknown locale, the editor bundle printing the bundled JSON, compiled files present for every .po.
  • I could not run the wp-env suites locally (no Docker), so the new PHPUnit test has not run yet. Checked instead: npm run check, composer cs, PHPStan, and a WordPress Playground site (WordPress latest, PHP 8.4, site language German) with this branch mounted as the plugin and the bundles rebuilt locally. The editor shows "Zeile hinzufügen", "Mediathek öffnen" and "Link auswählen", the Tools page loads its German JSON, PHP messages are German, de_DE_formal loads its own file, an unknown locale stays English, and files in wp-content/languages/plugins win for PHP and JS.

The last commit also makes the canvas menu (Pages, Blocks, Fit to view, Zoom to 100%, Live mode, Canvas options, Close focus mode) and the copy feedback of the devtools overlay translatable. A search over src/ (JSX text, label and title attributes, notices) finds no other visible English text outside __(). The pages bundle has no visible text.

The default texts of textMediaButton, textButton (link and repeater),
textRemove, the reset button of select fields, the alignment buttons and
the code toggle of the WYSIWYG field were assembled at runtime, for
example __( value || 'Add row' ) or __( `Align ${align}` ). No
translation tool can extract such strings, so they stayed in English in
every language. Each variant is now a literal string. Custom texts from
block.json still go through __() in the default text domain as before.

The reset button of select fields had no label: allowReset is a boolean,
so __( allowReset || 'Reset to default' ) returned true, which React does
not render.
Blockstudio is not on WordPress.org, so WordPress never downloads a
language pack for it, and the strings made translatable in 7.7.0 stayed
in English. Translations can now ship in the languages folder.

- Translations::load() loads the PHP translations on init and after a
  locale switch, from wp-content/languages/plugins first, then from the
  bundled folder. This works for plugin, must-use plugin and Composer
  installs, because the path comes from BLOCKSTUDIO_DIR.
- Both script bundles pass a translation path to
  wp_set_script_translations(). WordPress loads the handle-named JSON
  file from there, which does not depend on the install location.
- npm run i18n extracts the strings from PHP and from the TypeScript
  sources, including the @/utils/__ wrapper, updates
  languages/blockstudio.pot and every .po file, and writes the .mo,
  .l10n.php and per-handle .json files.
- npm run i18n:check is part of npm run check. It fails when a string is
  assembled at runtime or the translation files are out of date.
- A new docs page explains how to use, override and contribute
  translations.
de_DE and de_DE_formal, so sites in the formal locale get the same
translation with "Sie" instead of "du" where the text addresses the
reader.
The canvas menu (Pages, Blocks, Fit to view, Zoom to 100%, Live mode,
Canvas options, Close focus mode) and the copy feedback of the devtools
overlay were plain English text. They now go through __(), both scripts
load their translations like the editor bundles, and the German files
cover them. The placeholder check of npm run i18n now applies to every
string with a placeholder, not only to PHP strings.
@dnnsjsk
dnnsjsk changed the base branch from main to 7.7.2 October 15, 2026 11:07
@dnnsjsk
dnnsjsk merged commit 6902dfd into inline0:7.7.2 Oct 15, 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