]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gettext.h
Add a callback: minetest.register_on_craft(itemstack, player,
[dragonfireclient.git] / src / gettext.h
1 #ifndef GETTEXT_HEADER
2 #include "config.h" // for USE_GETTEXT
3 #include "log.h"
4
5 #if USE_GETTEXT
6 #include <libintl.h>
7 #else
8 #define gettext(String) String
9 #endif
10
11 #define _(String) gettext(String)
12 #define gettext_noop(String) String
13 #define N_(String) gettext_noop (String)
14
15 #if defined(_WIN32)
16 #define WIN32_LEAN_AND_MEAN
17 #ifndef _WIN32_WINNT
18         #define _WIN32_WINNT 0x0501
19 #endif
20 #include <windows.h>
21 #endif
22
23 inline void init_gettext(const char *path) {
24 #if USE_GETTEXT
25         // don't do this if MSVC compiler is used, it gives an assertion fail
26         #ifndef _MSC_VER
27                 setlocale(LC_MESSAGES, "");
28         #endif
29         bindtextdomain(PROJECT_NAME, path);
30         textdomain(PROJECT_NAME);
31 #if defined(_WIN32)
32         // As linux is successfully switched to UTF-8 completely at about year 2005
33         // Windows still uses obsolete codepage based locales because you
34         // cannot recompile closed-source applications
35
36         // Set character encoding for Win32
37         char *tdomain = textdomain( (char *) NULL );
38         if( tdomain == NULL )
39         {
40                 fprintf( stderr, "warning: domainname parameter is the null pointer, default domain is not set\n" );
41                 tdomain = (char *) "messages";
42         }
43         /*char *codeset = */bind_textdomain_codeset( tdomain, "UTF-8" );
44         //fprintf( stdout, "%s: debug: domainname = %s; codeset = %s\n", argv[0], tdomain, codeset );
45 #endif // defined(_WIN32)
46 #endif
47 }
48
49 inline wchar_t* chartowchar_t(const char *str)
50 {
51         wchar_t* nstr = 0;
52 #if defined(_WIN32)
53         int nResult = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, 0, 0 );
54         if( nResult == 0 )
55         {
56                 fprintf( stderr, "error: MultiByteToWideChar returned null\n" );
57         }
58         else
59         {
60                 nstr = new wchar_t[nResult];
61                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult );
62         }
63 #else
64         size_t l = strlen(str)+1;
65         nstr = new wchar_t[l];
66         mbstowcs(nstr, str, l);
67 #endif
68
69         return nstr;
70 }
71
72 inline wchar_t* wgettext(const char *str)
73 {
74         return chartowchar_t(gettext(str));
75 }
76
77 inline void changeCtype(const char *l)
78 {
79         /*char *ret = NULL;
80         ret = setlocale(LC_CTYPE, l);
81         if(ret == NULL)
82                 infostream<<"locale could not be set"<<std::endl;
83         else
84                 infostream<<"locale has been set to:"<<ret<<std::endl;*/
85 }
86
87 inline std::wstring wstrgettext(std::string text) {
88         wchar_t* wlabel = wgettext(text.c_str());
89         std::wstring out = (std::wstring)wlabel;
90         delete[] wlabel;
91         return out;
92 }
93 #define GETTEXT_HEADER
94 #endif