Blender  V2.93
boost_locale_wrapper.cpp
Go to the documentation of this file.
1 /*
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.
6  *
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.
11  *
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.
15  *
16  * The Original Code is Copyright (C) 2012, Blender Foundation
17  * All rights reserved.
18  */
19 
20 #include <boost/locale.hpp>
21 #include <stdio.h>
22 
23 #include "boost_locale_wrapper.h"
24 
25 static std::string messages_path;
26 static std::string default_domain;
27 static std::string locale_str;
28 
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;
35 
37 {
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. */
41  try {
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);
46  }
47  catch (const std::bad_cast
48  &e) { /* if std::has_facet<char_message_facet>(l) == false, LC_ALL = "C" case */
49 #ifndef NDEBUG
50  std::cout << "bl_locale_global_cache:" << e.what() << " \n";
51 #endif
52  (void)e;
54  }
55  catch (const std::exception &e) {
56 #ifndef NDEBUG
57  std::cout << "bl_locale_global_cache:" << e.what() << " \n";
58 #endif
59  (void)e;
61  }
62 }
63 
64 void bl_locale_init(const char *_messages_path, const char *_default_domain)
65 {
66  // Avoid using ICU backend, we do not need its power and it's rather heavy!
67  boost::locale::localization_backend_manager lman =
68  boost::locale::localization_backend_manager::global();
69 #if defined(_WIN32)
70  lman.select("winapi");
71 #else
72  lman.select("posix");
73 #endif
74  boost::locale::localization_backend_manager::global(lman);
75 
76  messages_path = _messages_path;
77  default_domain = _default_domain;
78 }
79 
80 void bl_locale_set(const char *locale)
81 {
82  boost::locale::generator gen;
83  std::locale _locale;
84  // Specify location of dictionaries.
85  gen.add_messages_path(messages_path);
86  gen.add_messages_domain(default_domain);
87  // gen.set_default_messages_domain(default_domain);
88 
89  try {
90  if (locale && locale[0]) {
91  _locale = gen(locale);
92  }
93  else {
94 #if defined(__APPLE__) && !defined(WITH_HEADLESS) && !defined(WITH_GHOST_SDL)
95  std::string locale_osx = osx_user_locale() + std::string(".UTF-8");
96  _locale = gen(locale_osx.c_str());
97 #else
98  _locale = gen("");
99 #endif
100  }
101  std::locale::global(_locale);
102  // Note: boost always uses "C" LC_NUMERIC by default!
103 
105 
106  // Generate the locale string
107  // (useful to know which locale we are actually using in case of "default" one).
108 #define LOCALE_INFO std::use_facet<boost::locale::info>(_locale)
109 
110  locale_str = LOCALE_INFO.language();
111  if (LOCALE_INFO.country() != "") {
112  locale_str += "_" + LOCALE_INFO.country();
113  }
114  if (LOCALE_INFO.variant() != "") {
115  locale_str += "@" + LOCALE_INFO.variant();
116  }
117 
118 #undef LOCALE_INFO
119  }
120  // Extra catch on `std::runtime_error` is needed for macOS/Clang as it seems that exceptions
121  // like `boost::locale::conv::conversion_error` (which inherit from `std::runtime_error`) are
122  // not caught by their ancestor `std::exception`. See
123  // https://developer.blender.org/T88877#1177108 .
124  catch (std::runtime_error const &e) {
125  std::cout << "bl_locale_set(" << locale << "): " << e.what() << " \n";
126  }
127  catch (std::exception const &e) {
128  std::cout << "bl_locale_set(" << locale << "): " << e.what() << " \n";
129  }
130 }
131 
132 const char *bl_locale_get(void)
133 {
134  return locale_str.c_str();
135 }
136 
137 const char *bl_locale_pgettext(const char *msgctxt, const char *msgid)
138 {
139  if (facet_global) {
140  char const *r = facet_global->get(0, msgctxt, msgid);
141  if (r) {
142  return r;
143  }
144  }
145 
146  return msgid;
147 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static std::locale locale_global
static void bl_locale_global_cache()
static std::string messages_path
const char * bl_locale_pgettext(const char *msgctxt, const char *msgid)
static std::string locale_str
const char * bl_locale_get(void)
void bl_locale_set(const char *locale)
static char_message_facet const * facet_global
boost::locale::message_format< char > char_message_facet
void bl_locale_init(const char *_messages_path, const char *_default_domain)
static std::string default_domain
#define LOCALE_INFO
const char * osx_user_locale()