Search code examples
phpinternationalizationmarkup

Markup part of localized string


I need to format a part of a localized string. Let's assume I want to make a part of it "bold" and let's consider HTML as the markup used.

In this example I need "Macy's" to be bold:

$en = "At %1$s, I bought %2$s.";
$es = "Compré %2$s en %1$s."

printf($en, "Macy's", "a cup"); // prints: At Macy's, I bought a cup.
printf($es, "Macy's", "una tasa"); // prints: Compré una tasa en Macy's.

Using markup language in string will confuse transators as they don't know what that is OR can be said it breaks the SoC:

$en = "At <b>%1$s</b>, I bought %2$s.";
$es = "Compré %2$s en <b>%1$s</b>."

Splitting the string around the markup-ed part will create small string out of context that will be harder to translate.

$en1 = 'At';
$en2 = ', I bought %2$s.';

print($en1."<b>"."Macy's"."</b>")
printf($en2, "a cup");
// prints: At </b>Macy's</b>, I bought a cup.

Q: What is the best way to achieve this ?

Thank you.


Solution

  • Markup in the string.

    Sure, the translators might not know what it is but they also don't magically know what %1$s. Someone has to have told them, so adding a couple of tags to the "this is magic stuff you should not mess with" list is not a big deal.

    There's also no real issue with separation of concerns here: you have decided that the text needs to have a certain part emphasized. You need a way to describe where the emphasis goes; a small subset of HTML can be used to do exactly that. If you didn't use HTML you would still need some other way to solve the problem, and that way could be a lot worse.

    I can also not emphasize enough that concatenating strings to form a sentence is an i18n practice that you will deeply regret in the future.