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
20 /** \file
21 * \ingroup blt
22 *
23 * Manages translation files and provides translation functions.
24 * (which are optional and can be disabled as a preference).
25 */
26
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
BLT_is_default_context(const char * msgctxt)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
BLT_pgettext(const char * msgctxt,const char * msgid)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
BLT_translate(void)81 bool BLT_translate(void)
82 {
83 #ifdef WITH_INTERNATIONAL
84 return BLI_thread_is_main() && (U.language != ULANGUAGE_ENGLISH);
85 #else
86 return false;
87 #endif
88 }
89
BLT_translate_iface(void)90 bool BLT_translate_iface(void)
91 {
92 #ifdef WITH_INTERNATIONAL
93 return BLT_translate() && (U.transopts & USER_TR_IFACE);
94 #else
95 return false;
96 #endif
97 }
98
BLT_translate_tooltips(void)99 bool BLT_translate_tooltips(void)
100 {
101 #ifdef WITH_INTERNATIONAL
102 return BLT_translate() && (U.transopts & USER_TR_TOOLTIPS);
103 #else
104 return false;
105 #endif
106 }
107
BLT_translate_new_dataname(void)108 bool BLT_translate_new_dataname(void)
109 {
110 #ifdef WITH_INTERNATIONAL
111 return BLT_translate() && (U.transopts & USER_TR_NEWDATANAME);
112 #else
113 return false;
114 #endif
115 }
116
BLT_translate_do(const char * msgctxt,const char * msgid)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
BLT_translate_do_iface(const char * msgctxt,const char * msgid)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
BLT_translate_do_tooltip(const char * msgctxt,const char * msgid)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
BLT_translate_do_new_dataname(const char * msgctxt,const char * msgid)162 const char *BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
163 {
164 #ifdef WITH_INTERNATIONAL
165 if (BLT_translate_new_dataname()) {
166 return BLT_pgettext(msgctxt, msgid);
167 }
168
169 return msgid;
170
171 #else
172 (void)msgctxt;
173 return msgid;
174 #endif
175 }
176