Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
gettext.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "config.h" // for USE_GETTEXT
8#include "porting.h"
9#include "util/string.h"
10
11#if USE_GETTEXT
12 #include <libintl.h>
13#else
14 // In certain environments, some standard headers like <iomanip>
15 // and <locale> include libintl.h. If libintl.h is included after
16 // we define our gettext macro below, this causes a syntax error
17 // at the declaration of the gettext function in libintl.h.
18 // Fix this by including such a header before defining the macro.
19 // See issue #4446.
20 // Note that we can't include libintl.h directly since we're in
21 // the USE_GETTEXT=0 case and can't assume that gettext is installed.
22 #include <locale>
23
24 #define gettext(String) (String)
25 #define ngettext(String1, String2, n) ((n) == 1 ? (String1) : (String2))
26#endif
27
28#define _(String) gettext(String)
29#define gettext_noop(String) (String)
30#define N_(String) gettext_noop((String))
31
32void init_gettext(const char *path, const std::string &configured_language,
33 int argc, char *argv[]);
34
35inline std::string strgettext(const char *str)
36{
37 // We must check here that is not an empty string to avoid trying to translate it
38 return str[0] ? gettext(str) : "";
39}
40
41inline std::string strgettext(const std::string &str)
42{
43 return strgettext(str.c_str());
44}
45
46inline std::wstring wstrgettext(const char *str)
47{
48 return utf8_to_wide(strgettext(str));
49}
50
51inline std::wstring wstrgettext(const std::string &str)
52{
53 return wstrgettext(str.c_str());
54}
55
64template <typename ...Args>
65inline std::wstring fwgettext(const char *src, Args&&... args)
66{
67 wchar_t buf[255];
68 swprintf(buf, sizeof(buf) / sizeof(wchar_t), wstrgettext(src).c_str(),
69 std::forward<Args>(args)...);
70 return std::wstring(buf);
71}
72
81template <typename ...Args>
82inline std::string fmtgettext(const char *format, Args&&... args)
83{
84 std::string buf;
85 std::size_t buf_size = 256;
86 buf.resize(buf_size);
87
88 format = gettext(format);
89
90 int len = porting::mt_snprintf(&buf[0], buf_size, format, std::forward<Args>(args)...);
91 if (len <= 0) throw std::runtime_error("gettext format error: " + std::string(format));
92 if ((size_t)len >= buf.size()) {
93 buf.resize(len+1); // extra null byte
94 porting::mt_snprintf(&buf[0], buf.size(), format, std::forward<Args>(args)...);
95 }
96 buf.resize(len); // remove null bytes
97
98 return buf;
99}
void init_gettext(const char *path, const std::string &configured_language, int argc, char *argv[])
Definition gettext.cpp:150
std::wstring wstrgettext(const char *str)
Definition gettext.h:46
std::string fmtgettext(const char *format, Args &&... args)
Returns translated string with format args applied.
Definition gettext.h:82
std::wstring fwgettext(const char *src, Args &&... args)
Returns translated string with format args applied.
Definition gettext.h:65
std::string strgettext(const char *str)
Definition gettext.h:35
int mt_snprintf(char *buf, const size_t buf_size, const char *fmt,...)
Definition porting.cpp:841
std::wstring utf8_to_wide(std::string_view input)
Definition string.cpp:87