2013-11-03 16:28:16 +00:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 20:19:39 +00:00
|
|
|
#pragma once
|
2013-11-03 16:28:16 +00:00
|
|
|
|
2015-02-05 16:49:14 +00:00
|
|
|
#include "config.h" // for USE_GETTEXT
|
2017-08-16 20:11:45 +00:00
|
|
|
#include <string>
|
2011-07-24 17:13:30 +00:00
|
|
|
|
2011-07-24 11:58:51 +00:00
|
|
|
#if USE_GETTEXT
|
2015-03-07 05:09:27 +00:00
|
|
|
#include <libintl.h>
|
2011-07-21 05:53:13 +00:00
|
|
|
#else
|
2016-08-21 00:40:23 +00:00
|
|
|
// In certain environments, some standard headers like <iomanip>
|
|
|
|
// and <locale> include libintl.h. If libintl.h is included after
|
|
|
|
// we define our gettext macro below, this causes a syntax error
|
|
|
|
// at the declaration of the gettext function in libintl.h.
|
|
|
|
// Fix this by including such a header before defining the macro.
|
|
|
|
// See issue #4446.
|
|
|
|
// Note that we can't include libintl.h directly since we're in
|
|
|
|
// the USE_GETTEXT=0 case and can't assume that gettext is installed.
|
|
|
|
#include <locale>
|
|
|
|
|
2015-03-07 05:09:27 +00:00
|
|
|
#define gettext(String) String
|
2011-07-23 20:36:11 +00:00
|
|
|
#endif
|
2011-07-21 05:53:13 +00:00
|
|
|
|
2011-07-20 14:51:19 +00:00
|
|
|
#define _(String) gettext(String)
|
2015-03-07 05:09:27 +00:00
|
|
|
#define gettext_noop(String) (String)
|
|
|
|
#define N_(String) gettext_noop((String))
|
2011-07-20 14:51:19 +00:00
|
|
|
|
2015-03-07 05:09:27 +00:00
|
|
|
void init_gettext(const char *path, const std::string &configured_language,
|
2015-10-24 17:31:42 +00:00
|
|
|
int argc, char *argv[]);
|
2011-07-23 14:38:37 +00:00
|
|
|
|
2015-07-07 03:55:07 +00:00
|
|
|
extern wchar_t *utf8_to_wide_c(const char *str);
|
2014-04-06 08:39:32 +00:00
|
|
|
|
2015-02-01 22:59:23 +00:00
|
|
|
// You must free the returned string!
|
2015-03-07 05:09:27 +00:00
|
|
|
// The returned string is allocated using new
|
2015-02-01 22:59:23 +00:00
|
|
|
inline const wchar_t *wgettext(const char *str)
|
2011-07-20 14:51:19 +00:00
|
|
|
{
|
2018-03-27 11:49:47 +00:00
|
|
|
// We must check here that is not an empty string to avoid trying to translate it
|
2018-04-09 14:25:57 +00:00
|
|
|
return str[0] ? utf8_to_wide_c(gettext(str)) : utf8_to_wide_c("");
|
2011-07-20 14:51:19 +00:00
|
|
|
}
|
2011-07-30 08:14:58 +00:00
|
|
|
|
2015-02-01 22:59:23 +00:00
|
|
|
inline std::string strgettext(const std::string &text)
|
|
|
|
{
|
2018-03-27 11:49:47 +00:00
|
|
|
return text.empty() ? "" : gettext(text.c_str());
|
2013-08-03 15:57:51 +00:00
|
|
|
}
|
2021-08-19 18:13:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns translated string with format args applied
|
|
|
|
*
|
|
|
|
* @tparam Args Template parameter for format args
|
|
|
|
* @param src Translation source string
|
|
|
|
* @param args Variable format args
|
|
|
|
* @return translated string
|
|
|
|
*/
|
|
|
|
template <typename ...Args>
|
|
|
|
inline std::wstring fwgettext(const char *src, Args&&... args)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|