Home›Support›English Support›[Resolved] Strings with HTML entities from Twig template are not showing on website
[Resolved] Strings with HTML entities from Twig template are not showing on website
This thread is resolved. Here is a description of the problem and solution.
Problem: If you're experiencing issues with some strings in TWIG files not appearing translated on your website, especially those containing HTML entities like non-breaking hyphens or spaces, despite being correctly translated in the WPML database, this summary might help. Solution: First, ensure that the issue isn't caused by any custom code or other plugins by testing with the default WordPress theme and disabling all non-WPML plugins. If the problem persists, it's likely due to a known issue with WPML handling HTML entities in strings. Currently, a permanent fix is scheduled for WPML version 4.7. In the meantime, you can use the workaround of deleting the problematic string, rescanning the website, and manually adding the translation within WPML. For more details, visit related known issues.
Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking the related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the issue persists, please open a new support ticket.
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.
- I have a website with the latest version of WordPress.
- A custom theme used there which contains TWIG files.
- I am localizing the website using latest version of WPML.
My procedure to localize the TWIG content is as follows:
1. I have converted all strings in TWIG files into constants, such as:
title: __('Bezpečné transakční zprávy', 'twigcon'),
desc: __('Posílejte zabezpečené transakční e‑maily, zprávy SMS, PUSH oznámení, nebo třeba dopisy.', 'twigcon')|raw,
etc.
2. To show the strings in WPML, I go to WPML/String Translation and enable the "Look for strings while pages are rendered" option.
3. I render the pages, go back to the settings and export the discovered strings to a .po file.
4. I translate the .po file and import it back with new translations. The translations are successfully visible in WPML under the correct domain (twigcon in my case).
The problem:
When I load the website, some strings on the page appear translated and some remain in the original language. I have noticed that the problem is with strings that contain HTML entities.
When a constant in TWIG file contains a HTML entity, such as non-breaking hyphen (‑), non-breaking space, etc. WPML does not display its translation on the website, even though it exists in WPML database.
I have tried applying raw or escape_html filters to constants in TWIG files, but the results were the same. Nevertheless, if I delete the problematic string, rescan the website, and add the translation for that string manually from within WPML, it shows on the website correctly.
Can you please advise on how to make this work without any workarounds? I.e., export/import.
Can you also confirm that this is the correct procedure to localize the strings from the TWIG files?
What is the link to your site? hidden link
(currently under a password, which I would prefer to not share publicly in a forum)
my apologies, the SSL was not working properly, please try again now.
There are only three TWIG strings and they are on the following page:
hidden link
These strings (constants) are available in the theme within index.twig under "twigcon" domain and they were correctly recognized by WPML when I enabled "Look for strings while pages are rendered".
You can see the excerpt of these constants from the index.twig here: hidden link
I then exported the strings under this domain (twigcon) as .po file, added translations, and imported it back. See the exported .po file with translations here, if needed: hidden link
Each translation starts with "Sample translation:" prefix to easily recognize whether the text has been switched or not.
Now, when you switch the language on the site to Slovak, you can see that the first string switches fine (this string does not contain any html entities), but the other two strings remain in original Czech language, likely because of the html entities used in them.
since there has not been any update since December, 2023 and the issue still persists, can you please let me know if there is a plan to resolve this anytime soon? Unfortunately, manually updating individual strings is not a workaround we could employ.