Is it possible to generate a global list of marked strings at compile time/runtime?

烈酒焚心 提交于 2019-12-06 08:38:23

I think you're almost there. Taking the last idea:

class TrString
{
public:
    static std::set< std::string > sEnglishPhrases;
    std::string phrase;
    TrString(const std::string& english_phrase ):phrase(english_phrase)
    { sEnglishPhrases.insert( english_phrase ); }
    friend ostream &operator<<(ostream &stream, const TrString& o);
};

ostream &operator<<(ostream &stream, const TrString& o)
{
    stream << lookupTranslatedString( currentLocale(), o.phrase);
    return stream;
}

#define TR(x) ( TrString(x) )
// ...
std::cout << TR("This phrase is in English") << std::endl;

And as you say, you do need to run the code over every TR() statement, but you could configure a unit test framework to do this.

My alternative would be to use the above TrString class to make static variables for each module:

// unnamed namespace gives static instances
namespace
{
   TrString InEnglish("This phrase is in English");
   // ...
}

Now you just need to link in an alternative main() to print out TrString:: sEnglishPhrases

You could just build a quick script to parse the file and strip what you need.

awk '/TR\(L"[^"]*")/ {print}' plop.c

If you need somthing slightly more complex then perl is your friend.

What you're looking for seems very similar to what GNU gettext does. In particular, look at the xgettext tool.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!