summaryrefslogtreecommitdiff
path: root/glib/glibmm/init.cc
blob: 0b34447d18ac6d4b6c2bb2817d602609c8d05314 (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
/* Copyright (C) 2003 The glibmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glibmm/init.h>
#include <glibmm/error.h>
#include <locale>
#include <clocale>
#include <stdexcept>

namespace
{
  bool init_to_users_preferred_locale = true;

} // anonymous namespace

namespace Glib
{
void set_init_to_users_preferred_locale(bool state)
{
  init_to_users_preferred_locale = state;
}

bool get_init_to_users_preferred_locale()
{
  return init_to_users_preferred_locale;
}

void init()
{
  static bool is_initialized = false;

  if (is_initialized)
    return;

  if (init_to_users_preferred_locale)
  {
    try
    {
      // Set the global locale for C++ functions and the locale for C functions
      // to the user-preferred locale.
      std::locale::global(std::locale(""));
    }
    catch (const std::runtime_error& ex)
    {
      g_warning("Can't set the global locale to the user's preferred locale.\n"
        "   %s\n   The environment variable LANG may be wrong.\n", ex.what());
    }
  }
  else
  {
    try
    {
      // Make the C++ locale equal to the C locale.
      std::locale::global(std::locale(std::setlocale(LC_ALL, nullptr)));
    }
    catch (const std::runtime_error& ex)
    {
      g_warning("Can't make the global C++ locale equal to the C locale.\n"
        "   %s\n   C locale = %s\n", ex.what(), std::setlocale(LC_ALL, nullptr));
    }
  }

  // Also calls Glib::wrap_register_init() and Glib::wrap_init().
  Glib::Error::register_init();

  is_initialized = true;
}

} // namespace Glib