]> git.mxchange.org Git - flightgear.git/commitdiff
Translation helpers, add global functions.
authorJames Turner <zakalawe@mac.com>
Thu, 14 Nov 2013 16:22:13 +0000 (16:22 +0000)
committerJames Turner <zakalawe@mac.com>
Thu, 14 Nov 2013 22:03:00 +0000 (22:03 +0000)
These are designed for compact access to translations, including a
positional printf.

src/Main/locale.cxx
src/Main/locale.hxx

index 1d30dfed0f3d086cd972d7ea1dfce642dd4358b6..41fafe995f6c2b92ecfeb98264706f3bf78da202 100644 (file)
@@ -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;
+    
+}
index 984a4c8028e7a3d2feae5da7a9f6e5fba007b2fd..a79a296b65828936a03d093402a73ccfd297f58c 100644 (file)
@@ -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