Repository navigation
feat: translate WPML link label suffixes and add a label filter - #142
Merged
Merged
Conversation
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wp bs wpml configlabels 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:linkfield getstitlewith the plain field label andurlwith<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.Change
Translatable link suffixes. Both link keys now get a suffix through the
blockstudiotext domain:%s: Link textfortitleand%s: URLforurl. "Link text" follows what the key holds (thetitlethe 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: Linktextand%s: URL(URL is the usual term in the German WordPress translation), same forde_DE_formal.The command writes the suffixes in the site language: WP-CLI loads WordPress with the site locale, and
Translations::load()runs oninitwithdetermine_locale().Label filter.
blockstudio/wpml/config/labelreceives each label before it is written:$contextholdsblock(block name),path(storage path belowblockstudio.attributes,*for repeater rows, e.g.cards/*/link),field(the field definition),parents(labels of the enclosing repeaters and groups with an ID) andpart(text, ortitle/urlfor a link). The name follows the command (wp bs wpml config) under theblockstudio/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
labelattribute 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
labelfor 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.HookCallbackRule::KNOWN_HOOKSand the runtime-families fixture; LLM context rebuilt.Tests
tests/unit/wpml/WpmlConfigTest.php:test_link_suffixes_are_translated: withde_DE, a link in a repeater getsKarten: Button: LinktextandKarten: 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.WpmlConfigRenderPathsTest).Run in WordPress Playground (PHP 8.4, WordPress latest, plugin and test theme mounted as in
tests/wp-env):wpml37 tests green (34 onmain),i18nandcligreen on both. With the site language set tode_DE,wp bs wpml config --file=...run through WP-CLI writes for the link fields of the test theme:A must-use plugin with a callback on the filter that appends the part for
blockstudio/type-linkgave, for that block only: