This thread is resolved. Here is a description of the problem and solution.
Problem:
The client is experiencing an issue where newly added strings in Twig templates are not being detected by WPML String Translation. This problem only affects Twig templates, as strings added in PHP files are still detected and translated correctly.
Solution:
We recommend handling string registration and translation retrieval in PHP, keeping Twig as the presentation layer. Here is a step-by-step guide:
1. Register strings using the
wpml_register_single_string
action and retrieve translations using the
wpml_translate_single_string
filter in PHP. Example:
function theme_t(string $name, string $default, string $context = 'Theme strings'): string {<br /> do_action('wpml_register_single_string', $context, $name, $default);<br /> return apply_filters('wpml_translate_single_string', $default, $context, $name);<br />}<br />$context['i18n'] = [<br /> 'read_more' => theme_t('button.read_more', 'Read more'),<br /> 'search_placeholder' => theme_t('search.placeholder', 'Search'),<br />];<br />Timber::render('template.twig', $context);2. Consider maintaining a centralized PHP file for translations or separate files per component/block for larger projects.
3. For block-based architectures, manage translations at the block level. Example:
function get_card_block_i18n(): array {<br /> return [<br /> 'title' => theme_t('blocks.card.title', 'Featured article', 'Theme blocks'),<br /> 'cta' => theme_t('blocks.card.cta', 'Read more', 'Theme blocks'),<br /> ];<br />}Documentation on string registration and translation can be found at:
wpml_register_single_string
wpml_translate_single_string
If this solution does not resolve your issue or seems outdated, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If needed, please open a new support ticket at WPML support forum.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
This topic contains 4 replies, has 0 voices.
Last updated by 1 month, 1 week ago.
Assisted by: Andrey.