Title: esc_html_x() issue
Last modified: September 1, 2016

---

# esc_html_x() issue

 *  Resolved [manuelmasia](https://wordpress.org/support/users/manuelmasia/)
 * (@manuelmasia)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/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
 * [https://wordpress.org/plugins/loco-translate/](https://wordpress.org/plugins/loco-translate/)

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Author [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * (@timwhitlock)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460349)
 * 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.
 *  Thread Starter [manuelmasia](https://wordpress.org/support/users/manuelmasia/)
 * (@manuelmasia)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460433)
 * 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 🙂
 *  Plugin Author [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * (@timwhitlock)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460438)
 * 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.
 *  Thread Starter [manuelmasia](https://wordpress.org/support/users/manuelmasia/)
 * (@manuelmasia)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460444)
 * Ok,
 * 1. this works:
    `printf( esc_html__( 'Showing %1$d&ndash;%2$d of %3$d results','
   logan' ), $first, $last, $total );`
 * 2. this does not work:
    `printf( esc_html_x( 'Showing %1$d&ndash;%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 🙂
 *  Plugin Author [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * (@timwhitlock)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460448)
 * (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.
 *  Plugin Author [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * (@timwhitlock)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460449)
 * 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 ""
       ```
   
 *  Thread Starter [manuelmasia](https://wordpress.org/support/users/manuelmasia/)
 * (@manuelmasia)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460450)
 * (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 🙂
 *  Plugin Author [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * (@timwhitlock)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460455)
 * Yes, Poedit will need to know the arguments of the esc_html_x function.
 * So the `X-Poedit-KeywordsList` header rules must include `esc_html_x:1,2c` to
   indicate to the `xgettext` program 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.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘esc_html_x() issue’ is closed to new replies.

 * ![](https://ps.w.org/loco-translate/assets/icon-256x256.png?rev=1000676)
 * [Loco Translate](https://wordpress.org/plugins/loco-translate/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/loco-translate/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/loco-translate/)
 * [Active Topics](https://wordpress.org/support/plugin/loco-translate/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/loco-translate/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/loco-translate/reviews/)

## Tags

 * [context](https://wordpress.org/support/topic-tag/context/)
 * [Localization](https://wordpress.org/support/topic-tag/localization/)

 * 8 replies
 * 2 participants
 * Last reply from: [Tim W](https://wordpress.org/support/users/timwhitlock/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/esc_html_x-issue/#post-7460455)
 * Status: resolved