16#include <structmember.h>
32#ifdef WITH_INTERNATIONAL
68#ifdef WITH_INTERNATIONAL
88 return get_default_hash(this->context, this->str);
91 static uint64_t hash_as(
const MessageKeyRef &key)
97inline bool operator==(
const MessageKey &a,
const MessageKey &
b)
99 return a.context ==
b.context && a.str ==
b.str;
102inline bool operator==(
const MessageKeyRef &a,
const MessageKey &
b)
104 return a.context ==
b.context && a.str ==
b.str;
120static std::unique_ptr<Map<MessageKey, std::string>> &get_translations_cache()
122 static std::unique_ptr<Map<MessageKey, std::string>> translations;
126static void _clear_translations_cache()
128 get_translations_cache().reset();
131static void _build_translations_cache(PyObject *py_messages,
const char *
locale)
133 PyObject *uuid, *uuid_dict;
135 char *language =
nullptr, *language_country =
nullptr, *language_variant =
nullptr;
140 locale, &language,
nullptr,
nullptr, &language_country, &language_variant);
143 _clear_translations_cache();
144 get_translations_cache() = std::make_unique<Map<MessageKey, std::string>>();
147 while (PyDict_Next(py_messages, &pos, &uuid, &uuid_dict)) {
151 PyObject_Print(uuid_dict, stdout, 0);
157 lang_dict = PyDict_GetItemString(uuid_dict,
locale);
158 if (!lang_dict && language_country) {
159 lang_dict = PyDict_GetItemString(uuid_dict, language_country);
160 locale = language_country;
162 if (!lang_dict && language_variant) {
163 lang_dict = PyDict_GetItemString(uuid_dict, language_variant);
164 locale = language_variant;
166 if (!lang_dict && language) {
167 lang_dict = PyDict_GetItemString(uuid_dict, language);
172 PyObject *pykey, *trans;
175 if (!PyDict_Check(lang_dict)) {
176 printf(
"WARNING! In translations' dict of \"");
177 PyObject_Print(uuid, stdout, Py_PRINT_RAW);
180 " Each language key must have a dictionary as value, \"%s\" is not valid, "
183 PyObject_Print(lang_dict, stdout, Py_PRINT_RAW);
189 while (PyDict_Next(lang_dict, &ppos, &pykey, &trans)) {
190 const char *msgctxt =
nullptr, *msgid =
nullptr;
191 bool invalid_key =
false;
193 if ((PyTuple_CheckExact(pykey) ==
false) || (PyTuple_GET_SIZE(pykey) != 2)) {
197 PyObject *tmp = PyTuple_GET_ITEM(pykey, 0);
198 if (tmp == Py_None) {
201 else if (PyUnicode_Check(tmp)) {
202 msgctxt = PyUnicode_AsUTF8(tmp);
208 tmp = PyTuple_GET_ITEM(pykey, 1);
209 if (PyUnicode_Check(tmp)) {
210 msgid = PyUnicode_AsUTF8(tmp);
218 printf(
"WARNING! In translations' dict of \"");
219 PyObject_Print(uuid, stdout, Py_PRINT_RAW);
222 " Keys must be tuples of (msgctxt [string or None], msgid [string]), "
223 "this one is not valid, skipping: ");
224 PyObject_Print(pykey, stdout, Py_PRINT_RAW);
228 if (PyUnicode_Check(trans) ==
false) {
229 printf(
"WARNING! In translations' dict of \"");
230 PyObject_Print(uuid, stdout, Py_PRINT_RAW);
232 printf(
" Values must be strings, this one is not valid, skipping: ");
233 PyObject_Print(trans, stdout, Py_PRINT_RAW);
239 if (!BPY_app_translations_py_pgettext(msgctxt, msgid).has_value()) {
243 Py_ssize_t trans_str_len;
244 const char *trans_str = PyUnicode_AsUTF8AndSize(trans, &trans_str_len);
245 get_translations_cache()->add(key, std::string(trans_str, trans_str_len));
252 MEM_SAFE_DELETE(language);
253 MEM_SAFE_DELETE(language_country);
254 MEM_SAFE_DELETE(language_variant);
257std::optional<StringRefNull> BPY_app_translations_py_pgettext(
const StringRef msgctxt,
260# define STATIC_LOCALE_SIZE 32
262 static char locale[STATIC_LOCALE_SIZE] =
"";
271 if (!
STREQ(tmp,
locale) || !get_translations_cache()) {
273 PyGILState_STATE _py_state = PyGILState_Ensure();
280 PyGILState_Release(_py_state);
288 const std::string *
result = get_translations_cache()->lookup_ptr_as(key);
294# undef STATIC_LOCALE_SIZE
301 app_translations_py_messages_register_doc,
302 ".. function:: register(module_name, translations_dict)\n"
304 " Registers an addon's UI translations.\n"
307 " Does nothing when Blender is built without internationalization support.\n"
309 " :param module_name: The name identifying the addon.\n"
310 " :type module_name: str\n"
311 " :param translations_dict: A dictionary built like that:\n"
312 " ``{locale: {msg_key: msg_translation, ...}, ...}``\n"
313 " :type translations_dict: dict[str, dict[tuple[str, str], str]]\n"
319#ifdef WITH_INTERNATIONAL
320 static const char *kwlist[] = {
"module_name",
"translations_dict",
nullptr};
321 PyObject *module_name, *uuid_dict;
323 if (!PyArg_ParseTupleAndKeywords(args,
327 ":bpy.app.translations.register",
337 if (PyDict_Contains(self->
py_messages, module_name)) {
340 "bpy.app.translations.register: translations message cache already contains some data for "
342 PyUnicode_AsUTF8(module_name));
346 PyDict_SetItem(self->
py_messages, module_name, uuid_dict);
349 _clear_translations_cache();
362 app_translations_py_messages_unregister_doc,
363 ".. function:: unregister(module_name)\n"
365 " Unregisters an addon's UI translations.\n"
368 " Does nothing when Blender is built without internationalization support.\n"
370 " :param module_name: The name identifying the addon.\n"
371 " :type module_name: str\n"
377#ifdef WITH_INTERNATIONAL
378 static const char *kwlist[] = {
"module_name",
nullptr};
379 PyObject *module_name;
381 if (!PyArg_ParseTupleAndKeywords(args,
384 ":bpy.app.translations.unregister",
392 if (PyDict_Contains(self->
py_messages, module_name)) {
395 _clear_translations_cache();
426 "bpy.app.translations.contexts",
427 "This named tuple contains all predefined translation contexts",
434 PyObject *translations_contexts;
439 if (translations_contexts ==
nullptr) {
443#define SetObjString(item) \
444 PyStructSequence_SET_ITEM(translations_contexts, pos++, PyUnicode_FromString(item))
445#define SetObjNone() PyStructSequence_SET_ITEM(translations_contexts, pos++, Py_NewRef(Py_None))
459 return translations_contexts;
470 app_translations_contexts_doc,
471 "A named tuple containing all predefined translation contexts.\n"
475 "\", it would be internally\n"
476 " assimilated as the default one!\n");
479 app_translations_contexts_C_to_py_doc,
480 "A readonly dict mapping contexts' C-identifiers to their py-identifiers.\n");
487 app_translations_contexts_doc},
492 app_translations_contexts_C_to_py_doc},
498 app_translations_locale_doc,
499 "The actual locale currently in use (will always return an empty string when Blender "
501 "internationalization support).\n"
512 app_translations_locales_doc,
513 "All locales currently known by Blender (i.e. available as translations).\n"
515 ":type: list[str]\n");
520 int num_locales = 0, pos = 0;
531 ret = PyTuple_New(num_locales);
536 PyTuple_SET_ITEM(
ret, pos++, PyUnicode_FromString(it->
identifier));
549 app_translations_locale_doc,
554 app_translations_locales_doc,
560#ifdef WITH_INTERNATIONAL
561# define PY_PGETTEXT_FORMAT \
566# define PY_PGETTEXT_FORMAT \
576 const char *(*_pgettext)(
const char *,
const char *))
578 static const char *kwlist[] = {
"msgid",
"msgctxt",
nullptr};
580#ifdef WITH_INTERNATIONAL
581 char *msgid, *msgctxt =
nullptr;
583 if (!PyArg_ParseTupleAndKeywords(args, kw, format, (
char **)kwlist, &msgid, &msgctxt)) {
589 PyObject *msgid, *msgctxt;
592 if (!PyArg_ParseTupleAndKeywords(
593 args, kw, format,
const_cast<char **
>(kwlist), &msgid, &msgctxt))
598 return Py_NewRef(msgid);
604 app_translations_pgettext_doc,
605 ".. function:: pgettext(msgid, msgctxt=None)\n"
607 " Try to translate the given msgid (with optional msgctxt).\n"
610 " The ``(msgid, msgctxt)`` parameters order has been switched compared to gettext "
611 "function, to allow\n"
612 " single-parameter calls (context then defaults to BLT_I18NCONTEXT_DEFAULT).\n"
615 " You should really rarely need to use this function in regular addon code, as all "
616 "translation should be\n"
617 " handled by Blender internal code. "
618 "The only exceptions are strings containing formatting "
619 "(like \"File: %r\"),\n"
620 " but you should rather use :func:`pgettext_iface`/:func:`pgettext_tip` in those cases!\n"
623 " Does nothing when Blender is built without internationalization support (hence always "
624 "returns ``msgid``).\n"
626 " :param msgid: The string to translate.\n"
627 " :type msgid: str\n"
628 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
629 " :type msgctxt: str | None\n"
630 " :return: The translated string (or msgid if no translation was found).\n"
642 app_translations_pgettext_n_doc,
643 ".. function:: pgettext_n(msgid, msgctxt=None)\n"
645 " Extract the given msgid to translation files. This is a no-op function that will "
646 "only mark the string to extract, but not perform the actual translation.\n"
649 " See :func:`pgettext` notes.\n"
651 " :param msgid: The string to extract.\n"
652 " :type msgid: str\n"
653 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
654 " :type msgctxt: str | None\n"
655 " :return: The original string.\n"
662 static const char *kwlist[] = {
"msgid",
"msgctxt",
nullptr};
663 PyObject *msgid, *msgctxt;
666 if (!PyArg_ParseTupleAndKeywords(args,
671 ":bpy.app.translations.pgettext_n",
672 const_cast<char **
>(kwlist),
679 return Py_NewRef(msgid);
684 app_translations_pgettext_iface_doc,
685 ".. function:: pgettext_iface(msgid, msgctxt=None)\n"
687 " Try to translate the given msgid (with optional msgctxt), if labels' translation "
691 " See :func:`pgettext` notes.\n"
693 " :param msgid: The string to translate.\n"
694 " :type msgid: str\n"
695 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
696 " :type msgctxt: str | None\n"
697 " :return: The translated string (or msgid if no translation was found).\n"
710 app_translations_pgettext_tip_doc,
711 ".. function:: pgettext_tip(msgid, msgctxt=None)\n"
713 " Try to translate the given msgid (with optional msgctxt), if tooltips' "
714 "translation is enabled.\n"
717 " See :func:`pgettext` notes.\n"
719 " :param msgid: The string to translate.\n"
720 " :type msgid: str\n"
721 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
722 " :type msgctxt: str | None\n"
723 " :return: The translated string (or msgid if no translation was found).\n"
736 app_translations_pgettext_rpt_doc,
737 ".. function:: pgettext_rpt(msgid, msgctxt=None)\n"
739 " Try to translate the given msgid (with optional msgctxt), if reports' translation "
743 " See :func:`pgettext` notes.\n"
745 " :param msgid: The string to translate.\n"
746 " :type msgid: str\n"
747 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
748 " :type msgctxt: str | None\n"
749 " :return: The translated string (or msgid if no translation was found).\n"
762 app_translations_pgettext_data_doc,
763 ".. function:: pgettext_data(msgid, msgctxt=None)\n"
765 " Try to translate the given msgid (with optional msgctxt), if new data name's "
766 "translation is enabled.\n"
769 " See :func:`pgettext` notes.\n"
771 " :param msgid: The string to translate.\n"
772 " :type msgid: str\n"
773 " :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).\n"
774 " :type msgctxt: str | None\n"
775 " :return: The translated string (or ``msgid`` if no translation was found).\n"
790 app_translations_locale_explode_doc,
791 ".. function:: locale_explode(locale)\n"
793 " Return all components and their combinations of the given ISO locale string.\n"
795 " >>> bpy.app.translations.locale_explode(\"sr_RS@latin\")\n"
796 " (\"sr\", \"RS\", \"latin\", \"sr_RS\", \"sr@latin\")\n"
798 " For non-complete locales, missing elements will be None.\n"
800 " :param locale: The ISO locale string to explode.\n"
801 " :type locale: str\n"
802 " :return: A tuple ``(language, country, variant, language_country, language@variant)``.\n"
803 " :rtype: tuple[str | None, str | None, str | None, str | None, str | None]\n"
810 static const char *kwlist[] = {
"locale",
nullptr};
812 char *language, *country, *variant, *language_country, *language_variant;
814 if (!PyArg_ParseTupleAndKeywords(args,
817 ":bpy.app.translations.locale_explode",
818 const_cast<char **
>(kwlist),
825 locale, &language, &country, &variant, &language_country, &language_variant);
827 ret_tuple = Py_BuildValue(
828 "sssss", language, country, variant, language_country, language_variant);
830 MEM_SAFE_DELETE(language);
831 MEM_SAFE_DELETE(country);
832 MEM_SAFE_DELETE(variant);
833 MEM_SAFE_DELETE(language_country);
834 MEM_SAFE_DELETE(language_variant);
841# pragma clang diagnostic push
842# pragma clang diagnostic ignored "-Wcast-function-type"
844# pragma GCC diagnostic push
845# pragma GCC diagnostic ignored "-Wcast-function-type"
853 METH_VARARGS | METH_KEYWORDS,
854 app_translations_py_messages_register_doc},
857 METH_VARARGS | METH_KEYWORDS,
858 app_translations_py_messages_unregister_doc},
861 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
862 app_translations_pgettext_doc},
865 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
866 app_translations_pgettext_n_doc},
869 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
870 app_translations_pgettext_iface_doc},
873 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
874 app_translations_pgettext_tip_doc},
877 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
878 app_translations_pgettext_rpt_doc},
881 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
882 app_translations_pgettext_data_doc},
885 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
886 app_translations_locale_explode_doc},
892# pragma clang diagnostic pop
894# pragma GCC diagnostic pop
903 BLI_assert(PyTuple_GET_SIZE(args) == 0 && kw ==
nullptr);
916 PyObject *val = PyUnicode_FromString(ctxt->
py_id);
917 PyDict_SetItemString(py_ctxts, ctxt->
c_id, val);
939#ifdef WITH_INTERNATIONAL
940 _clear_translations_cache();
946 app_translations_doc,
947 "This object contains some data/methods regarding internationalization in Blender, "
948 "and allows every py script\n"
949 "to feature translations for its own UI messages.\n"
952 PyVarObject_HEAD_INIT(
nullptr, 0)
953 "bpy_app_translations",
972 app_translations_doc,
1010 PyStructSequence_Field *desc;
1016 desc->name = ctxt->
py_id;
1017 desc->doc =
nullptr;
1019 desc->name = desc->doc =
nullptr;
1042#ifdef WITH_INTERNATIONAL
1043 _clear_translations_cache();
#define STRNCPY_UTF8(dst, src)
#define BLT_I18NCONTEXTS_DESC
#define BLT_I18NCONTEXT_DEFAULT
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
#define SetObjString(item)
#define PY_PGETTEXT_FORMAT
unsigned long long int uint64_t
bool operator==(const Error &left, const Error &right)
static PyTypeObject BlenderAppTranslationsType
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
static BLT_i18n_contexts_descriptor _contexts[]
static PyObject * app_translations_pgettext_rpt(BlenderAppTranslations *, PyObject *args, PyObject *kw)
static PyObject * app_translations_locales_get(PyObject *, void *)
static PyObject * app_translations_pgettext_n(BlenderAppTranslations *, PyObject *args, PyObject *kw)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyStructSequence_Field app_translations_contexts_fields[ARRAY_SIZE(_contexts)]
static PyObject * _py_pgettext(PyObject *args, PyObject *kw, const char *format, const char *(*_pgettext)(const char *, const char *))
static BlenderAppTranslations * _translations
bool BLT_is_default_context(StringRef msgctxt)
static PyObject * app_translations_pgettext_data(BlenderAppTranslations *, PyObject *args, PyObject *kw)
static void app_translations_free(void *self_v)
static PyObject * app_translations_new(PyTypeObject *type, PyObject *args, PyObject *kw)
const char * BLT_pgettext(const char *msgctxt, const char *msgid)
const char * BLT_translate_do_report(const char *msgctxt, const char *msgid)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyObject * app_translations_pgettext_tip(BlenderAppTranslations *, PyObject *args, PyObject *kw)
static PyObject * app_translations_pgettext(BlenderAppTranslations *, PyObject *args, PyObject *kw)
void BPY_app_translations_end()
static PyMemberDef app_translations_members[]
static PyObject * app_translations_py_messages_register(BlenderAppTranslations *self, PyObject *args, PyObject *kw)
static PyTypeObject BlenderAppTranslationsContextsType
const char * BLT_translate_do_iface(const char *msgctxt, const char *msgid)
static PyObject * app_translations_py_messages_unregister(BlenderAppTranslations *self, PyObject *args, PyObject *kw)
static PyMethodDef app_translations_methods[]
static PyObject * app_translations_pgettext_iface(BlenderAppTranslations *, PyObject *args, PyObject *kw)
PyObject * BPY_app_translations_struct()
void BLT_lang_locale_explode(const char *locale, char **language, char **country, char **variant, char **language_country, char **language_variant)
static PyObject * app_translations_locale_get(PyObject *, void *)
static PyObject * app_translations_locale_explode(BlenderAppTranslations *, PyObject *args, PyObject *kw)
static PyStructSequence_Desc app_translations_contexts_desc
const char * BLT_lang_get()
static PyGetSetDef app_translations_getseters[]
const char * BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
const EnumPropertyItem * BLT_lang_RNA_enum_properties()
const char * BLT_translate_do_tooltip(const char *msgctxt, const char *msgid)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
static PyObject * app_translations_contexts_make()
header-only compatibility defines.
PyObject * contexts_C_to_py
PyObject_HEAD const char * context_separator