- char *ret = NULL;
- ret = setlocale(LC_CTYPE, l);
- if(ret == NULL)
- std::cout<<"locale could not be set"<<std::endl;
- else
- std::cout<<"locale has been set to:"<<ret<<std::endl;
+ wchar_t buf[255];
+ const wchar_t* str = wgettext(src);
+ swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, std::forward<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;