   String formatting
  Hey there,I was wondering if someone knows a good library (c++) to format unicode strings in a convenient way.What I'm looking for is something like this:AwesomeStringFormatter fmt;fmt.add_tag("who", "John");fmt.add_tag("target", "Hans-Peter");fmt.add_tag("weapon", "M14");fmt.format("{who} killed {target} with {weapon}.");std::string str = fmt.get.string();I want to use this with localized text, so the positions where the formating tags are positioned within the actual sentence may vary. I'm not quite sure if the boost::format does support something like that. Anyone knows a lib which support something like that?I wanted to ask you guys first before I start to reinvent the wheel ;)Thanks in advance.Imp   
  boost::format should do it, yes. Look it up.Don't localize the "variable names", BTW. It's wasted effort because they actually effectively are just variable names - they aren't supposed to display anyway.   ;  Thanks Zahlman.Hehe, I didn't want to localized the "variable names". I rather wanted to localize it like this:[lng=en]1="{who} killed {target} with {weapon}."2=etc..[lng=de]1="{who} hat {target} mit {weapon} getötet."2=etc.....AwesomeStringFormatter fmt;fmt.add_tag("who", "John");fmt.add_tag("target", "Hans-Peter");fmt.add_tag("weapon", "M14");fmt.format(lng_string_en);std::string str = fmt.get.string();Something along that lines. Atleast that's my current understanding of how to localized text with specific variables in it.Is boost::format able to cope with varying positions of the "variable names"? Thanks in advance.Imp;Quote:Original post by Impz0rIs boost::format able to cope with varying positionsAs far as I know, you don't actually get to use your own variable names (instead you use numeric codes - e.g. "%1% killed %2% with %3%"), but switching the positions around is no problem. That's the whole point of having labels for the substitution-points in the first place :)Seriously, why don't you just read the documentation?   ;  or do the formatting as in printf, scanf:string.format("%s killer %s with %s. Win/Lose: %d/%d", "John", "Hans-Peter", "M14", 1, 0);then you can add sort of an any-type variable which generates if that is integer or string or other data type:string.format("%? killer %? with %?. Win/Lose %?/%?, "John", "Hans-Peter", "M14", 1, 0);   ;Quote:"%1% killed %2% with %3%")Things like this could be hard to the translator, though.std::string message = requires_pure_cpp0x_gold({"who", "John"}, {"target", "Hans-Peter"}, {"weapon", "M14"}).format("{who} killed {target} with {weapon}.");(Just wondering how verbose it would look.)---------------------However, I think that such mad-libs might be untranslatable in the general case anyway, perhaps only between very similar languages. I think even doing German and English there are many things to consider - e.g how you're planning to deal with differences in declension?[Edited by - visitor on August 6, 2010 6:22:35 PM];Quote:Original post by Kasyastring.format("%? killer %? with %?. Win/Lose %?/%?, "John", "Hans-Peter", "M14", 1, 0);That doesn't work. Not all languages use same word order, so the result will be unnatural. There's also more subtle issues, names might need to be displayed in different order, or individual words are merged in one language but not another, meaning there are missing or not enough slots.Quote:I rather wanted to localize it like this:Doesn't include conjugation and other, often frequently occurring grammatical issues. Things also quickly fall apart once gender is involved. That basically requires having a dictionary or similar helper built in localizer.I18N is a topic that is too big for a single developer to handle. For a robust solution there exist so called resource bundles which address all aspects, including the important ones that deal with translators, updating, and the rest of the non-code stuff. IBM offers ICU library. It's big and bulky, but it's enterprise grade.   ;  A real localization engine would probably work something like this.  Needless to say, it's a non-trivial problem.   ;Quote:Original post by ZipsterA real localization engine would probably work something like this.  Needless to say, it's a non-trivial problem.In practice, it's just outsourced to local specialized companies. It's next to impossible to even get a grammatically correct translation, let alone transfer the style and nuances.Even Facebook and other web sites crowdsource translations. It really is that hard of a problem where a lot of time is wasted on tweaking.For small teams without adequate resources, there is probably only one option. Decide upfront which languages to support. Get a linguist of sorts, preferably native, then have them present through entire development to constantly iterate. In this case, printf can work as well, since the details are fixed as they occur.Otherwise, sticking to one language, preferably English is just as good. The effort is simply not worth it for hobby projects.   ;Quote:Original post by AntheusIn practice, it's just outsourced to local specialized companies. It's next to impossible to even get a grammatically correct translation, let alone transfer the style and nuances.Regardless of who does the translation, the translated strings need to be mixed and matched together at run-time, and you need a system to tell you which format string to use based on the variable context.  The complexity of the system (if you take into account gender, plural versus singular, proper nouns, etc.) will determine how close you are to a good translation.  For a hobby project you'll need at the very least simple variable replacement even if you only support one language, but for a large commercial-quality MMO that supports a half-dozen languages and has tons of content you might want better text quality.   