ManaPlus
gettexthelper.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "utils/gettexthelper.h"
23 
24 #if defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
25 #include "configuration.h"
26 #endif // defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
27 
28 #ifdef ENABLE_NLS
29 #include "client.h"
30 #include "logger.h"
31 
32 #include "fs/virtfs/fs.h"
33 
34 #include <libintl.h>
35 #include <locale.h>
36 
37 #ifdef WIN32
38 #include <string>
39 extern "C" char const *_nl_locale_name_default(void);
40 #endif // WIN32
41 #elif defined(ENABLE_CUSTOMNLS)
43 #ifdef __native_client__
44 #include "utils/naclmessages.h"
45 #endif // __native_client__
46 #endif // ENABLE_NLS
47 
48 #if defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS) && !defined(WIN32)
49 #include "utils/env.h"
50 #endif // defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS) && !defined(WIN32)
51 
52 #include "debug.h"
53 
54 #if defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
55 static std::string setLangEnv()
56 {
57  std::string lang = config.getStringValue("lang");
58 #if defined(ENABLE_NLS) && defined(WIN32)
59  if (lang.empty())
60  lang = std::string(_nl_locale_name_default());
61 #elif defined(ENABLE_CUSTOMNLS) && defined(__native_client__)
62  if (lang.empty())
63  {
64  NaclMessageHandle *handle = naclRegisterMessageHandler(
65  "get-uilanguage");
66  naclPostMessage("get-uilanguage", "");
67  lang = naclWaitForMessage(handle);
68  }
69 #endif // defined(ENABLE_NLS) && defined(WIN32)
70 
71  if (!lang.empty())
72  {
73 #ifdef WIN32
74  putenv(const_cast<char*>(("LANG=" + lang).c_str()));
75  putenv(const_cast<char*>(("LANGUAGE=" + lang).c_str()));
76 #else // WIN32
77 
78  setEnv("LANG", lang.c_str());
79  setEnv("LANGUAGE", lang.c_str());
80 #endif // WIN32
81  }
82 
83  return lang;
84 }
85 #endif // defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
86 
88 {
89 #ifdef ENABLE_NLS
90  const std::string lang = setLangEnv();
91 #ifdef WIN32
92  // mingw doesn't like LOCALEDIR to be defined for some reason
93  if (lang != "C")
94  bindTextDomain("translations/");
95 #else // WIN32
96 #ifdef ANDROID
97 #ifdef USE_SDL2
98  bindTextDomain((std::string(getenv("APPDIR")).append("/locale")).c_str());
99 #else // USE_SDL2
100 
101  bindTextDomain((std::string(VirtFs::getBaseDir()).append(
102  "/locale")).c_str());
103 #endif // USE_SDL2
104 #else // ANDROID
105 #ifdef ENABLE_PORTABLE
106  bindTextDomain((std::string(VirtFs::getBaseDir()).append(
107  "../locale/")).c_str());
108 #else // ENABLE_PORTABLE
109 #ifdef __APPLE__
110  bindTextDomain((std::string(VirtFs::getBaseDir())
111  .append("ManaPlus.app/Contents/Resources/locale/")).c_str());
112 #else // __APPLE__
113 
114  bindTextDomain(LOCALEDIR);
115 #endif // __APPLE__
116 #endif // ENABLE_PORTABLE
117 #endif // ANDROID
118 #endif // WIN32
119 
120  char *locale = setlocale(LC_MESSAGES, lang.c_str());
121  if (locale)
122  {
123  logger->log("locale: %s", locale);
124  }
125  else
126  {
127  locale = setlocale(LC_MESSAGES, (lang + ".utf8").c_str());
128  if (locale)
129  logger->log("locale: %s", locale);
130  else
131  logger->log("locale empty");
132  }
133  bind_textdomain_codeset("manaplus", "UTF-8");
134  textdomain("manaplus");
135 #elif defined(ENABLE_CUSTOMNLS)
136  mainTranslator = new PoDict("en");
137  setLangEnv();
138 #endif // ENABLE_NLS
139 }
140 
141 #ifdef ENABLE_NLS
142 void GettextHelper::bindTextDomain(const char *const path)
143 {
144  const char *const dir = bindtextdomain("manaplus", path);
145  if (dir)
146  logger->log("bindtextdomain: %s", dir);
147  else
148  logger->log("bindtextdomain failed");
149 }
150 #else // ENABLE_NLS
151 
152 void GettextHelper::bindTextDomain(const char *const path A_UNUSED)
153 {
154 }
155 #endif // ENABLE_NLS
std::string getStringValue(const std::string &key) const
static void bindTextDomain(const char *const path)
static void initLang()
void log(const char *const log_text,...)
Definition: logger.cpp:269
Definition: podict.h:33
Configuration config
void setEnv(const char *const name, const char *const value)
Definition: env.cpp:60
static std::string setLangEnv()
#define A_UNUSED
Definition: localconsts.h:160
Logger * logger
Definition: logger.cpp:89
const char * getBaseDir()
Definition: fs.cpp:79