2

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?

2

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.

  • 2
    Please add an explanation to your answer: why could that solve the problem? – fuxia Feb 25 '14 at 9:04
  • Possible?. I just though of something now to do with _ex like $link = '<a href="'. esc_url( get_permalink() ) . '">' . _ex( 'here', 'click here to go and watch the video', 'pietergoosen' ) . '</a>'; and then returning $link back into my printf() function like this <?php printf(__('Click %s to go and wacth the video.', 'pietergoosen'), $link ); ?>. Still untested as I'm currently at work and sending from my phone. Any ideas on this Mark – Pieter Goosen Feb 25 '14 at 9:07
  • @toscho, added some verbage but not sure it adds much value.... – Mark Kaplun Feb 25 '14 at 13:25
  • 1
    @PieterGoosen, yeh possible because as kaiser shows in the other answer you can do it differently. I think his answer is heedlessly bloated but it is more of a matter of personal style then correctness. – Mark Kaplun Feb 25 '14 at 13:32
  • Just read Marks comment above, thinking "My answer heedlessly bloated"? Wtf? And who upvoted that comment? Oh... it was me. :D – kaiser Mar 19 '14 at 20:43
3

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>');
  • Thanks for your suggestion levi. It is always good to have couple of ways to do one thing. In this case I realy think that there is no wrong way or right way. – Pieter Goosen Mar 6 '14 at 19:39
3

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!

  • Thanks for suggestion Emzo. This also a valid way to look at the problem. – Pieter Goosen Mar 12 '14 at 10:20
2

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

  • I don't think this is the right use for _X. AFAIK you should use it to be able to translate based on their context and let the gettext select the right version (ie, it is not just for adding comments for translators), therefor it is kind of pointless to use it for a string that appears only once in your text domain. – Mark Kaplun Feb 25 '14 at 13:40
  • Yeah, it's not the corrrect and surely not the intended use of the function - I agree with that. But it would work in that scenario. Anyway, you could simply replace it with the __() function and leave the context off. Would work exactly the same. – kaiser Feb 25 '14 at 13:41
  • 1
    Thanks to both kaizer and Mark. No one's answer is wrong or less worth that the other. It is always good to know that there are more than one way to get things done. – Pieter Goosen Feb 25 '14 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.