Support » Developing with WordPress » Alternative format for tags in sprintf during internationalization

  • I am working on a plugin for which I am developing internationalization. In a translated string, I have an HTML tag.

    When using sprintf, I have seen the following format being used:

    sprintf(__('Go to <a href="%s" target="_blank">this page</a> for more information.', 'text-domain'), 'http://wordpress.org');

    This practice of using sprintf is used to have the link outside the translation string as to not give the translator the ability to control the link.

    I would like to know if the following format is valid, if it is a better alternative to the above and maybe if it should be standard:

    sprintf(__('Go to <a%s>this page</a> for more information.', 'text-domain'), ' href="http://stackexchange.com" target="_blank"');

    It seems better for a few reasons:

    • Clearer text for the translator. He only has the minimum text required.
    • All tag attributes are outside of the translated text.
    • Translator can rightfully control the location of the tag but not change the existing attributes.

    One negative thing is that it breaks away from standard and maybe it can be confusing to some translators.

    Or maybe in some cases it makes the text less readable?

    Maybe something like this: <span%s>%n</span>

    would not be as clear as this: <span class="%s">%n</span>

    What do you think, is the second approach valid or is it better to stick with attribute names inside the string?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    My personal preference is to first minimize any markup at all, which is also part of the guidelines. Beyond that, I provide attributes as part of the translate string, but all attribute values are handled with placeholders unless the value is intended to be translated. Thus my translate string would be "Go to <a href=\"%1$s\" target=\"%2$s\" >this page</a> for more information." I also always use double quoted strings, so internal double quotes need to be escaped. It’s just a better fit for my workflow. I’m unaware of any requirement or guideline to do so.

    Leaving the _blank as part of the translatable string could be construed as something to translate. Obviously this would be incorrect for target attributes, but what about aria-label values? Those may conceivably be something to translate. Translators presumably know enough to not translate tags and attributes, but values? “If you do not want attribute values translated, don’t provide them.” is one of my rules.

    Your suggestion to pass all the attributes and values as a single string has merit in minimizing markup, but it sort of creeps me out because I cannot tell if the markup is valid or not. As a translator, I shouldn’t care, but not seeing things the way that is expected could cause confusion. In some cases the attributes used may give more context to garner the best translation. Not knowing which attributes are used will still yield a valid translation, but the attributes used could lead to a better translation.

    AFAIK, using your suggested format would be completely valid and you can use it if you think it’s superior, but you haven’t convinced me to deviate from what I’ve been doing 😉

    This post represents solely my own personal opinion. It should in no way be considered to represent any kind of official determination on behalf of the WordPress organization.

Viewing 1 replies (of 1 total)
  • The topic ‘Alternative format for tags in sprintf during internationalization’ is closed to new replies.