aboutsummaryrefslogtreecommitdiff
path: root/src/gettext.h
diff options
context:
space:
mode:
authorRiceball LEE <snowyu.lee@gmail.com>2021-11-01 20:27:46 +0800
committerGitHub <noreply@github.com>2021-11-01 12:27:46 +0000
commit693f98373bc4681d8eac1ab898f9ca9b9c9860d2 (patch)
tree638b1434b4a693c33cfc9f34eba23766503d5f78 /src/gettext.h
parent6910c8d920acedb3f1df1ac03a5cdf14f5fb6081 (diff)
downloadminetest-693f98373bc4681d8eac1ab898f9ca9b9c9860d2.tar.xz
Localize error messages in mainmenu (#11495)
Co-authored-by: sfan5 <sfan5@live.de> Co-authored-by: rubenwardy <rw@rubenwardy.com>
Diffstat (limited to 'src/gettext.h')
-rw-r--r--src/gettext.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/gettext.h b/src/gettext.h
index 5a3654be4..67fd9244f 100644
--- a/src/gettext.h
+++ b/src/gettext.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h" // for USE_GETTEXT
#include <string>
+#include "porting.h"
#if USE_GETTEXT
#include <libintl.h>
@@ -77,3 +78,31 @@ inline std::wstring fwgettext(const char *src, Args&&... args)
delete[] str;
return std::wstring(buf);
}
+
+/**
+ * Returns translated string with format args applied
+ *
+ * @tparam Args Template parameter for format args
+ * @param format Translation source string
+ * @param args Variable format args
+ * @return translated string.
+ */
+template <typename ...Args>
+inline std::string fmtgettext(const char *format, Args&&... args)
+{
+ std::string buf;
+ std::size_t buf_size = 256;
+ buf.resize(buf_size);
+
+ format = gettext(format);
+
+ int len = porting::mt_snprintf(&buf[0], buf_size, format, std::forward<Args>(args)...);
+ if (len <= 0) throw std::runtime_error("gettext format error: " + std::string(format));
+ if ((size_t)len >= buf.size()) {
+ buf.resize(len+1); // extra null byte
+ porting::mt_snprintf(&buf[0], buf.size(), format, std::forward<Args>(args)...);
+ }
+ buf.resize(len); // remove null bytes
+
+ return buf;
+}