GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/gettexthelper.cpp Lines: 0 24 0.0 %
Date: 2017-11-29 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
35
#ifdef WIN32
36
#include <string>
37
extern "C" char const *_nl_locale_name_default(void);
38
#endif  // WIN32
39
#elif defined(ENABLE_CUSTOMNLS)
40
#include "utils/translation/podict.h"
41
#ifdef __native_client__
42
#include "utils/naclmessages.h"
43
#endif  // __native_client__
44
#endif  // ENABLE_NLS
45
46
#if defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS) && !defined(WIN32)
47
#include "utils/env.h"
48
#endif  // defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS) && !defined(WIN32)
49
50
#include "debug.h"
51
52
#if defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
53
static std::string setLangEnv()
54
{
55
    std::string lang = config.getStringValue("lang");
56
#if defined(ENABLE_NLS) && defined(WIN32)
57
    if (lang.empty())
58
        lang = std::string(_nl_locale_name_default());
59
#elif defined(ENABLE_CUSTOMNLS) && defined(__native_client__)
60
    if (lang.empty())
61
    {
62
        NaclMessageHandle *handle = naclRegisterMessageHandler(
63
            "get-uilanguage");
64
        naclPostMessage("get-uilanguage", "");
65
        lang = naclWaitForMessage(handle);
66
    }
67
#endif  // defined(ENABLE_NLS) && defined(WIN32)
68
69
    if (!lang.empty())
70
    {
71
#ifdef WIN32
72
        putenv(const_cast<char*>(("LANG=" + lang).c_str()));
73
        putenv(const_cast<char*>(("LANGUAGE=" + lang).c_str()));
74
#else  // WIN32
75
76
        if (!lang.empty())
77
        {
78
            setEnv("LANG", lang.c_str());
79
            setEnv("LANGUAGE", lang.c_str());
80
        }
81
#endif  // WIN32
82
    }
83
84
    return lang;
85
}
86
#endif  // defined(ENABLE_NLS) || defined(ENABLE_CUSTOMNLS)
87
88
void GettextHelper::initLang()
89
{
90
#ifdef ENABLE_NLS
91
    const std::string lang = setLangEnv();
92
#ifdef WIN32
93
    // mingw doesn't like LOCALEDIR to be defined for some reason
94
    if (lang != "C")
95
        bindTextDomain("translations/");
96
#else  // WIN32
97
#ifdef ANDROID
98
#ifdef USE_SDL2
99
    bindTextDomain((std::string(getenv("APPDIR")).append("/locale")).c_str());
100
#else  // USE_SDL2
101
102
    bindTextDomain((std::string(VirtFs::getBaseDir()).append(
103
        "/locale")).c_str());
104
#endif  // USE_SDL2
105
#else  // ANDROID
106
#ifdef ENABLE_PORTABLE
107
    bindTextDomain((std::string(VirtFs::getBaseDir()).append(
108
        "../locale/")).c_str());
109
#else  // ENABLE_PORTABLE
110
#ifdef __APPLE__
111
    bindTextDomain((std::string(VirtFs::getBaseDir())
112
        .append("ManaPlus.app/Contents/Resources/locale/")).c_str());
113
#else  // __APPLE__
114
115
    bindTextDomain(LOCALEDIR);
116
#endif  // __APPLE__
117
#endif  // ENABLE_PORTABLE
118
#endif  // ANDROID
119
#endif  // WIN32
120
121
    char *locale = setlocale(LC_MESSAGES, lang.c_str());
122
    if (locale)
123
    {
124
        logger->log("locale: %s", locale);
125
    }
126
    else
127
    {
128
        locale = setlocale(LC_MESSAGES, (lang + ".utf8").c_str());
129
        if (locale)
130
            logger->log("locale: %s", locale);
131
        else
132
            logger->log("locale empty");
133
    }
134
    bind_textdomain_codeset("manaplus", "UTF-8");
135
    textdomain("manaplus");
136
#elif defined(ENABLE_CUSTOMNLS)
137
    mainTranslator = new PoDict("en");
138
    setLangEnv();
139
#endif  // ENABLE_NLS
140
}
141
142
#ifdef ENABLE_NLS
143
void GettextHelper::bindTextDomain(const char *const path)
144
{
145
    const char *const dir = bindtextdomain("manaplus", path);
146
    if (dir)
147
        logger->log("bindtextdomain: %s", dir);
148
    else
149
        logger->log("bindtextdomain failed");
150
}
151
#else  // ENABLE_NLS
152
153
void GettextHelper::bindTextDomain(const char *const path A_UNUSED)
154
{
155
}
156
#endif  // ENABLE_NLS