Support » Developing with WordPress » esc_html__ function for translations

  • Resolved arsm


    I have a Loco translation that doesn’t apply, and I discovered that the developers have written some lines wrong. For instance I’m finding this
    esc_html__( ‘hello darling’, ‘myplugin-name-trans’ )

    which, if I compare to working translations like
    esc_html__( ‘hello world’, ‘mytheme-name’ )
    it suggests that I should use
    esc_html__( ‘hello darling’, ‘myplugin-name’ ),
    as confirmed by this function hinting at the $domain string (changing myplugin-trans into myplugin-name).

    But how come it works for the theme name and not with the plugin name?

    • This topic was modified 1 month ago by arsm.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    The text domain name tells WP in part which .mo file to get translations from. If your plugin’s .pot file doesn’t have that string defined, it won’t get translated under your text domain. Apparently the theme does have such a string defined if it’s getting translated under that text domain.

    More about text domains here:

    Thread Starter arsm


    thank you for the hint. This is a custom plugin I had made by the theme devs for my site, so I can play with it at will.
    The first thing I noticed in the myplugin.php file is that the string
    * Text Domain: my-plugin
    is missing in the first lines.
    Is it as simple as just adding that line, and then replace any instances like?
    esc_html__( ‘hello darling’, ‘my-plugin’ )

    Besides, how do I detect whether the .pot file has the domaion defined?

    Moderator bcworkz


    The .pot file takes on the text domain name: my-plugin.pot. I’m not sure how gettext really works, so I don’t know if the Text Domain: comment header is required or not. No harm in adding it just in case. Otherwise, ensuring all your translation calls use the same text domain is what is needed for proper translation.

    Because “my-plugin” is commonly used in examples, it’s not a good choice for an actual plugin. Use your preferred unique string instead.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.