summaryrefslogtreecommitdiffstats
path: root/src/helper/gettext.cpp
blob: 0bd3da06a88a1eb25e299ab6c0e958c59ab3f8ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * \file
 * \brief helper functions for gettext
 *//*
 * Authors:
 *   Eduard Braun <eduard.braun2@gmx.de>
 *
 * Copyright 2017 Authors
 *
 * This file is part of Inkscape.
 *
 * Inkscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Inkscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Inkscape.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"  // only include where actually required!
#endif

#ifdef _WIN32
#include <windows.h>
#endif

#ifdef ENABLE_BINRELOC
#include "prefix.h"
#endif

#include <string>
#include <glibmm.h>
#include <glibmm/i18n.h>

namespace Inkscape {

/** does all required gettext initialization and takes care of the respective locale directory paths */
void initialize_gettext() {
#ifdef _WIN32
    gchar *datadir = g_win32_get_package_installation_directory_of_module(NULL);

    // obtain short path to executable dir and pass it
    // to bindtextdomain (it doesn't understand UTF-8)
    gchar *shortdatadir = g_win32_locale_filename_from_utf8(datadir);
    gchar *localepath = g_build_filename(shortdatadir, PACKAGE_LOCALE_DIR, NULL);
    bindtextdomain(GETTEXT_PACKAGE, localepath);
    g_free(shortdatadir);
    g_free(localepath);

    g_free(datadir);
#else
# ifdef ENABLE_BINRELOC
    bindtextdomain(GETTEXT_PACKAGE, BR_LOCALEDIR(""));
# else
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
    // needed by Python/Gettext
    g_setenv("PACKAGE_LOCALE_DIR", PACKAGE_LOCALE_DIR, TRUE);
# endif
#endif

    // Allow the user to override the locale directory by setting
    // the environment variable INKSCAPE_LOCALEDIR.
    char const *inkscape_localedir = g_getenv("INKSCAPE_LOCALEDIR");
    if (inkscape_localedir != nullptr) {
        bindtextdomain(GETTEXT_PACKAGE, inkscape_localedir);
    }

    // common setup
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);
}

/** set gettext codeset to UTF8 */
void bind_textdomain_codeset_utf8() {
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
}

/** set gettext codeset to codeset of the system console
 *   - on *nix this is typically the current locale,
 *   - on Windows we don't care and simply use UTF8
 *       any conversion would need to happen in our console wrappers (see winconsole.cpp) anyway
 *       as we have no easy way of determining console encoding from inkscape/inkview.exe process;
 *       for now do something even easier - switch console encoding to UTF8 and be done with it!
 *     this also works nicely on MSYS consoles where UTF8 encoding is used by default, too  */
void bind_textdomain_codeset_console() {
#ifdef _WIN32
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#else
    std::string charset;
    Glib::get_charset(charset);
    bind_textdomain_codeset(GETTEXT_PACKAGE, charset.c_str());
#endif
}

}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace .0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: