2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2012, Blender Foundation
17 * All rights reserved.
21 #include <boost/locale.hpp>
23 #include "boost_locale_wrapper.h"
25 static std::string messages_path;
26 static std::string default_domain;
27 static std::string locale_str;
29 /* Note: We cannot use short stuff like boost::locale::gettext, because those return
30 * std::basic_string objects, which c_ptr()-returned char* is no more valid
31 * once deleted (which happens as soons they are out of scope of this func). */
32 typedef boost::locale::message_format<char> char_message_facet;
33 static std::locale locale_global;
34 static char_message_facet const *facet_global = NULL;
36 static void bl_locale_global_cache()
38 /* Cache facet in global variable. Not only is it better for performance,
39 * it also fixes crashes on macOS when doing translation from threads other
40 * than main. Likely because of some internal thread local variables. */
42 /* facet_global reference is valid as long as local_global exists,
43 * so we store both. */
44 locale_global = std::locale();
45 facet_global = &std::use_facet<char_message_facet>(locale_global);
47 catch(const std::bad_cast &e) { /* if std::has_facet<char_message_facet>(l) == false, LC_ALL = "C" case */
49 std::cout << "bl_locale_global_cache:" << e.what() << " \n";
54 catch(const std::exception &e) {
56 std::cout << "bl_locale_global_cache:" << e.what() << " \n";
63 void bl_locale_init(const char *_messages_path, const char *_default_domain)
65 // Avoid using ICU backend, we do not need its power and it's rather heavy!
66 boost::locale::localization_backend_manager lman = boost::locale::localization_backend_manager::global();
68 lman.select("winapi");
72 boost::locale::localization_backend_manager::global(lman);
74 messages_path = _messages_path;
75 default_domain = _default_domain;
78 void bl_locale_set(const char *locale)
80 boost::locale::generator gen;
82 // Specify location of dictionaries.
83 gen.add_messages_path(messages_path);
84 gen.add_messages_domain(default_domain);
85 //gen.set_default_messages_domain(default_domain);
88 if (locale && locale[0]) {
89 _locale = gen(locale);
92 #if defined(__APPLE__) && !defined(WITH_HEADLESS) && !defined(WITH_GHOST_SDL)
93 std::string locale_osx = osx_user_locale() + std::string(".UTF-8");
94 _locale = gen(locale_osx.c_str());
99 std::locale::global(_locale);
100 // Note: boost always uses "C" LC_NUMERIC by default!
102 bl_locale_global_cache();
104 // Generate the locale string (useful to know which locale we are actually using in case of "default" one).
105 #define LOCALE_INFO std::use_facet<boost::locale::info>(_locale)
107 locale_str = LOCALE_INFO.language();
108 if (LOCALE_INFO.country() != "") {
109 locale_str += "_" + LOCALE_INFO.country();
111 if (LOCALE_INFO.variant() != "") {
112 locale_str += "@" + LOCALE_INFO.variant();
118 catch(std::exception const &e) {
119 std::cout << "bl_locale_set(" << locale << "): " << e.what() << " \n";
123 const char *bl_locale_get(void)
125 return locale_str.c_str();
128 const char *bl_locale_pgettext(const char *msgctxt, const char *msgid)
131 char const *r = facet_global->get(0, msgctxt, msgid);