Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a system for internationalizing C++ streams?

I'm looking into how to internationalize my C++ projects, and it didn't take me long to wonder how one handles the translation of streamed text that's interspersed with non-string values. The only page I've found so far that even mentions this would be this C++ FQA page, though that page unfortunately isn't too interested in offering any solutions. There's also this Boost mailing list thread from 2000 that doesn't appear to go anywhere.

As an example, for this C-style printf statement:

printf("There are %d lines in '%s'.", numlines, filename);

It's trivial to wrap entire message in some sort of translation function, such as gettext's various functions, and allow the text and its nonliteral components moved around as needed. If you have access to the POSIX version of printf (or some other library offering improvements to the printf format), you can even arrange the values in a different order as needed.

However, for the equivalent C++ stream-based statement:

 std::cout << "There are " << numlines
           << "lines in '" << filename << "'.";

I have yet to find a way to mark the entire message for translation. You could wrap up each string in the appropriate functions, but that requires the translator to know that these three strings all are a part of one message, and what appears between them. Additionally, at least some i18n solutions would need to be told that other occurrences of the same string literal are distinct, for languages where that content of that literal changes based on context. And forget about situations where you'd have to rearrange the non-string-literal values.

So my question is, is there an internationalization solution out there that supports the use of streaming operations, or are there only printf-style solutions to the concerns I raised?

like image 860
ShimmerFairy Avatar asked Nov 10 '22 06:11

ShimmerFairy


1 Answers

Boost.Format can help:

cout << boost::format("There are %d lines in '%s'.") % numlines % filename;

or

cout << boost::format("There are %1% lines in '%2%'.") % numlines % filename;
like image 93
musiphil Avatar answered Nov 15 '22 07:11

musiphil