Blender  V2.93
blt_translation.c
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) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "BLT_translation.h"
31 
32 #include "MEM_guardedalloc.h"
33 
34 #include "DNA_userdef_types.h" /* For user settings. */
35 
36 #ifdef WITH_PYTHON
37 # include "BPY_extern.h"
38 #endif
39 
40 #ifdef WITH_INTERNATIONAL
41 # include "BLI_threads.h"
42 # include "boost_locale_wrapper.h"
43 #endif /* WITH_INTERNATIONAL */
44 
45 bool BLT_is_default_context(const char *msgctxt)
46 {
47  /* We use the "short" test, a more complete one could be:
48  * return (!msgctxt || !msgctxt[0] || STREQ(msgctxt, BLT_I18NCONTEXT_DEFAULT_BPYRNA))
49  */
50  /* Note: trying without the void string check for now, it *should* not be necessary... */
51  return (!msgctxt || msgctxt[0] == BLT_I18NCONTEXT_DEFAULT_BPYRNA[0]);
52 }
53 
54 const char *BLT_pgettext(const char *msgctxt, const char *msgid)
55 {
56 #ifdef WITH_INTERNATIONAL
57  const char *ret = msgid;
58 
59  if (msgid && msgid[0]) {
60  if (BLT_is_default_context(msgctxt)) {
61  msgctxt = BLT_I18NCONTEXT_DEFAULT;
62  }
63  ret = bl_locale_pgettext(msgctxt, msgid);
64  /* We assume if the returned string is the same (memory level) as the msgid,
65  * no translation was found, and we can try py scripts' ones!
66  */
67 # ifdef WITH_PYTHON
68  if (ret == msgid) {
69  ret = BPY_app_translations_py_pgettext(msgctxt, msgid);
70  }
71 # endif
72  }
73 
74  return ret;
75 #else
76  (void)msgctxt;
77  return msgid;
78 #endif
79 }
80 
81 bool BLT_translate(void)
82 {
83 #ifdef WITH_INTERNATIONAL
84  return BLI_thread_is_main();
85 #else
86  return false;
87 #endif
88 }
89 
91 {
92 #ifdef WITH_INTERNATIONAL
93  return BLT_translate() && (U.transopts & USER_TR_IFACE);
94 #else
95  return false;
96 #endif
97 }
98 
100 {
101 #ifdef WITH_INTERNATIONAL
102  return BLT_translate() && (U.transopts & USER_TR_TOOLTIPS);
103 #else
104  return false;
105 #endif
106 }
107 
109 {
110 #ifdef WITH_INTERNATIONAL
111  return BLT_translate() && (U.transopts & USER_TR_NEWDATANAME);
112 #else
113  return false;
114 #endif
115 }
116 
117 const char *BLT_translate_do(const char *msgctxt, const char *msgid)
118 {
119 #ifdef WITH_INTERNATIONAL
120  if (BLT_translate()) {
121  return BLT_pgettext(msgctxt, msgid);
122  }
123 
124  return msgid;
125 
126 #else
127  (void)msgctxt;
128  return msgid;
129 #endif
130 }
131 
132 const char *BLT_translate_do_iface(const char *msgctxt, const char *msgid)
133 {
134 #ifdef WITH_INTERNATIONAL
135  if (BLT_translate_iface()) {
136  return BLT_pgettext(msgctxt, msgid);
137  }
138 
139  return msgid;
140 
141 #else
142  (void)msgctxt;
143  return msgid;
144 #endif
145 }
146 
147 const char *BLT_translate_do_tooltip(const char *msgctxt, const char *msgid)
148 {
149 #ifdef WITH_INTERNATIONAL
150  if (BLT_translate_tooltips()) {
151  return BLT_pgettext(msgctxt, msgid);
152  }
153 
154  return msgid;
155 
156 #else
157  (void)msgctxt;
158  return msgid;
159 #endif
160 }
161 
162 const char *BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
163 {
164 #ifdef WITH_INTERNATIONAL
166  return BLT_pgettext(msgctxt, msgid);
167  }
168 
169  return msgid;
170 
171 #else
172  (void)msgctxt;
173  return msgid;
174 #endif
175 }
int BLI_thread_is_main(void)
Definition: threads.cc:234
#define BLT_I18NCONTEXT_DEFAULT
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
@ USER_TR_NEWDATANAME
@ USER_TR_TOOLTIPS
@ USER_TR_IFACE
Read Guarded memory(de)allocation.
const char * BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
const char * BLT_translate_do(const char *msgctxt, const char *msgid)
bool BLT_translate_iface(void)
bool BLT_translate_tooltips(void)
const char * BLT_pgettext(const char *msgctxt, const char *msgid)
bool BLT_is_default_context(const char *msgctxt)
bool BLT_translate(void)
const char * BLT_translate_do_tooltip(const char *msgctxt, const char *msgid)
const char * BLT_translate_do_iface(const char *msgctxt, const char *msgid)
bool BLT_translate_new_dataname(void)
const char * bl_locale_pgettext(const char *msgctxt, const char *msgid)
unsigned int U
Definition: btGjkEpa3.h:78
return ret