Strings including html for localisation

How to localise this string (including html) ?

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

Is this correct?

Read More
     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>

Related posts

Leave a Reply

1 comment

  1. 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">
            <p>%1$s</p>
        </div>',
        __( '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.