esc_html_x() issue
-
Is there any particular way to translate esc_html_x() strings? If I turn esc_html_x() to esc_html__() and remove the context Loco Translate works fine, otherwise there’s no way to display the translation. Any advice? TIA
-
All strings are unique by msgid AND msgctxt. If you change your source code in this way then your translation files will no longer know the string is supposed to be the same one.
Regardless of what translation software you’re using you’ll have to update/merge your translation files to use any changed strings. This may result in losing existing translations, because you’ve changed how they’re identified.
Note that Loco’s sync functions don’t do fuzzy matching. If you regularly change source text identifiers you may find using the command line msgmerge tool is better for your needs.
Sorry, maybe I haven’t explained well. This is the scenario: a theme developed by me, localization files put in the right places, right strings etc. There is a string that Loco Translate detects fine and the theme uses esc_html_x() for that string. So I enter a translation, but on the front-end still displays the original language.
So (after many attempts to understand what’s wrong) I try to edit the theme: I turn esc_html_x() into esc_html__() and I remove the ‘context’ field, of course. I do not edit Loco Translate again, so the .po file is the same. Now the string in the frontend is translated.
Hope this makes sense this time 🙂
Please post the following:
1. the full line of PHP code that works.
2. the full line of PHP code that doesn’t work.
3. the full corresponding message in the PO source file.Ok,
1. this works:
printf( esc_html__( 'Showing %1$d–%2$d of %3$d results', 'logan' ), $first, $last, $total );2. this does not work:
printf( esc_html_x( 'Showing %1$d–%2$d of %3$d results', '%1$d = first, %2$d = last, %3$d = total', 'domain' ), $first, $last, $total );3. from PO original file:
`#: ../woocommerce/loop/result-count.php:30
#, php-format
msgid “Showing %1$d–%2$d of %3$d results”
msgstr “”`As you can see is part of WooCommerce included in the theme. Thank you in advance.
Manuel 🙂
(I assume the different domains you’ve put in 1 and 2 are a typo).
3. your PO message has no msgctxt so it won’t ever work with esc_html_x. If the theme authors set it up this way then it’s their mistake.
Looking at the context string in no.2, this looks like it is intended to be an author’s note anyway. It’s the wrong use of context, so I’m guessing the PHP code was modified after the strings were extracted.
I can’t verify any fault on the part of Loco here. Extracting strings from both your examples works fine in my tests.
I think the original PHP should be:
// translators: %1$d = first, %2$d = last, %3$d = total printf( esc_html__( 'Showing %1$d–%2$d of %3$d results', 'logan' ), $first, $last, $total );and the extracted string should be:
#. translators: %1$d = first, %2$d = last, %3$d = total #: ../woocommerce/loop/result-count.php:30 #, php-format msgid "Showing %1$d–%2$d of %3$d results" msgstr ""(Yes, of course)
Thanks, really appreciate, I’ll do more tests. I created the PO file recently with PoEdit, and the string is definitely older than the PO file, however maybe I misconfigured something in PoEdit settings.
Thank you. Manuel 🙂
Yes, Poedit will need to know the arguments of the esc_html_x function.
So the
X-Poedit-KeywordsListheader rules must includeesc_html_x:1,2cto indicate to thexgettextprogram that the second argument is the context.Loco has these rules built in when it extracts POT files from source code.
I’m going to mark as resolved for now, as the issue seems unrelated to this plugin.
The topic ‘esc_html_x() issue’ is closed to new replies.