diff options
Diffstat (limited to 'src/gettext.h')
-rw-r--r-- | src/gettext.h | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/gettext.h b/src/gettext.h index 5a3654be4..6225fef93 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> @@ -47,8 +48,7 @@ void init_gettext(const char *path, const std::string &configured_language, extern wchar_t *utf8_to_wide_c(const char *str); -// You must free the returned string! -// The returned string is allocated using new +// The returned string must be freed using delete[] inline const wchar_t *wgettext(const char *str) { // We must check here that is not an empty string to avoid trying to translate it @@ -77,3 +77,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; +} |