GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/gettexthelper.cpp Lines: 0 23 0.0 %
Date: 2018-07-14 Branches: 0 24 0.0 %

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