Blender  V2.93
bpy_app_translations.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 
25 #include <Python.h>
26 /* XXX Why bloody hell isn't that included in Python.h???? */
27 #include <structmember.h>
28 
29 #include "BLI_utildefines.h"
30 
31 #include "BPY_extern.h"
32 #include "bpy_app_translations.h"
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "BLT_lang.h"
37 #include "BLT_translation.h"
38 
39 #include "RNA_types.h"
40 
41 #include "../generic/python_utildefines.h"
42 
43 #ifdef WITH_INTERNATIONAL
44 # include "BLI_ghash.h"
45 # include "BLI_string.h"
46 #endif
47 
48 /* ------------------------------------------------------------------- */
52 typedef struct {
53  PyObject_HEAD
55  const char *context_separator;
57  PyObject *contexts;
59  PyObject *contexts_C_to_py;
64  PyObject *py_messages;
66 
67 /* Our singleton instance pointer */
69 
72 #ifdef WITH_INTERNATIONAL
73 
74 /* ------------------------------------------------------------------- */
78 typedef struct GHashKey {
79  const char *msgctxt;
80  const char *msgid;
81 } GHashKey;
82 
83 static GHashKey *_ghashutil_keyalloc(const void *msgctxt, const void *msgid)
84 {
85  GHashKey *key = MEM_mallocN(sizeof(GHashKey), "Py i18n GHashKey");
87  msgctxt);
88  key->msgid = BLI_strdup(msgid);
89  return key;
90 }
91 
92 static uint _ghashutil_keyhash(const void *ptr)
93 {
94  const GHashKey *key = ptr;
95  const uint hash = BLI_ghashutil_strhash(key->msgctxt);
96  return hash ^ BLI_ghashutil_strhash(key->msgid);
97 }
98 
99 static bool _ghashutil_keycmp(const void *a, const void *b)
100 {
101  const GHashKey *A = a;
102  const GHashKey *B = b;
103 
104  /* Note: comparing msgid first, most of the time it will be enough! */
105  if (BLI_ghashutil_strcmp(A->msgid, B->msgid) == false) {
106  return BLI_ghashutil_strcmp(A->msgctxt, B->msgctxt);
107  }
108  return true; /* true means they are not equal! */
109 }
110 
111 static void _ghashutil_keyfree(void *ptr)
112 {
113  const GHashKey *key = ptr;
114 
115  /* We assume both msgctxt and msgid were BLI_strdup'ed! */
116  MEM_freeN((void *)key->msgctxt);
117  MEM_freeN((void *)key->msgid);
118  MEM_freeN((void *)key);
119 }
120 
121 # define _ghashutil_valfree MEM_freeN
122 
125 /* ------------------------------------------------------------------- */
129 /* We cache all messages available for a given locale from all py dicts into a single ghash.
130  * Changing of locale is not so common, while looking for a message translation is,
131  * so let's try to optimize the later as much as we can!
132  * Note changing of locale, as well as (un)registering a message dict, invalidate that cache.
133  */
134 static GHash *_translations_cache = NULL;
135 
136 static void _clear_translations_cache(void)
137 {
138  if (_translations_cache) {
139  BLI_ghash_free(_translations_cache, _ghashutil_keyfree, _ghashutil_valfree);
140  }
141  _translations_cache = NULL;
142 }
143 
144 static void _build_translations_cache(PyObject *py_messages, const char *locale)
145 {
146  PyObject *uuid, *uuid_dict;
147  Py_ssize_t pos = 0;
148  char *language = NULL, *language_country = NULL, *language_variant = NULL;
149 
150  /* For each py dict, we'll search for full locale, then language+country, then language+variant,
151  * then only language keys... */
152  BLT_lang_locale_explode(locale, &language, NULL, NULL, &language_country, &language_variant);
153 
154  /* Clear the cached ghash if needed, and create a new one. */
155  _clear_translations_cache();
156  _translations_cache = BLI_ghash_new(_ghashutil_keyhash, _ghashutil_keycmp, __func__);
157 
158  /* Iterate over all py dicts. */
159  while (PyDict_Next(py_messages, &pos, &uuid, &uuid_dict)) {
160  PyObject *lang_dict;
161 
162 # if 0
163  PyObject_Print(uuid_dict, stdout, 0);
164  printf("\n");
165 # endif
166 
167  /* Try to get first complete locale, then language+country,
168  * then language+variant, then only language. */
169  lang_dict = PyDict_GetItemString(uuid_dict, locale);
170  if (!lang_dict && language_country) {
171  lang_dict = PyDict_GetItemString(uuid_dict, language_country);
172  locale = language_country;
173  }
174  if (!lang_dict && language_variant) {
175  lang_dict = PyDict_GetItemString(uuid_dict, language_variant);
176  locale = language_variant;
177  }
178  if (!lang_dict && language) {
179  lang_dict = PyDict_GetItemString(uuid_dict, language);
180  locale = language;
181  }
182 
183  if (lang_dict) {
184  PyObject *pykey, *trans;
185  Py_ssize_t ppos = 0;
186 
187  if (!PyDict_Check(lang_dict)) {
188  printf("WARNING! In translations' dict of \"");
189  PyObject_Print(uuid, stdout, Py_PRINT_RAW);
190  printf("\":\n");
191  printf(
192  " Each language key must have a dictionary as value, \"%s\" is not valid, "
193  "skipping: ",
194  locale);
195  PyObject_Print(lang_dict, stdout, Py_PRINT_RAW);
196  printf("\n");
197  continue;
198  }
199 
200  /* Iterate over all translations of the found language dict, and populate our ghash cache. */
201  while (PyDict_Next(lang_dict, &ppos, &pykey, &trans)) {
202  const char *msgctxt = NULL, *msgid = NULL;
203  bool invalid_key = false;
204 
205  if ((PyTuple_CheckExact(pykey) == false) || (PyTuple_GET_SIZE(pykey) != 2)) {
206  invalid_key = true;
207  }
208  else {
209  PyObject *tmp = PyTuple_GET_ITEM(pykey, 0);
210  if (tmp == Py_None) {
212  }
213  else if (PyUnicode_Check(tmp)) {
214  msgctxt = PyUnicode_AsUTF8(tmp);
215  }
216  else {
217  invalid_key = true;
218  }
219 
220  tmp = PyTuple_GET_ITEM(pykey, 1);
221  if (PyUnicode_Check(tmp)) {
222  msgid = PyUnicode_AsUTF8(tmp);
223  }
224  else {
225  invalid_key = true;
226  }
227  }
228 
229  if (invalid_key) {
230  printf("WARNING! In translations' dict of \"");
231  PyObject_Print(uuid, stdout, Py_PRINT_RAW);
232  printf("\", %s language:\n", locale);
233  printf(
234  " Keys must be tuples of (msgctxt [string or None], msgid [string]), "
235  "this one is not valid, skipping: ");
236  PyObject_Print(pykey, stdout, Py_PRINT_RAW);
237  printf("\n");
238  continue;
239  }
240  if (PyUnicode_Check(trans) == false) {
241  printf("WARNING! In translations' dict of \"");
242  PyObject_Print(uuid, stdout, Py_PRINT_RAW);
243  printf("\":\n");
244  printf(" Values must be strings, this one is not valid, skipping: ");
245  PyObject_Print(trans, stdout, Py_PRINT_RAW);
246  printf("\n");
247  continue;
248  }
249 
250  /* Do not overwrite existing keys! */
251  if (BPY_app_translations_py_pgettext(msgctxt, msgid) == msgid) {
252  GHashKey *key = _ghashutil_keyalloc(msgctxt, msgid);
253  BLI_ghash_insert(_translations_cache, key, BLI_strdup(PyUnicode_AsUTF8(trans)));
254  }
255  }
256  }
257  }
258 
259  /* Clean up! */
260  MEM_SAFE_FREE(language);
261  MEM_SAFE_FREE(language_country);
262  MEM_SAFE_FREE(language_variant);
263 }
264 
265 const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *msgid)
266 {
267 # define STATIC_LOCALE_SIZE 32 /* Should be more than enough! */
268 
269  GHashKey key;
270  static char locale[STATIC_LOCALE_SIZE] = "";
271  const char *tmp;
272 
273  /* Just in case, should never happen! */
274  if (!_translations) {
275  return msgid;
276  }
277 
278  tmp = BLT_lang_get();
279  if (!STREQ(tmp, locale) || !_translations_cache) {
280  PyGILState_STATE _py_state;
281 
282  BLI_strncpy(locale, tmp, STATIC_LOCALE_SIZE);
283 
284  /* Locale changed or cache does not exist, refresh the whole cache! */
285  /* This func may be called from C (i.e. outside of python interpreter 'context'). */
286  _py_state = PyGILState_Ensure();
287 
288  _build_translations_cache(_translations->py_messages, locale);
289 
290  PyGILState_Release(_py_state);
291  }
292 
293  /* And now, simply create the key (context, messageid) and find it in the cached dict! */
294  key.msgctxt = BLT_is_default_context(msgctxt) ? BLT_I18NCONTEXT_DEFAULT_BPYRNA : msgctxt;
295  key.msgid = msgid;
296 
297  tmp = BLI_ghash_lookup(_translations_cache, &key);
298 
299  return tmp ? tmp : msgid;
300 
301 # undef STATIC_LOCALE_SIZE
302 }
303 
304 #endif /* WITH_INTERNATIONAL */
305 
306 PyDoc_STRVAR(app_translations_py_messages_register_doc,
307  ".. method:: register(module_name, translations_dict)\n"
308  "\n"
309  " Registers an addon's UI translations.\n"
310  "\n"
311  " .. note::\n"
312  " Does nothing when Blender is built without internationalization support.\n"
313  "\n"
314  " :arg module_name: The name identifying the addon.\n"
315  " :type module_name: string\n"
316  " :arg translations_dict: A dictionary built like that:\n"
317  " ``{locale: {msg_key: msg_translation, ...}, ...}``\n"
318  " :type translations_dict: dict\n"
319  "\n");
321  PyObject *args,
322  PyObject *kw)
323 {
324 #ifdef WITH_INTERNATIONAL
325  static const char *kwlist[] = {"module_name", "translations_dict", NULL};
326  PyObject *module_name, *uuid_dict;
327 
328  if (!PyArg_ParseTupleAndKeywords(args,
329  kw,
330  "O!O!:bpy.app.translations.register",
331  (char **)kwlist,
332  &PyUnicode_Type,
333  &module_name,
334  &PyDict_Type,
335  &uuid_dict)) {
336  return NULL;
337  }
338 
339  if (PyDict_Contains(self->py_messages, module_name)) {
340  PyErr_Format(
341  PyExc_ValueError,
342  "bpy.app.translations.register: translations message cache already contains some data for "
343  "addon '%s'",
344  (const char *)PyUnicode_AsUTF8(module_name));
345  return NULL;
346  }
347 
348  PyDict_SetItem(self->py_messages, module_name, uuid_dict);
349 
350  /* Clear cached messages dict! */
351  _clear_translations_cache();
352 #else
353  (void)self;
354  (void)args;
355  (void)kw;
356 #endif
357 
358  /* And we are done! */
359  Py_RETURN_NONE;
360 }
361 
362 PyDoc_STRVAR(app_translations_py_messages_unregister_doc,
363  ".. method:: unregister(module_name)\n"
364  "\n"
365  " Unregisters an addon's UI translations.\n"
366  "\n"
367  " .. note::\n"
368  " Does nothing when Blender is built without internationalization support.\n"
369  "\n"
370  " :arg module_name: The name identifying the addon.\n"
371  " :type module_name: string\n"
372  "\n");
374  PyObject *args,
375  PyObject *kw)
376 {
377 #ifdef WITH_INTERNATIONAL
378  static const char *kwlist[] = {"module_name", NULL};
379  PyObject *module_name;
380 
381  if (!PyArg_ParseTupleAndKeywords(args,
382  kw,
383  "O!:bpy.app.translations.unregister",
384  (char **)kwlist,
385  &PyUnicode_Type,
386  &module_name)) {
387  return NULL;
388  }
389 
390  if (PyDict_Contains(self->py_messages, module_name)) {
391  PyDict_DelItem(self->py_messages, module_name);
392  /* Clear cached messages ghash! */
393  _clear_translations_cache();
394  }
395 #else
396  (void)self;
397  (void)args;
398  (void)kw;
399 #endif
400 
401  /* And we are done! */
402  Py_RETURN_NONE;
403 }
404 
407 /* ------------------------------------------------------------------- */
411 /* This is always available (even when WITH_INTERNATIONAL is not defined). */
412 
414 
416 
417 /* These fields are just empty placeholders, actual values get set in app_translations_struct().
418  * This allows us to avoid many handwriting, and above all,
419  * to keep all context definition stuff in BLT_translation.h! */
420 static PyStructSequence_Field app_translations_contexts_fields[ARRAY_SIZE(_contexts)] = {{NULL}};
421 
422 static PyStructSequence_Desc app_translations_contexts_desc = {
423  "bpy.app.translations.contexts", /* name */
424  "This named tuple contains all predefined translation contexts", /* doc */
427 };
428 
429 static PyObject *app_translations_contexts_make(void)
430 {
431  PyObject *translations_contexts;
433  int pos = 0;
434 
435  translations_contexts = PyStructSequence_New(&BlenderAppTranslationsContextsType);
436  if (translations_contexts == NULL) {
437  return NULL;
438  }
439 
440 #define SetObjString(item) \
441  PyStructSequence_SET_ITEM(translations_contexts, pos++, PyUnicode_FromString((item)))
442 #define SetObjNone() \
443  PyStructSequence_SET_ITEM(translations_contexts, pos++, Py_INCREF_RET(Py_None))
444 
445  for (ctxt = _contexts; ctxt->c_id; ctxt++) {
446  if (ctxt->value) {
447  SetObjString(ctxt->value);
448  }
449  else {
450  SetObjNone();
451  }
452  }
453 
454 #undef SetObjString
455 #undef SetObjNone
456 
457  return translations_contexts;
458 }
459 
462 /* ------------------------------------------------------------------- */
466 PyDoc_STRVAR(app_translations_contexts_doc,
467  "A named tuple containing all predefined translation contexts.\n"
468  "\n"
469  ".. warning::\n"
470  " Never use a (new) context starting with \"" BLT_I18NCONTEXT_DEFAULT_BPYRNA
471  "\", it would be internally\n"
472  " assimilated as the default one!\n");
473 
474 PyDoc_STRVAR(app_translations_contexts_C_to_py_doc,
475  "A readonly dict mapping contexts' C-identifiers to their py-identifiers.");
476 
477 static PyMemberDef app_translations_members[] = {
478  {"contexts",
479  T_OBJECT_EX,
480  offsetof(BlenderAppTranslations, contexts),
481  READONLY,
482  app_translations_contexts_doc},
483  {"contexts_C_to_py",
484  T_OBJECT_EX,
485  offsetof(BlenderAppTranslations, contexts_C_to_py),
486  READONLY,
487  app_translations_contexts_C_to_py_doc},
488  {NULL},
489 };
490 
491 PyDoc_STRVAR(app_translations_locale_doc,
492  "The actual locale currently in use (will always return a void string when Blender "
493  "is built without "
494  "internationalization support).");
495 static PyObject *app_translations_locale_get(PyObject *UNUSED(self), void *UNUSED(userdata))
496 {
497  return PyUnicode_FromString(BLT_lang_get());
498 }
499 
500 /* Note: defining as getter, as (even if quite unlikely), this *may* change during runtime... */
501 PyDoc_STRVAR(app_translations_locales_doc,
502  "All locales currently known by Blender (i.e. available as translations).");
503 static PyObject *app_translations_locales_get(PyObject *UNUSED(self), void *UNUSED(userdata))
504 {
505  PyObject *ret;
507  int num_locales = 0, pos = 0;
508 
509  if (items) {
510  /* This is not elegant, but simple! */
511  for (it = items; it->identifier; it++) {
512  if (it->value) {
513  num_locales++;
514  }
515  }
516  }
517 
518  ret = PyTuple_New(num_locales);
519 
520  if (items) {
521  for (it = items; it->identifier; it++) {
522  if (it->value) {
523  PyTuple_SET_ITEM(ret, pos++, PyUnicode_FromString(it->description));
524  }
525  }
526  }
527 
528  return ret;
529 }
530 
531 static PyGetSetDef app_translations_getseters[] = {
532  /* {name, getter, setter, doc, userdata} */
533  {"locale", (getter)app_translations_locale_get, NULL, app_translations_locale_doc, NULL},
534  {"locales", (getter)app_translations_locales_get, NULL, app_translations_locales_doc, NULL},
535  {NULL},
536 };
537 
538 /* pgettext helper. */
539 static PyObject *_py_pgettext(PyObject *args,
540  PyObject *kw,
541  const char *(*_pgettext)(const char *, const char *))
542 {
543  static const char *kwlist[] = {"msgid", "msgctxt", NULL};
544 
545 #ifdef WITH_INTERNATIONAL
546  char *msgid, *msgctxt = NULL;
547 
548  if (!PyArg_ParseTupleAndKeywords(
549  args, kw, "s|z:bpy.app.translations.pgettext", (char **)kwlist, &msgid, &msgctxt)) {
550  return NULL;
551  }
552 
553  return PyUnicode_FromString((*_pgettext)(msgctxt ? msgctxt : BLT_I18NCONTEXT_DEFAULT, msgid));
554 #else
555  PyObject *msgid, *msgctxt;
556  (void)_pgettext;
557 
558  if (!PyArg_ParseTupleAndKeywords(
559  args, kw, "O|O:bpy.app.translations.pgettext", (char **)kwlist, &msgid, &msgctxt)) {
560  return NULL;
561  }
562 
563  return Py_INCREF_RET(msgid);
564 #endif
565 }
566 
568  app_translations_pgettext_doc,
569  ".. method:: pgettext(msgid, msgctxt=None)\n"
570  "\n"
571  " Try to translate the given msgid (with optional msgctxt).\n"
572  "\n"
573  " .. note::\n"
574  " The ``(msgid, msgctxt)`` parameters order has been switched compared to gettext "
575  "function, to allow\n"
576  " single-parameter calls (context then defaults to BLT_I18NCONTEXT_DEFAULT).\n"
577  "\n"
578  " .. note::\n"
579  " You should really rarely need to use this function in regular addon code, as all "
580  "translation should be\n"
581  " handled by Blender internal code. The only exception are string containing formatting "
582  "(like \"File: %r\"),\n"
583  " but you should rather use :func:`pgettext_iface`/:func:`pgettext_tip` in those cases!\n"
584  "\n"
585  " .. note::\n"
586  " Does nothing when Blender is built without internationalization support (hence always "
587  "returns ``msgid``).\n"
588  "\n"
589  " :arg msgid: The string to translate.\n"
590  " :type msgid: string\n"
591  " :arg msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
592  " :type msgctxt: string or None\n"
593  " :return: The translated string (or msgid if no translation was found).\n"
594  "\n");
596  PyObject *args,
597  PyObject *kw)
598 {
599  return _py_pgettext(args, kw, BLT_pgettext);
600 }
601 
602 PyDoc_STRVAR(app_translations_pgettext_iface_doc,
603  ".. method:: pgettext_iface(msgid, msgctxt=None)\n"
604  "\n"
605  " Try to translate the given msgid (with optional msgctxt), if labels' translation "
606  "is enabled.\n"
607  "\n"
608  " .. note::\n"
609  " See :func:`pgettext` notes.\n"
610  "\n"
611  " :arg msgid: The string to translate.\n"
612  " :type msgid: string\n"
613  " :arg msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
614  " :type msgctxt: string or None\n"
615  " :return: The translated string (or msgid if no translation was found).\n"
616  "\n");
618  PyObject *args,
619  PyObject *kw)
620 {
621  return _py_pgettext(args, kw, BLT_translate_do_iface);
622 }
623 
624 PyDoc_STRVAR(app_translations_pgettext_tip_doc,
625  ".. method:: pgettext_tip(msgid, msgctxt=None)\n"
626  "\n"
627  " Try to translate the given msgid (with optional msgctxt), if tooltips' "
628  "translation is enabled.\n"
629  "\n"
630  " .. note::\n"
631  " See :func:`pgettext` notes.\n"
632  "\n"
633  " :arg msgid: The string to translate.\n"
634  " :type msgid: string\n"
635  " :arg msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
636  " :type msgctxt: string or None\n"
637  " :return: The translated string (or msgid if no translation was found).\n"
638  "\n");
640  PyObject *args,
641  PyObject *kw)
642 {
643  return _py_pgettext(args, kw, BLT_translate_do_tooltip);
644 }
645 
646 PyDoc_STRVAR(app_translations_pgettext_data_doc,
647  ".. method:: pgettext_data(msgid, msgctxt=None)\n"
648  "\n"
649  " Try to translate the given msgid (with optional msgctxt), if new data name's "
650  "translation is enabled.\n"
651  "\n"
652  " .. note::\n"
653  " See :func:`pgettext` notes.\n"
654  "\n"
655  " :arg msgid: The string to translate.\n"
656  " :type msgid: string\n"
657  " :arg msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
658  " :type msgctxt: string or None\n"
659  " :return: The translated string (or ``msgid`` if no translation was found).\n"
660  "\n");
662  PyObject *args,
663  PyObject *kw)
664 {
666 }
667 
669  app_translations_locale_explode_doc,
670  ".. method:: locale_explode(locale)\n"
671  "\n"
672  " Return all components and their combinations of the given ISO locale string.\n"
673  "\n"
674  " >>> bpy.app.translations.locale_explode(\"sr_RS@latin\")\n"
675  " (\"sr\", \"RS\", \"latin\", \"sr_RS\", \"sr@latin\")\n"
676  "\n"
677  " For non-complete locales, missing elements will be None.\n"
678  "\n"
679  " :arg locale: The ISO locale string to explode.\n"
680  " :type msgid: string\n"
681  " :return: A tuple ``(language, country, variant, language_country, language@variant)``.\n"
682  "\n");
684  PyObject *args,
685  PyObject *kw)
686 {
687  PyObject *ret_tuple;
688  static const char *kwlist[] = {"locale", NULL};
689  const char *locale;
690  char *language, *country, *variant, *language_country, *language_variant;
691 
692  if (!PyArg_ParseTupleAndKeywords(
693  args, kw, "s:bpy.app.translations.locale_explode", (char **)kwlist, &locale)) {
694  return NULL;
695  }
696 
698  locale, &language, &country, &variant, &language_country, &language_variant);
699 
700  ret_tuple = Py_BuildValue(
701  "sssss", language, country, variant, language_country, language_variant);
702 
703  MEM_SAFE_FREE(language);
704  MEM_SAFE_FREE(country);
705  MEM_SAFE_FREE(variant);
706  MEM_SAFE_FREE(language_country);
707  MEM_SAFE_FREE(language_variant);
708 
709  return ret_tuple;
710 }
711 
712 static PyMethodDef app_translations_methods[] = {
713  /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */
714  {"register",
716  METH_VARARGS | METH_KEYWORDS,
717  app_translations_py_messages_register_doc},
718  {"unregister",
720  METH_VARARGS | METH_KEYWORDS,
721  app_translations_py_messages_unregister_doc},
722  {"pgettext",
723  (PyCFunction)app_translations_pgettext,
724  METH_VARARGS | METH_KEYWORDS | METH_STATIC,
725  app_translations_pgettext_doc},
726  {"pgettext_iface",
727  (PyCFunction)app_translations_pgettext_iface,
728  METH_VARARGS | METH_KEYWORDS | METH_STATIC,
729  app_translations_pgettext_iface_doc},
730  {"pgettext_tip",
731  (PyCFunction)app_translations_pgettext_tip,
732  METH_VARARGS | METH_KEYWORDS | METH_STATIC,
733  app_translations_pgettext_tip_doc},
734  {"pgettext_data",
735  (PyCFunction)app_translations_pgettext_data,
736  METH_VARARGS | METH_KEYWORDS | METH_STATIC,
737  app_translations_pgettext_data_doc},
738  {"locale_explode",
739  (PyCFunction)app_translations_locale_explode,
740  METH_VARARGS | METH_KEYWORDS | METH_STATIC,
741  app_translations_locale_explode_doc},
742  {NULL},
743 };
744 
745 static PyObject *app_translations_new(PyTypeObject *type,
746  PyObject *UNUSED(args),
747  PyObject *UNUSED(kw))
748 {
749  /* printf("%s (%p)\n", __func__, _translations); */
750 
751  if (!_translations) {
752  _translations = (BlenderAppTranslations *)type->tp_alloc(type, 0);
753  if (_translations) {
754  PyObject *py_ctxts;
756 
758 
759  py_ctxts = _PyDict_NewPresized(ARRAY_SIZE(_contexts));
760  for (ctxt = _contexts; ctxt->c_id; ctxt++) {
761  PyObject *val = PyUnicode_FromString(ctxt->py_id);
762  PyDict_SetItemString(py_ctxts, ctxt->c_id, val);
763  Py_DECREF(val);
764  }
765  _translations->contexts_C_to_py = PyDictProxy_New(py_ctxts);
766  Py_DECREF(py_ctxts); /* The actual dict is only owned by its proxy */
767 
768  _translations->py_messages = PyDict_New();
769  }
770  }
771 
772  return (PyObject *)_translations;
773 }
774 
775 static void app_translations_free(void *obj)
776 {
777  PyObject_Del(obj);
778 #ifdef WITH_INTERNATIONAL
779  _clear_translations_cache();
780 #endif
781 }
782 
783 PyDoc_STRVAR(app_translations_doc,
784  "This object contains some data/methods regarding internationalization in Blender, "
785  "and allows every py script\n"
786  "to feature translations for its own UI messages.\n"
787  "\n");
788 static PyTypeObject BlenderAppTranslationsType = {
789  PyVarObject_HEAD_INIT(NULL, 0)
790  /* tp_name */
791  "bpy.app._translations_type",
792  /* tp_basicsize */
793  sizeof(BlenderAppTranslations),
794  0, /* tp_itemsize */
795  /* methods */
796  /* No destructor, this is a singleton! */
797  NULL, /* tp_dealloc */
798  0, /* tp_vectorcall_offset */
799  NULL, /* getattrfunc tp_getattr; */
800  NULL, /* setattrfunc tp_setattr; */
801  NULL,
802  /* tp_compare */ /* DEPRECATED in python 3.0! */
803  NULL, /* tp_repr */
804 
805  /* Method suites for standard classes */
806  NULL, /* PyNumberMethods *tp_as_number; */
807  NULL, /* PySequenceMethods *tp_as_sequence; */
808  NULL, /* PyMappingMethods *tp_as_mapping; */
809 
810  /* More standard operations (here for binary compatibility) */
811  NULL, /* hashfunc tp_hash; */
812  NULL, /* ternaryfunc tp_call; */
813  NULL, /* reprfunc tp_str; */
814  NULL, /* getattrofunc tp_getattro; */
815  NULL, /* setattrofunc tp_setattro; */
816 
817  /* Functions to access object as input/output buffer */
818  NULL, /* PyBufferProcs *tp_as_buffer; */
819 
820  /*** Flags to define presence of optional/expanded features ***/
821  Py_TPFLAGS_DEFAULT, /* long tp_flags; */
822 
823  app_translations_doc, /* char *tp_doc; Documentation string */
824 
825  /*** Assigned meaning in release 2.0 ***/
826  /* call function for all accessible objects */
827  NULL, /* traverseproc tp_traverse; */
828 
829  /* delete references to contained objects */
830  NULL, /* inquiry tp_clear; */
831 
832  /*** Assigned meaning in release 2.1 ***/
833  /*** rich comparisons ***/
834  NULL, /* richcmpfunc tp_richcompare; */
835 
836  /*** weak reference enabler ***/
837  0, /* long tp_weaklistoffset */
838 
839  /*** Added in release 2.2 ***/
840  /* Iterators */
841  NULL, /* getiterfunc tp_iter; */
842  NULL, /* iternextfunc tp_iternext; */
843 
844  /*** Attribute descriptor and subclassing stuff ***/
845  app_translations_methods, /* struct PyMethodDef *tp_methods; */
846  app_translations_members, /* struct PyMemberDef *tp_members; */
847  app_translations_getseters, /* struct PyGetSetDef *tp_getset; */
848  NULL, /* struct _typeobject *tp_base; */
849  NULL, /* PyObject *tp_dict; */
850  NULL, /* descrgetfunc tp_descr_get; */
851  NULL, /* descrsetfunc tp_descr_set; */
852  0, /* long tp_dictoffset; */
853  NULL, /* initproc tp_init; */
854  NULL, /* allocfunc tp_alloc; */
855  /* newfunc tp_new; */
856  (newfunc)app_translations_new,
857  /* Low-level free-memory routine */
858  app_translations_free, /* freefunc tp_free; */
859  /* For PyObject_IS_GC */
860  NULL, /* inquiry tp_is_gc; */
861  NULL, /* PyObject *tp_bases; */
862  /* method resolution order */
863  NULL, /* PyObject *tp_mro; */
864  NULL, /* PyObject *tp_cache; */
865  NULL, /* PyObject *tp_subclasses; */
866  NULL, /* PyObject *tp_weaklist; */
867  NULL,
868 };
869 
871 {
872  PyObject *ret;
873 
874  /* Let's finalize our contexts structseq definition! */
875  {
877  PyStructSequence_Field *desc;
878 
879  /* We really populate the contexts' fields here! */
880  for (ctxt = _contexts, desc = app_translations_contexts_desc.fields; ctxt->c_id;
881  ctxt++, desc++) {
882  desc->name = ctxt->py_id;
883  desc->doc = NULL;
884  }
885  desc->name = desc->doc = NULL; /* End sentinel! */
886 
887  PyStructSequence_InitType(&BlenderAppTranslationsContextsType,
889  }
890 
891  if (PyType_Ready(&BlenderAppTranslationsType) < 0) {
892  return NULL;
893  }
894 
895  ret = PyObject_CallObject((PyObject *)&BlenderAppTranslationsType, NULL);
896 
897  /* prevent user from creating new instances */
899  /* without this we can't do set(sys.modules) T29635. */
900  BlenderAppTranslationsType.tp_hash = (hashfunc)_Py_HashPointer;
901 
902  return ret;
903 }
904 
906 {
907  /* In case the object remains in a module's name-space, see T44127. */
908 #ifdef WITH_INTERNATIONAL
909  _clear_translations_cache();
910 #endif
911 }
912 
bool BLI_ghashutil_strcmp(const void *a, const void *b)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:718
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
#define BLI_ghashutil_strhash(key)
Definition: BLI_ghash.h:350
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define UNUSED(x)
#define STREQ(a, b)
void BLT_lang_locale_explode(const char *locale, char **language, char **country, char **variant, char **language_country, char **language_variant)
Definition: blt_lang.c:318
const char * BLT_lang_get(void)
Definition: blt_lang.c:289
struct EnumPropertyItem * BLT_lang_RNA_enum_properties(void)
Definition: blt_lang.c:189
#define BLT_I18NCONTEXTS_DESC
const char * BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
#define BLT_I18NCONTEXT_DEFAULT
const char * BLT_pgettext(const char *msgctxt, const char *msgid)
bool BLT_is_default_context(const char *msgctxt)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
const char * BLT_translate_do_tooltip(const char *msgctxt, const char *msgid)
const char * BLT_translate_do_iface(const char *msgctxt, const char *msgid)
_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 type
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define A
PyDoc_STRVAR(app_translations_py_messages_register_doc, ".. method:: register(module_name, translations_dict)\n" "\n" " Registers an addon's UI translations.\n" "\n" " .. note::\n" " Does nothing when Blender is built without internationalization support.\n" "\n" " :arg module_name: The name identifying the addon.\n" " :type module_name: string\n" " :arg translations_dict: A dictionary built like that:\n" " ``{locale: {msg_key: msg_translation, ...}, ...}``\n" " :type translations_dict: dict\n" "\n")
static PyGetSetDef app_translations_getseters[]
static PyObject * app_translations_locale_explode(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw)
#define SetObjString(item)
static PyObject * app_translations_pgettext(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw)
static PyObject * app_translations_py_messages_register(BlenderAppTranslations *self, PyObject *args, PyObject *kw)
static PyTypeObject BlenderAppTranslationsContextsType
static void app_translations_free(void *obj)
static PyStructSequence_Field app_translations_contexts_fields[ARRAY_SIZE(_contexts)]
#define SetObjNone()
static BlenderAppTranslations * _translations
static PyTypeObject BlenderAppTranslationsType
PyObject * BPY_app_translations_struct(void)
static PyObject * app_translations_new(PyTypeObject *type, PyObject *UNUSED(args), PyObject *UNUSED(kw))
static PyObject * app_translations_py_messages_unregister(BlenderAppTranslations *self, PyObject *args, PyObject *kw)
static BLT_i18n_contexts_descriptor _contexts[]
static PyObject * app_translations_pgettext_iface(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw)
static PyObject * app_translations_pgettext_data(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw)
static PyObject * app_translations_locale_get(PyObject *UNUSED(self), void *UNUSED(userdata))
static PyObject * app_translations_contexts_make(void)
static PyMethodDef app_translations_methods[]
void BPY_app_translations_end(void)
static PyMemberDef app_translations_members[]
static PyObject * _py_pgettext(PyObject *args, PyObject *kw, const char *(*_pgettext)(const char *, const char *))
static PyStructSequence_Desc app_translations_contexts_desc
static PyObject * app_translations_pgettext_tip(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw)
static PyObject * app_translations_locales_get(PyObject *UNUSED(self), void *UNUSED(userdata))
PyObject * self
Definition: bpy_driver.c:185
uint pos
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
#define B
static unsigned a[3]
Definition: RandGen.cpp:92
#define hash
Definition: noise.c:169
return ret
PyObject_HEAD const char * context_separator
const char * identifier
Definition: RNA_types.h:446
const char * description
Definition: RNA_types.h:452
PointerRNA * ptr
Definition: wm_files.c:3157