From 1db9d25f56c2b7eb7777ac04344ba7f3657524c5 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Thu, 14 Nov 2013 16:22:13 +0000
Subject: [PATCH] Translation helpers, add global functions.

These are designed for compact access to translations, including a
positional printf.
---
 src/Main/locale.cxx | 33 +++++++++++++++++++++++++++++++++
 src/Main/locale.hxx | 14 ++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/src/Main/locale.cxx b/src/Main/locale.cxx
index 1d30dfed0..41fafe995 100644
--- a/src/Main/locale.cxx
+++ b/src/Main/locale.cxx
@@ -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;
+    
+}
diff --git a/src/Main/locale.hxx b/src/Main/locale.hxx
index 984a4c802..a79a296b6 100644
--- a/src/Main/locale.hxx
+++ b/src/Main/locale.hxx
@@ -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