This article is clearly at level “piece of cake” for those of you working day to day with UTF-8 and unicode headaches, but I have plenty of problems with my own team to explain why they should never put a string like this:
$SomeString = “This value must not be inferior to”;
in the Dokeos translations system, but they never learn 🙂
This would later appear in the code as something close to:
and this would make it available in all the supported languages (if translated by a good will in his own language).
So here is an attempt at explaining clearly what it’s all about, and why this kind of expression should be avoided at all cost in a translation system.
First of all, let’s delve a bit deeper into the string above, to make sure we all start on the same bases. The idea is to have a final string in the user interface which will look like this:
This value must not be inferior to 25
OK, you all get the point?
Now where is the problem in that? Well, the problem is that not all languages work the same or, better said, have the same sentence construction rules. The same sentence would be translated in the following ways:
French: Cette valeur ne peut pas être inférieure à 25
Spanish: Este valor no puede ser inferior a 25
Dutch: Dit waarde macht niet minder dan 25 zijn
Did you see what just happened? Yes, “25” was not the last word of the sentence! So, how do you fix that now with your string, huh? You’re pretty stuck now, aren’t you? Well, that’s why I’m writing this. Dutch is not the only language with this kind of structural change. Japanese would most probably put 25 (ni-juu-go) right in the middle of that sentence.
Now, I hope you already learnt something right now. And I hope that what you learnt is that you have to think a bit more and be a little less egoistic when programming. A lot of people might use your program (particularly if it’s open-source), and a huge lot of these will not speak a work of English or French or even Spanish, so you will have to find new ways to do things properly.
One of these new ways is to use the nice sprintf() function in PHP (in our case, of course), and pass parameters in the form of %s (string) or %d (decimal) inside your string.
For example, the previous string could now be stored in a language file this way:
$SomeString = “This value must not be inferior to %d”;
When translating this string to Dutch, I would write:
$SomeString = “Deze waarde mag niet minder dan %d zijn”;
Did you see that? Now the translation is completely up to the translator. No more problem of positionning the number at the end of the string.
Then you would call it from your script not this way:
Et voilà! Now you know why I’m upset when I see a partial string translation… So let’s make it a better world for all translators around! Let’s give them multilingual-ready strings to translate!
Note: Purists will tell me that I should use printf() instead of echo + sprintf(), and also that I should use %d and not %s, and I would agree, but I’m just trying to show a one-simple-step change between the previous form and this one.
Thanks for reading.