I have translated many string successfully previously, but this one gives me a headache. This part is used inside my content-related.php file which I use to display related posts
Here is the current string I’m using
<?php echo 'Click <a href="'. esc_url( get_permalink() ) . '">here</a> to go and wacth the video.' ?>
My problem in the localization process is the
<a href="'. esc_url( get_permalink() ) . '">here</a>
part in the middle.
The best I could come up with is
<?php printf(__('Click %s to go and wacth the video.', 'pietergoosen'), '<a href="'. esc_url( get_permalink() ) . '">here</a>'); ?>
OK, I know I can just create a variable for the <a>
tag and rather use the variable in the printf()
string instead of the whole <a>
tag, but that is not the concern now, and that was only done to test the result first.
With the string in its changed order, the string translates, except for the word “here” which is inside the <a>
tag. How can I get the string to translate the word “here” as well. I’ve read somewhere, think it was in the Codex, that it is not a good idea breaking up a string into several strings as it may cause problems for certain languages as word order differ in some languages.
Any more suggestions to solve this problem?
Possible solution to get everything translatable as one sting.
It is usually safe to assume that the translator have enough knowledge of HTML to understand what is going on there.
Another option is to split the tag into two strings:
Although this doesn’t exactly answer the question of translating strings containing HTML, but take a step back for a moment – you may be making things more difficult then they need to be. Why don’t you reword the text to simply say “Watch the video”. It becomes a non issue then, because you can simply wrap the whole string in an anchor, leaving the string free of any HTML like so:
If the link is styled appropriately (meaning it is obviously clickable) I don’t think you need to tell people to “Click here”.
Just a thought!
When you look at the i18n Documentation in Codex and especially at the Disambiguation by Context section, then you’ll find
_x()
and_ex()
(the later is theecho
variant), which allows you to add context to a string or in other words: leave a message for translators:So first the string gets translated. After that, the
%s
parts inside the string will get replaced. This will help you to overcome scenarios where a language needs a completely different amount of words or a different position in the sentence to be wrapped with an anchor tag. This will also help you with avoiding the need to split up your sentence.