问题
I am planning to rewrite our application using wxWidgets. Also because of that I would like to use the gettext
way of working with human languages. We currently use four of them: Czech, Slovak, English, and German. The specific languages are not that important.
What is important, the Czech and English are considered a kind of base languages. This means that if the use switches to German, and the text is not defined there, he or she automatically gets the English text (no problem with gettext here). However, when the user uses Slovak, then the fallback language is Czech. And when the user uses English it can even fall back to Czech (a kind of bug by developers as English should also be complete).
I know that gettext uses one language as a default -- written directly in the sources. The other languages are supplied as .mo files. Being new to gettext and wxWidgets...
Is there any way to implement the fallback to another explicit language that differs from the default language (i.e. from source files)? In other words, can more languages be present in memory and switched dynamically?
Thanks for your time and experience,
Petr
回答1:
No, you can't selectively provide such fallbacks. OTOH absolutely nothing prevents you from copying Czech translations to Slovak ones (i.e. cp cz.po sk.po
) and then replacing only some of them with the real Slovak translations. And, of course, for German it should work automatically if you use English in your sources (and even if your native language is Czech, I strongly advise you to do this).
回答2:
The language that your app will display is controlled by wxLocale ( http://docs.wxwidgets.org/trunk/classwx_locale.html )
When your application starts up, you can set the language to the system default by calling Init(wxLANGUAGE_DEFAULT) http://docs.wxwidgets.org/trunk/classwx_locale.html#a37c254f20d4862b6efea2fedf63a231a
You can find out what language has been loaded by calling GetLanguage() http://docs.wxwidgets.org/trunk/classwx_locale.html#a6516d2529c936e441d7d23c42dc3e1b4
If the language that is returned is NOT one of the languages you support, then you can change it to whatever you want to use as a fallback for that particular language. For example if GetLanguage tells you that German is used, your code can set it to English but if Slovak then fallback to Czech and so on for as many pairs as you wish to define.
// set languagew to system default
myLocale = new wxLocale;
myLocale->Init();
// find language being used and choose fallback if required
switch( myLocale->GetLanguage() ) {
case wxLANGUAGE_ENGLISH: case wxLANGUAGE_CZECH: ...
// a language we support, nothing more needed
break;
case wxLANGUAGE_SLOVAK: ...
// a language that falls back to czech
myLocale->Init( wxLANGUAGE_CZECH ); break;
case wxLANGUAGE_GERMAN: ...
// a language that falls back to english
myLocale->Init( wxLANGUAGE_ENGLISH ); break;
default:
// a language we dont support and have not specified a fall back for
// for now, let's go with english
myLocale->Init( wxLANGUAGE_ENGLISH ); break;
}
来源:https://stackoverflow.com/questions/13724198/gettext-how-to-fall-back-to-the-base-language