diff options
Diffstat (limited to 'src/gettext.h')
-rw-r--r-- | src/gettext.h | 29 |
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; +} |