<p>A while back I was reading the W3C article on '<a href="http://www.w3.org/International/articles/text-reuse/" rel="noreferrer">Re-using Strings in Scripted Content</a>', which contains some useful advice on internationalisation, but which strikes me as at odds iwth the DRY (Don't Repeat Yourself) principle of eliminating repetitive code.</p>

<p>To take their example, we might have some code like this...</p>

<pre><code>print "The printer is ";
if (printer.working) {
    print "on.\n";
} else {
    print "off.\n";
}

print "The stapler is ";
if (stapler.working) {
    print "on.\n";
} else {
    print "off.\n";
}
</code></pre>

<p>My instinct would be to eliminate the repetition roughly as follows...</p>

<pre><code>report-state(printer, "printer");
report-state(stapler, "stapler");

function report-state(name, object) {
    print "The "+name+" is ";
    if (object.working) {
        print "on\n";
    } else {
        print "off\n";
    }
}
</code></pre>

<p>...but doing so would cause a difficulty in the code if we needed to localise it to Spanish because the word for 'on' is apparently different in those two cases.</p>

<p>So, I guess my question is, how have other developers approached balancing the DRY principle with internationalisation of their code?</p>

<p>Part of me wants to argue that internationalisation is one of those extreme programming “<a href="http://www.extremeprogramming.org/rules/early.html" rel="noreferrer">you arent gonna need it</a>” situations. On the flip side however, refactoring with the DRY principle in mind is supposed to balance this by making it easy to implement functionality as it’s required, not harder as it does here.</p>

[{'answer_id': 56609, 'author': 'badbod99', 'author_id': 3614, 'author_profile': 'https://Stackoverflow.com/users/3614', 'pm_score': 0, 'selected': False, 'text': '<p>I would suggest using a CMS rather than hardcoding in your textual values to cover localisation.</p>\n'}, {'answer_id': 56617, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 5, 'selected': True, 'text': "<p>I'd try to keep complete sentences in the language resource. As you said you might need different words in different contexts. But a bigger problem is that the order of sentences might be different in different languages. So building up strings from words can cause problems.</p>\n\n<p>Just store</p>\n\n<pre><code>The printer is on\nThe printer is off\nThe stapler is on\nThe stapler is off\n</code></pre>\n\n<p>in the language resource for every language. The repetition here is less of a maintenance headache than trying to figure out where all the single words are going to pop up in your application.</p>\n"}, {'answer_id': 56640, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 2, 'selected': False, 'text': '<p>I agree with Mendelt Siebenga when he says you should keep entire sentences or phrases in your language resource files.  Differences in grammar will always prevent you from doing single word replacement across languages.  This will still lead to less repetitive code than your first example because you only need to check the object type and its state, then print the appropriate message from the language resource.</p>\n'}, {'answer_id': 56651, 'author': 'Chris McAtackney', 'author_id': 5827, 'author_profile': 'https://Stackoverflow.com/users/5827', 'pm_score': 1, 'selected': False, 'text': '<p>I suppose it depends on the level of language quality that you are aiming to achieve.</p>\n\n<p>By trying to minimise repetition of code that deals with these real language strings, you are just exposing yourself to a whole other layer of logic in the syntaxes and structures of different languages. There would be a massive amount of work involved in producing code which still retains the original structure of the language whilst minimising repetition.</p>\n\n<p>You\'d have to decide which was a more suitable approach to a particular problem; Code that repeats itself, or code that tries to be a Jack of all Trades and accomodates for countless rules of language (no doubt a maintenance nightmare).</p>\n\n<p>Of course, you can strike a middle-ground and minimise your code repitition but give up satisfactory grammatical eloquence. Take the example of Ultima Online - when it was localised, a string that previously read "A pile of 329 gold coins" became something like "A pile of gold coins: 329". Not great, but a fairly reasonable solution that lends itself easily to localisation.</p>\n'}, {'answer_id': 60925, 'author': 'maccullt', 'author_id': 4945, 'author_profile': 'https://Stackoverflow.com/users/4945', 'pm_score': 2, 'selected': False, 'text': '<p>We try not to create message strings by program manipulation because the loc. team can\'t see them.</p>\n\n<p>The loc. team actually prefer separate but nearly duplicate messages. \nHowever they will accept parameterized messages.</p>\n\n<p>E.g., "The %(appliance)% is %(on_or_off)%."</p>\n\n<p>The parameters can break down but at least it\'s more obvious to the loc team when it will work and when it won\'t.</p>\n'}, {'answer_id': 103623, 'author': 'user19050', 'author_id': 19050, 'author_profile': 'https://Stackoverflow.com/users/19050', 'pm_score': 3, 'selected': False, 'text': '<p>100% agree with Mendelt.</p>\n\n<p>It is not only a maintenance problem, but can also be a linguistic one.\nIn all Latin languages the gender, number, and case of the subject affect other elements.\nExample for Romanian</p>\n\n<pre><code>  The printer is on: Imprimanta este pornită // feminine\n  The printer is off: Imprimanta este oprită\n  The stapler is on: Perforatorul este pornit // masculine\n  The stapler is off: Perforatorul este oprit\n</code></pre>\n\n<p>Also see <a href="http://www.mihai-nita.net/article.php?artID=20060430a" rel="noreferrer">http://www.mihai-nita.net/article.php?artID=20060430a</a></p>\n'}]
['https://Stackoverflow.com/questions/56574', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']