How to localise this string (including html) ?

     echo "<div class='updated'><p>All options are restored successfully.</p></div>" ;

Is this correct?

     echo '<div class="updated"><p>' . __( 'All options are restored successfully.', 'mytextdomain' ) . '</p></div>';

And how to go about this one?

     <p><?php _e( 'On the <a href="' . get_admin_url() . 'import.php">Import</a> page, choose the previously created file and click the <strong>Upload file and import</strong> button.', 'mytextdomain' ); ?></p>
  • If you want to return the string, use __(), if you want to echo, use _e(). So, _e() is exactly the same as echo __(). – Max Yudin Feb 12 '13 at 8:46
  • Had a mistake there, edited, so it's correct now? – urok93 Feb 12 '13 at 8:50
  • 1
    Yes, it is correct. A simple test with a real translation could have told you that. Somehow I don’t see you actual problem? – fuxia Feb 12 '13 at 8:55
  • I know it works, I just wanted to confirm that it's the proper way to do it, and if there is a more elegant solution. – urok93 Feb 12 '13 at 9:13
  • @drtanz You asked 111(!!) questions so far. You should really know how to do an update of a question and that posting updates as comments in inappropriate. – kaiser Feb 12 '13 at 11:29

Your example is correct, simply because it works. There are hundreds of ways to localize strings wich are displayed including html, all good and elegant in their own way.

I like to do this as follows:

printf( '<div class="updated">
    __( 'All options are restored successfully.', 'mytextdomain' )

You also could do this like:

$str = __( 'All options are restored successfully.', 'mytextdomain' );
echo "<div class='updated'><p>$str</p></div>";

The most important thing is to maintain readability I guess. There isn't a "correct" way to display localized strings in combination with html.

For your second one, I would use:

_e( sprintf( 'On the <a href="%1$s">Import</a> page, choose the previously created file and click the <strong>Upload file and import</strong> button.', get_admin_url() . 'import.php' ), 'mytextdomain' );

In this case, the URL doesn't have to be translated.

  • Thanks that makes sense, how would you go about the second example I put in the updated question? – urok93 Feb 12 '13 at 13:49
  • I updated my answer with my opinion on your second example. :-) – Mike Madern Feb 12 '13 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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