Use narrow_to_wide in gettext instead of os dependent conversion fct
[oweals/minetest.git] / src / gettext.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef GETTEXT_HEADER
21 #define GETTEXT_HEADER
22
23 #include "config.h" // for USE_GETTEXT
24 #include "log.h"
25
26 #if USE_GETTEXT
27 #include <libintl.h>
28 #else
29 #define gettext(String) String
30 #endif
31
32 #define _(String) gettext(String)
33 #define gettext_noop(String) String
34 #define N_(String) gettext_noop (String)
35
36 #if defined(_WIN32)
37 #define WIN32_LEAN_AND_MEAN
38 #ifndef _WIN32_WINNT
39         #define _WIN32_WINNT 0x0501
40 #endif
41 #include <windows.h>
42
43 #endif // #if defined(_WIN32)
44
45 #ifdef _MSC_VER
46 void init_gettext(const char *path,std::string configured_language,int argc, char** argv);
47 #else
48 void init_gettext(const char *path,std::string configured_language);
49 #endif
50
51 extern std::wstring narrow_to_wide(const std::string& mbs);
52 #include "util/numeric.h"
53
54
55 /******************************************************************************/
56 inline wchar_t* chartowchar_t(const char *str)
57 {
58         wchar_t* nstr = 0;
59 #if defined(_WIN32)
60         int nResult = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, 0, 0 );
61         if( nResult == 0 )
62         {
63                 errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
64         }
65         else
66         {
67                 nstr = new wchar_t[nResult];
68                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult );
69         }
70 #else
71         size_t l = strlen(str)+1;
72         nstr = new wchar_t[l];
73
74         std::wstring intermediate = narrow_to_wide(str);
75         memset(nstr,0,l);
76         memcpy(nstr,intermediate.c_str(),l*sizeof(wchar_t));
77 #endif
78
79         return nstr;
80 }
81
82 /******************************************************************************/
83 inline wchar_t* wgettext(const char *str)
84 {
85         return chartowchar_t(gettext(str));
86 }
87
88 /******************************************************************************/
89 inline std::wstring wstrgettext(std::string text) {
90         wchar_t* wlabel = wgettext(text.c_str());
91         std::wstring out = (std::wstring)wlabel;
92         delete[] wlabel;
93         return out;
94 }
95
96 #endif