-
-
Save tacoverdo/2ffad5693b9532305a41 to your computer and use it in GitHub Desktop.
<?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' ); |
I summarized my thoughts and my interpretation of this discussion in answer at WPSE, but since Ze grumbled at me for it — alternate answers welcomed. :)
If you can go with method 2 then that is the best but not always possible. Method 3 is simple and balanced.
Method 1 and 4 are the same except for using %s
or %1$s
@cfoellmann solution should work in every language as you can move the variable as needed but requires a certain understanding to make it work.
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.
Please continue discussion here, if necessary.
Thanks!
@tacoverdo You are totally right. The "chapter" is great and logical but I think the case here with a link it a little bit beyond the described case. This discussion is a good indicator for that.
Hopefully there are some more ideas coming up here. I am not happy with either of the solutions (including mine)