Quick reference

/* TRANSLATE:

//WITHIN BLOCK:
${!${''}=(__('

','mytd'))}

${!${''}=(__('','mytd'))}


//WITHIN CODE
__('', 'mytd');

//  ${!${''}=(__('','mytd'))}
//  ${!${''}=(sprintf(__(' %s ','mytd'), $MyVariable))}
*/

Best practice tips

  • Use entire sentences – in most languages word order is different than that in English.
  • Split at paragraphs – merge related sentences, but do not include a whole page of text in one string.
  • Assume strings can double in length when translated

text-domain

You have to specify a text-domain with every function call. If you don’t the parent theme file will be used and if your string isn’t in it it won’t be translated.

For the functions below we’ve used ‘mytd’ instead of ‘text-domain’ as a simple unique string we can just use or use a find and replace on later if we want.

The text domain name must use dashes and not underscores and be lowercase.

WordPress functions

WordPress uses PHP gettext(), but you should use these functions instead.

WordPress resources:

https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/

Return translation
__('', 'mytd');

$MyString = __('My string to be internationalized', 'mytd');

//To prevent malicious translation code being passed:
esc_html__('', 'mytd')

'text-domain' should be your plugin slug or theme slug.
Using within a <<< Heredoc string
${!${''}=(__('', 'mytd'))}
Echo
//Echo translation (the _e is equivalent of echo)

_e('', 'mytd');

_e('My string to be internationalized', 'mytd');

//To prevent malicious translation code being passed:
esc_html_e('', 'mytd');
Variables

Do not use double quotes for the strings!!!!

//1 VARIABLE
$MyString = sprintf(
  //translators: %s will be replaced by ###, ensure it remains in the translation
  __( 'Your city is %s.', 'mytd' ), $city);

//MULTIPLE VARIABLES
//If you want the option of moving variables to different places within the translations depending on translation you can use this:
$MyString = sprintf(
  //translators: %1$s will be replaced by ###,  %2$s will be replaced by ###, ensure they remain in the translation
  __('Your city is %1$s, and your zip code is %2$s.', 'mytd' ), $city, $zipcode);
Using within a <<< Heredoc string
${!${''}=(sprintf(__(' %s ','mytd'), $MyVariable))}
Plurals
$MyString = _n(
        '%s comment',
        '%s comments',
        get_comments_number(),
        'mytd'
    ),
    number_format_i18n( get_comments_number() )
);

New Line

Use /n

Comments to translators

Use this format as the last comment before a “__()” etc call

/* translators: My comment to give them */
_e('My string to be internationalized', 'mytd');

// translators: My comment to give them
_e('My string to be internationalized', 'mytd');
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *