4 ms·
printf_p is pretty neat, thanks for the pointer. But I would bet that you will still find at least one %d gets turned into a %s. I like printf format strings,
by tom_ 4y ago
printf_p is pretty neat, thanks for the pointer. But I would bet that you will still find at least one %d gets turned into a %s.
I like printf format strings, but as a way of handling localizable strings I don't think they are the best.
- skissane 4y agoYou are 100% correct. printf, even with the argument numbering feature, is insufficient for high quality internationalisation. A good example of this is pluralisation. We've all done things like: printf("%d file(s) copied\n", count); which is acceptable but kind of ugly. Some people want to make it nicer: printf("%d file%s copied\n", count, count != 1 ? "s" : ""); Which is fine for English, but doesn't work at all for other languages. The problem is not just that the plural ending is something other than `s` – if it was just that, it wouldn't be too hard. The problem is that the `count != 1` bit only works for English. For example, while 0 is plural in English, in French it is singular. Many other languages are much more complex. The GNU gettext manual has a chapter which goes into this in great detail – https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html https://www.gnu.org/software/gettext/manual/html_node/Plural... printf() has zero hope of coping with this complexity. gettext provides a special function to handle this, ngettext(), which is passed the number as a separate argument, so it can select which plural form to use. And then the translated message files contain a header defining how many plural forms that language has, and the rules to choose which one to use. And for some languages it is crazy complex. Arabic is the most extreme, for which the manual gives this plural rule: Plural-Forms: nplurals=6; \ plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 \ : n%100>=11 ? 4 : 5;