Translation helpers, add global functions.
These are designed for compact access to translations, including a positional printf.
This commit is contained in:
parent
136cd6ac51
commit
1db9d25f56
2 changed files with 47 additions and 0 deletions
|
@ -348,6 +348,24 @@ FGLocale::getDefaultFont(const char* fallbackFont)
|
|||
return fallbackFont;
|
||||
}
|
||||
|
||||
std::string FGLocale::localizedPrintf(const char* id, const char* resource, ... )
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, resource);
|
||||
string r = vlocalizedPrintf(id, resource, args);
|
||||
va_end(args);
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string FGLocale::vlocalizedPrintf(const char* id, const char* resource, va_list args)
|
||||
{
|
||||
const char* format = getLocalizedString(id, resource);
|
||||
int len = ::vsprintf(NULL, format, args);
|
||||
char* buf = (char*) alloca(len);
|
||||
::vsprintf(buf, format, args);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
// Simple UTF8 to Latin1 encoder.
|
||||
void FGLocale::utf8toLatin1(string& s)
|
||||
{
|
||||
|
@ -397,3 +415,18 @@ void FGLocale::utf8toLatin1(string& s)
|
|||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
const char* fgTrMsg(const char* key)
|
||||
{
|
||||
return globals->get_locale()->getLocalizedString(key, "message");
|
||||
}
|
||||
|
||||
std::string fgTrPrintfMsg(const char* key, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, key);
|
||||
string r = globals->get_locale()->vlocalizedPrintf(key, "message", args);
|
||||
va_end(args);
|
||||
return r;
|
||||
|
||||
}
|
||||
|
|
|
@ -67,6 +67,14 @@ public:
|
|||
*/
|
||||
const char* getDefaultFont (const char* fallbackFont);
|
||||
|
||||
/**
|
||||
* Obtain a message string, from a localized resource ID, and use it as
|
||||
* a printf format string.
|
||||
*/
|
||||
std::string localizedPrintf(const char* id, const char* resource, ... );
|
||||
|
||||
std::string vlocalizedPrintf(const char* id, const char* resource, va_list args);
|
||||
|
||||
/**
|
||||
* Simple UTF8 to Latin1 encoder.
|
||||
*/
|
||||
|
@ -105,4 +113,10 @@ protected:
|
|||
SGPropertyNode_ptr _defaultLocale;
|
||||
};
|
||||
|
||||
// global translation wrappers
|
||||
|
||||
const char* fgTrMsg(const char* key);
|
||||
std::string fgTrPrintfMsg(const char* key, ...);
|
||||
|
||||
|
||||
#endif // __FGLOCALE_HXX
|
||||
|
|
Loading…
Add table
Reference in a new issue