5 ms·
How do you internationalize strings built up from multiple string fragments?
by cbd1984 11y ago
How do you internationalize strings built up from multiple string fragments?
- endgame 11y agoLink for those who need more context: http://yosefk.com/c++fqa/web-vs-c++.html#misfeature-4 http://yosefk.com/c++fqa/web-vs-c++.html#misfeature-4
- simoncion 11y agoThe same way you'd do it in printf? Stringize data and insert it in the middle of static text? (Pardon my inelegant code, I've been away from C++ and doing Erlang for quite a while now.) void printStuff(ostream Os, string WarningPart, string SizePart, string EndPart, int Size) { //The English version might look like: //"Warning: Size too big (200). Get smaller." Os << WarningPart << SizePart << Size << EndPart << endl; }; I expect that if I thought about the problem for a few minutes (and was unable to find any C++ i18n libraries) I would come up with something much nicer and easier to use than this thing I spent ten seconds working on. :)
- prodigal_erik 11y agoPOSIX printf allows a translator to write a conversion spec like "%1$d" that says which arg to print. This lets you do stuff like put currency before or after the amount depending on what's idiomatic for the locale, without recompiling (if you lookup your format strings at runtime).
- simoncion 11y agoSure, but that's l10n, which is a different -but related- thing. :) printf format strings are well thought-out. I was demonstrating that the same ideas can easily be used with io*stream.
- simoncion 11y agoFor the downvoters: Here's a lib that accomplishes this task that's had a fair bit more than 10 seconds of thought put into it: [0]. Examples [1][2]. :) [0] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/index.html http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/in... [1] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/hello_8cpp-example.html http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/he... [2] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/messages_formatting.html http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/me...
- cbd1984 11y agoThat solution is good, but it assumes you can break the message up into those parts for every language and use the same iostream sequence for every language. With printf, we have gettext. I don't really see how there's any analogy for gettext in iosream-world.
- simoncion 11y ago> With printf, we have gettext. I don't really see how there's any analogy for gettext in iosream-world. Funny thing. Boost more-or-less uses gettext, too! https://news.ycombinator.com/item?id=10774941 https://news.ycombinator.com/item?id=10774941
- cbd1984 11y agoAm I being downvoted for asking an honest question?
- simoncion 11y agoIt's better to engage with the folks who have provided responses to your questions than to complain about downvotes.