How to translate a string with tags in between.

Suppose there is a string like

$a = 5;
There are <?php echo $a; ?> <span>people</span> in this country.

Javascript files:

wp_localize_script( 'store-locator', 'storelocatorjstext', array(
    'nostores'   => __( 'There are 5 people in this country.', 'textdomain' )
) );

What is the correct way of internationalizing such cases?

My approach:

<?php _e('There are 5 <span>people</span> in this country.', 'textdomain'); ?>

1 Answer 1


You can wrap the translation in a printf function. printf ( or sprintf for when you don't want to print to the screen immediately ) allow you to put placeholders in the string. See the documentation for all the type specifiers

Use __() instead of _e() because printf already outputs to the screen.

$no_people = 5;

printf( __( 'There are %d <span>people</span> in this country.', 'textdomain' ), $no_people );


Added example for wp_localize_script

$no_people = 5;

$nostores = sprintf( __( 'There are %d <span>people</span> in this country.', 'textdomain' ), $no_people );

wp_localize_script( 'store-locator', 'storelocatorjstext', array(
    'nostores'   => $nostores
) );
  • Thanks. In PHP you can mention dynamic variable with %d. But how to do so in Javascript? Please check the updated question for JS code
    – Kiran Dash
    Commented Feb 7, 2017 at 10:59
  • Do the exact same thing but use sprintf. You can then save the entire translated string in a variabe and send that to the javascript localization. See updated answer
    – Jebble
    Commented Feb 7, 2017 at 11:01
  • Yes But I meant a dynamic JS variable. Since this was off topic for my current question. I created a new question. wordpress.stackexchange.com/questions/255514/…
    – Kiran Dash
    Commented Feb 7, 2017 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.