Skip to content

feat: translate WPML link label suffixes and add a label filter - #142

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/wpml-config-link-labels
Oct 22, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/wpml-config-link-labels

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

wp bs wpml config labels the keys it declares with the editor label of the field (preceded by the labels of the enclosing repeaters and groups). WPML shows these labels as field names in its translation editor. Two things make them hard to work with:

  • A link field gets title with the plain field label and url with <label>: URL, the suffix hard-coded in English. A translator sees "Button" and "Button: URL" in every site language, and "Button" does not say that it is the link text.
  • Editor labels often carry hints meant for the block editor, such as "Image (optional)". They end up in the translation editor as they are, and there is no way to adjust them short of editing the generated section by hand, which the next run overwrites.

Change

Translatable link suffixes. Both link keys now get a suffix through the blockstudio text domain: %s: Link text for title and %s: URL for url. "Link text" follows what the key holds (the title the LinkControl text input writes) and keeps the two keys of one link recognizable as a pair. The format strings let a locale reorder or change the separator. German: %s: Linktext and %s: URL (URL is the usual term in the German WordPress translation), same for de_DE_formal.

The command writes the suffixes in the site language: WP-CLI loads WordPress with the site locale, and Translations::load() runs on init with determine_locale().

Label filter. blockstudio/wpml/config/label receives each label before it is written:

add_filter('blockstudio/wpml/config/label', function ($label, $context) {
  return str_replace(' (optional)', '', $label);
}, 10, 2);

$context holds block (block name), path (storage path below blockstudio.attributes, * for repeater rows, e.g. cards/*/link), field (the field definition), parents (labels of the enclosing repeaters and groups with an ID) and part (text, or title / url for a link). The name follows the command (wp bs wpml config) under the blockstudio/ prefix; there was no WPML filter yet. One filter with a context array keeps the signature stable if more parts are added later.

The filtered value is escaped like every other label. An empty string leaves the label attribute out, so WPML falls back to the label of the block (per WPML's "Make Custom Gutenberg Blocks Translatable"). A return value that is not a string keeps the original label.

The English title label of link keys changes from "Button" to "Button: Link text". WPML uses label for display only, so existing translations are not affected; regenerating the file is enough.

Docs

  • dev/wpml: suffixes, locale note, new section "Change the labels" with the context keys; example output updated.
  • blocks/hooks/php: new "WPML" section with the filter.
  • Hook added to HookCallbackRule::KNOWN_HOOKS and the runtime-families fixture; LLM context rebuilt.

Tests

tests/unit/wpml/WpmlConfigTest.php:

  • test_link_suffixes_are_translated: with de_DE, a link in a repeater gets Karten: Button: Linktext and Karten: Button: URL.
  • test_label_filter_receives_the_context_of_each_key: block, path, part, parents and field for a text field and both parts of a link in a repeater.
  • test_label_filter_changes_escapes_and_leaves_out_labels: a changed label is escaped, an empty string drops the attribute, a non-string keeps the original, the document still validates.
  • Existing expectations updated for the new title suffix (also in WpmlConfigRenderPathsTest).

Run in WordPress Playground (PHP 8.4, WordPress latest, plugin and test theme mounted as in tests/wp-env): wpml 37 tests green (34 on main), i18n and cli green on both. With the site language set to de_DE, wp bs wpml config --file=... run through WP-CLI writes for the link fields of the test theme:

<key name="title" label="Group: Link: Linktext" />
<key name="url" type="link" label="Group: Link: URL" />

A must-use plugin with a callback on the filter that appends the part for blockstudio/type-link gave, for that block only:

<key name="title" label="link: Linktext [filtered title]" />
<key name="url" type="link" label="link: URL [filtered url]" />
``` `composer cs`, PHPStan on the changed file, `npm run check` (including `i18n:check` and `docs:check`) and the `packages/phpstan` hook tests clean; the other `packages/phpstan` failures in my environment are the same on `main`.

`@since 7.7.6` on the filter is a placeholder, please adjust.

wp bs wpml config labels the two keys of a link field with the field label
and the label followed by a hard-coded ": URL". The translation editor shows
"Button" and "Button: URL" in every site language, and the title key does not
say that it holds the link text.

Both keys now get a suffix through the blockstudio text domain: "%s: Link
text" and "%s: URL". WP-CLI loads the site locale and Blockstudio loads its
translations on init, so the command writes the suffixes in the site
language. German translations are included.

Field labels are written for the block editor and often carry hints such as
"(optional)". The new blockstudio/wpml/config/label filter receives each
label with the block name, the storage path, the field definition, the
parent labels and the part (text, title, url). An empty string leaves the
label attribute out, so WPML falls back to the block label; a value that is
not a string keeps the original.

Adds docs on the WPML page and the PHP hooks page, the hook in the PHPStan
hook inventory, and unit tests for the German suffixes, the filter context,
escaping and the empty label.
@dnnsjsk
dnnsjsk merged commit 7b0a3c6 into inline0:main Oct 22, 2026
@dnnsjsk

dnnsjsk commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.6, now officially released. This PR is merged into main as part of the reviewed release, with full CI and distribution checks passing for the release commit.

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