WPML - markhowellsmead/helpers GitHub Wiki
Translating in FSE
Making the Theme translatable
WPML continues to rely on the gettext
functions in order to manage translatability. Because the theme templates and template parts are HTML files in full-site-editing, it is not possible to use the gettext
functions directly in these files. WordPress automatically wraps and encodes PHP tags in these templates, so that they are not executed as PHP.
If you need to provide parts of a template containing translatable strings, then you will need to do so by creating Block Patterns. You can see examples of how this is achieved in the source code of the Twenty Twenty Two theme.
(An example of how to integrate patterns to a Hello Roots theme is explained under Block Patterns.)
The block pattern can then be inserted into the FSE layout as a block, using the following syntax.
<!-- wp:pattern {"slug":"twentytwentytwo/footer-navigation"} /-->
Editing translations
Translating strings from the Theme files
- Go to WPML > Theme and plugins localization and scan the theme for strings.
- Go to WPML > String translation and search for the strings, then translate them.
Translating modified templates/template parts
If the templates or template parts have been editorially modified in the FSE editor, then they will appear under WPML > Translations. (Templates and template parts which have not been modified from the theme will not appear in this list.
Click on Translate or Edit in the right-hand column. This will allow you to translate the strings using the WPML Advanced Translation Editor.
Pending translations also appear under WPML > Translation Management. The links to edit templates and template parts are currently broken as of 23.3.2022. (WPML Ticket.)
Translating Advanced Custom Fields labels
Register the strings for WPML String Translation
add_action('init', [$this, 'registerACFStrings']);
…
/**
* Register every ACF form field label, message and instructions
* so that they can be translated in WPML String Translation.
*
* @return void
*/
public function registerACFStrings()
{
if (!is_admin()) {
foreach (acf_get_field_groups() as $group) {
$fields = acf_get_fields($group['ID']);
if (is_array($fields) && count($fields)) {
foreach ($fields as $field) {
do_action('wpml_register_single_string', 'ACF Labels', $field['label'], $field['label']);
do_action('wpml_register_single_string', 'ACF Instructions', $field['instructions'], $field['instructions']);
if ($field['type'] === 'group' && !empty($field['sub_fields'] ?? [])) {
foreach ($field['sub_fields'] as $sub_field) {
do_action('wpml_register_single_string', 'ACF Labels', $sub_field['label'], $sub_field['label']);
do_action('wpml_register_single_string', 'ACF Instructions', $sub_field['instructions'], $sub_field['instructions']);
}
}
}
}
}
}
}
Hook into the field and output the translations
add_filter('acf/load_field', [$this, 'translateAcfFieldLabel']);
…
/**
* Hook into ACF field generation and load the translated label,
* message and instructions from WPML.
*
* @param array $field
* @return array
*/
public function translateAcfFieldLabel(array $field)
{
$field['label'] = apply_filters('wpml_translate_single_string', $field['label'], 'ACF Labels', $field['label']);
$field['instructions'] = apply_filters('wpml_translate_single_string', $field['instructions'], 'ACF Instructions', $field['instructions']);
return $field;
}
Stopping URLs from being rewritten
WPML overrides URLs in the_content
so that if you link across languages on the same website, the content in the current language is linked. The wpml_sl_blacklist_requests
filter stops that from happening.
add_filter('wpml_sl_blacklist_requests', [$this, 'blacklistRequests']);
…
/**
* Add URL exceptions which won't be replaced/stripped
* in the_content by WPML
*
* @param array $blacklist
* @return array
*/
public function blacklistRequests($blacklist)
{
$blacklist[] = '/articles-specialises/';
$blacklist[] = '/fachartikel/';
return $blacklist;
}