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.
<?php printf(__('Click <a href="%s">Here</a> to go and wacth the video.', 'pietergoosen'), esc_url( get_permalink() )); ?>
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:
printf(__('Click %shere%s to go and wacth the video.', 'pietergoosen'), '<a href="'. esc_url( get_permalink() ) . '">', '</a>');
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:
printf( '<a href="%s">%s</a>', esc_url( get_permalink() ), __( 'Watch the video', 'pietergoosen' ) );
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
_ex() (the later is the
echo variant), which allows you to add context to a string or in other words: leave a message for translators:
printf( _x( 'Click %shere%s to go and watch the video', '%s represents the opening and closing HTML tags for an anchor or link', 'your_textdomain' ), '<a href="'.esc_url( get_permalink() ).'" title="'.get_the_title().'">', '</a>' );
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.