Skip to content

Instantly share code, notes, and snippets.

@tacoverdo
Last active November 13, 2017 07:56
Show Gist options
  • Select an option

  • Save tacoverdo/2ffad5693b9532305a41 to your computer and use it in GitHub Desktop.

Select an option

Save tacoverdo/2ffad5693b9532305a41 to your computer and use it in GitHub Desktop.
What is the best way to translate a text with hyperlink?
@cfoellmann
Copy link
Copy Markdown

In some cases I would recommend something like:

sprintf( __( 'Please read the %s.', 'tacoverdo-example-domain' ), '<a href="' . esc_html__( 'https://goo.gl', 'tacoverdo-example-domain' ) . '">' . __( 'docs', 'tacoverdo-example-domain' ) . '</a>' );

you might want to use _x() to clarify the connection but I think this way the translator is safe from screwing up html elements

EDIT:
Or do I need to go with:

sprintf( __( 'Please read the %s.', 'tacoverdo-example-domain' ), '<a href="' . esc_url( __( 'https://goo.gl', 'tacoverdo-example-domain' ) ) . '">' . __( 'docs', 'tacoverdo-example-domain' ) . '</a>' );

@tacoverdo
Copy link
Copy Markdown
Author

@cfoellmann I must say I disagree with both, as Otto explained under Law the Second.

@bueltge & @johnbillion I think it's good practice to provide context when needed, but do translators really need it in this case? Do they need to know whether %s stands for <strong> or <a href target="#">? Does it influence their translation?

@bueltge
Copy link
Copy Markdown

bueltge commented Oct 14, 2014

I find context helpful. if you add a context for %s, makes easier for translators, especially if she don't know about the position, the context in the source.

@nlemoine
Copy link
Copy Markdown

I used to include HTML tag in translations until some people reported bugs that I couldn't figure out. It turns out that a translator messed up with some HTML closing tags: < /a> (space between < and /a>) which caused some errors.

So from now, I will go for a non including HTML tags method.

@cfoellmann
Copy link
Copy Markdown

@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)

@Rarst
Copy link
Copy Markdown

Rarst commented Oct 14, 2014

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. :)

@grappler
Copy link
Copy Markdown

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.

@2ndkauboy
Copy link
Copy Markdown

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.

@tacoverdo
Copy link
Copy Markdown
Author

Please continue discussion here, if necessary.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment