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