Last active
November 13, 2017 07:56
-
-
Save tacoverdo/2ffad5693b9532305a41 to your computer and use it in GitHub Desktop.
What is the best way to translate a text with hyperlink?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// METHOD 1 | |
sprintf( __( 'Please read %1$sthis%2$s.', 'tacoverdo-example-domain' ), '<a target="_blank" href="' . esc_url( 'https://goo.gl' ) . '">', '</a>' ); | |
// METHOD 2 | |
echo '<a target="_blank" href="' . esc_url( 'https://goo.gl' ) . '">'; | |
__( 'Please read this.', 'tacoverdo-example-domain' ); | |
echo '</a>'; | |
// METHOD 3 | |
sprintf( __( 'Please read <a href="%s">this</a>.', 'tacoverdo-example-domain' ), esc_url( 'https://goo.gl' ) ); | |
// METHOD 4 | |
sprintf( __( 'Please read %sthis%s.', 'tacoverdo-example-domain' ), '<a target="_blank" href="' . esc_url( 'https://goo.gl' ) . '">', '</a>' ); | |
// METHOD 5 | |
__( 'Please read <a target="_blank" href="' . esc_url( 'https://goo.gl' ) . '">this</a>', 'tacoverdo-example-domain' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would also opt for method 3. It's not uncommon for a translator to have strings with some HTML markup inside. As long as it's just as much as you need.
I would also find 1 and 4 not easy to translate if you don't know what the placeholders stand for. And beside readability, it's quite easy to mess it up. Method 2 would be somehow OK. I would never use method 5, as it's quite impossible to parse such a translation. If you hard code the URL in method 5, to make the URL itself translatable (as @Rarst pointed out), than I am also OK with method 5, but still wouldn't do it this way.