I have a C++ program with many thousands of string literals in the code which need to be translated, for example:
statusBar->Print( "My Message" );
I wrapped the string literals with a function which looks up the value in a dictionary and returns the translated version:
statusBar->Print( Translated( "My Message" ) );
The problem is that after profiling I’ve discovered that doing this look up all over the code is a performance problem. What I’d like to do is change lines like that to:
static const char * translatedMessage5 = Translated( "My Message" );
statusBar->Print( translatedMessage5 );
But due to the many thousands of instances of this in the code, it’s going to be error prone (and a bit of a maintenance nightmare). I was hoping that I could turn Translated into a macro which declared the static variable in-line. This obviously doesn’t work. Anyone have a better idea?
I/O time needed to print your message should be several orders of magnitude more than any dictionary lookup time. If this is not the case, you are doing something wrong.
Tried and tested software is available that does what you need. I suggest you either study GNU Gettext, which is used by every other FOSS project out there, or just use it in your program instead of a homebrew solution.
EDIT: With C++0x it is possible to do what you want, but still consider using GNU Gettext as your real l10n engine. Here’s some proof-of-concept little code:
I’m not sure what the real C++ standard is going to specify, but under gcc-4.6 the
realTranslate()function is called 3 times.