diff --git a/intern/locale/boost_locale_wrapper.cpp b/intern/locale/boost_locale_wrapper.cpp index 80e75298d70..ebd2dd652c7 100644 --- a/intern/locale/boost_locale_wrapper.cpp +++ b/intern/locale/boost_locale_wrapper.cpp @@ -33,6 +33,7 @@ static std::string messages_path; static std::string default_domain; +static std::string locale_str; void bl_locale_init(const char *_messages_path, const char *_default_domain) { @@ -52,6 +53,7 @@ void bl_locale_init(const char *_messages_path, const char *_default_domain) void bl_locale_set(const char *locale) { boost::locale::generator gen; + std::locale _locale; // Specify location of dictionaries. gen.add_messages_path(messages_path); gen.add_messages_domain(default_domain); @@ -59,7 +61,8 @@ void bl_locale_set(const char *locale) try { if (locale && locale[0]) { - std::locale::global(gen(locale)); + _locale = gen(locale); + std::locale::global(_locale); } else { #ifdef __APPLE__ @@ -85,9 +88,11 @@ void bl_locale_set(const char *locale) if (locale_osx == "") fprintf(stderr, "Locale set: failed to read AppleLocale read from defaults\n"); - std::locale::global(gen(locale_osx.c_str())); + _locale = gen(locale_osx.c_str()); + std::locale::global(_locale); #else - std::locale::global(gen("")); + _locale = gen(""); + std::locale::global(_locale); #endif } // Note: boost always uses "C" LC_NUMERIC by default! @@ -95,6 +100,22 @@ void bl_locale_set(const char *locale) catch(std::exception const &e) { std::cout << "bl_locale_set(" << locale << "): " << e.what() << " \n"; } + + /* Generate the locale string (useful to know which locale we are actually using in case of "default" one). */ +#define LOCALE_INFO std::use_facet(_locale) + + locale_str = LOCALE_INFO.language(); + if (LOCALE_INFO.country() != "") { + locale_str += "_" + LOCALE_INFO.country(); + } + if (LOCALE_INFO.variant() != "") { + locale_str += "@" + LOCALE_INFO.variant(); + } +} + +const char *bl_locale_get(void) +{ + return locale_str.c_str(); } const char *bl_locale_pgettext(const char *msgctxt, const char *msgid) @@ -115,5 +136,4 @@ const char *bl_locale_pgettext(const char *msgctxt, const char *msgid) // std::cout << "bl_locale_pgettext(" << msgctxt << ", " << msgid << "): " << e.what() << " \n"; return msgid; } -} - +} \ No newline at end of file diff --git a/intern/locale/boost_locale_wrapper.h b/intern/locale/boost_locale_wrapper.h index e7956d216f1..4e3a1f848d2 100644 --- a/intern/locale/boost_locale_wrapper.h +++ b/intern/locale/boost_locale_wrapper.h @@ -40,7 +40,8 @@ extern "C" { void bl_locale_init(const char *messages_path, const char *default_domain); void bl_locale_set(const char *locale); -const char* bl_locale_pgettext(const char *msgctxt, const char *msgid); +const char *bl_locale_get(void); +const char *bl_locale_pgettext(const char *msgctxt, const char *msgid); #ifdef __cplusplus } diff --git a/source/blender/blenfont/BLF_translation.h b/source/blender/blenfont/BLF_translation.h index 0832907d1d9..59f66cae727 100644 --- a/source/blender/blenfont/BLF_translation.h +++ b/source/blender/blenfont/BLF_translation.h @@ -51,6 +51,13 @@ void BLF_lang_set(const char *); /* Get the current locale (short code, e.g. es_ES). */ const char *BLF_lang_get(void); +/* Get locale's elements (if relevant pointer is not NULL and element actually exists, e.g. if there is no variant, + * *variant and *language_variant will always be NULL). + * Non-null elements are always MEM_mallocN'ed, it's the caller's responsibility to free them. + */ +void BLF_locale_explode(const char *locale, char **language, char **country, char **variant, + char **language_country, char **language_variant); + /* Get EnumPropertyItem's for translations menu. */ struct EnumPropertyItem *BLF_RNA_lang_enum_properties(void); @@ -143,4 +150,54 @@ const char *BLF_translate_do_tooltip(const char *msgctxt, const char *msgid); #define BLF_I18NCONTEXT_ID_MOVIECLIP "MovieClip" #define BLF_I18NCONTEXT_ID_MASK "Mask" -#endif /* __BLF_TRANSLATION_H__ */ +/* Helper for bpy.app.i18n object... */ +typedef struct +{ + const char *c_id; + const char *py_id; + const char *value; +} BLF_i18n_contexts_descriptor; + +#define BLF_I18NCONTEXTS_ITEM(ctxt_id, py_id) {#ctxt_id, py_id, ctxt_id} + +#define BLF_I18NCONTEXTS_DESC { \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_DEFAULT, "default"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "operator_default"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_ACTION, "id_action"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_ARMATURE, "id_armature"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_BRUSH, "id_brush"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_CAMERA, "id_camera"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_CURVE, "id_curve"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_GPENCIL, "id_gpencil"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_GROUP, "id_group"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_ID, "id_id"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_IMAGE, "id_image"), \ + /*BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_IPO, "id_ipo"),*/ \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SHAPEKEY, "id_shapekey"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_LAMP, "id_lamp"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_LIBRARY, "id_library"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_LATTICE, "id_lattice"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_MATERIAL, "id_material"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_METABALL, "id_metaball"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_MESH, "id_mesh"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_NODETREE, "id_nodetree"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_OBJECT, "id_object"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_PARTICLESETTINGS, "id_particlesettings"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SCENE, "id_scene"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SCREEN, "id_screen"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SEQUENCE, "id_sequence"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SPEAKER, "id_speaker"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_SOUND, "id_sound"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_TEXTURE, "id_texture"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_TEXT, "id_text"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_VFONT, "id_vfont"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_WORLD, "id_world"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_WINDOWMANAGER, "id_windowmanager"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_MOVIECLIP, "id_movieclip"), \ + BLF_I18NCONTEXTS_ITEM(BLF_I18NCONTEXT_ID_MASK, "id_mask"), \ + {NULL, NULL, NULL} \ +} + +//#undef _BLF_I18NCONTEXTS_ITEM + +#endif /* __BLF_TRANSLATION_H__ */ \ No newline at end of file diff --git a/source/blender/blenfont/CMakeLists.txt b/source/blender/blenfont/CMakeLists.txt index 90baef14a74..7bb80c34323 100644 --- a/source/blender/blenfont/CMakeLists.txt +++ b/source/blender/blenfont/CMakeLists.txt @@ -29,6 +29,7 @@ set(INC ../editors/include ../makesdna ../makesrna + ../python ../imbuf ../../../intern/guardedalloc ../../../intern/locale diff --git a/source/blender/blenfont/SConscript b/source/blender/blenfont/SConscript index a6ea724a51f..d1ef181f75c 100644 --- a/source/blender/blenfont/SConscript +++ b/source/blender/blenfont/SConscript @@ -31,7 +31,7 @@ Import ('env') sources = env.Glob('intern/*.c') incs = '. intern #/intern/guardedalloc #/intern/locale ../blenkernel ../blenlib ../blenloader' -incs += ' ../makesdna ../makesrna ../imbuf ../editors/include' +incs += ' ../makesdna ../makesrna ../python ../imbuf ../editors/include' incs += ' #/extern/glew/include' incs += ' ' + env['BF_FREETYPE_INC'] diff --git a/source/blender/blenfont/intern/blf_lang.c b/source/blender/blenfont/intern/blf_lang.c index 9086799f984..ab29fae1a58 100644 --- a/source/blender/blenfont/intern/blf_lang.c +++ b/source/blender/blenfont/intern/blf_lang.c @@ -60,7 +60,7 @@ static EnumPropertyItem *locales_menu = NULL; static int num_locales_menu = 0; #define ULANGUAGE ((U.language >= 0 && U.language < num_locales) ? U.language : 0) -#define LOCALE(_id) (locales ? locales[_id] : "") +#define LOCALE(_id) (locales ? locales[(_id)] : "") static void free_locales(void) { @@ -113,7 +113,7 @@ static void fill_locales(void) } num_locales_menu++; /* The "closing" void item... */ - /* And now, buil locales and locale_menu! */ + /* And now, build locales and locale_menu! */ locales_menu = MEM_callocN(num_locales_menu * sizeof(EnumPropertyItem), __func__); line = lines; /* Do not allocate locales with zero-sized mem, as LOCALE macro uses NULL locales as invalid marker! */ @@ -234,10 +234,15 @@ void BLF_lang_set(const char *str) } } +/* Get the current locale (short code, e.g. es_ES). */ const char *BLF_lang_get(void) { - int uilang = ULANGUAGE; - return LOCALE(uilang); + const char *locale = LOCALE(ULANGUAGE); + if (locale[0] == '\0') { + /* Default locale, we have to find which one we are actually using! */ + locale = bl_locale_get(); + } + return locale; } #undef LOCALE @@ -245,6 +250,11 @@ const char *BLF_lang_get(void) #else /* ! WITH_INTERNATIONAL */ +EnumPropertyItem *BLF_RNA_lang_enum_properties(void) +{ + return NULL; +} + void BLF_lang_init(void) { return; @@ -266,3 +276,56 @@ const char *BLF_lang_get(void) } #endif /* WITH_INTERNATIONAL */ + + +/* Get locale's elements (if relevant pointer is not NULL and element actually exists, e.g. if there is no variant, + * *variant and *language_variant will always be NULL). + * Non-null elements are always MEM_mallocN'ed, it's the caller's responsibility to free them. + * NOTE: Keep that one always available, you never know, may become useful even in no-WITH_INTERNATIONAL context... + */ +void BLF_locale_explode(const char *locale, char **language, char **country, char **variant, + char **language_country, char **language_variant) +{ + char *m1, *m2, *_t = NULL; + + m1 = strchr(locale, '_'); + m2 = strchr(locale, '@'); + + if (language || language_variant) { + if (m1 || m2) { + _t = m1 ? BLI_strdupn(locale, m1 - locale) : BLI_strdupn(locale, m2 - locale); + if (language) + *language = _t; + } + else if (language) { + *language = NULL; + } + } + if (country) { + if (m1) + *country = m2 ? BLI_strdupn(m1 + 1, m2 - (m1 + 1)) : BLI_strdup(m1 + 1); + else + *country = NULL; + } + if (variant) { + if (m2) + *variant = BLI_strdup(m2 + 1); + else + *variant = NULL; + } + if (language_country) { + if (m2) + *language_country = BLI_strdupn(locale, m2 - locale); + else + *language_country = NULL; + } + if (language_variant) { + if (m2) + *language_variant = m1 ? BLI_strdupcat(_t, m2 + 1) : BLI_strdup(locale); + else + *language_variant = NULL; + } + if (_t && !language) { + MEM_freeN(_t); + } +} \ No newline at end of file diff --git a/source/blender/blenfont/intern/blf_translation.c b/source/blender/blenfont/intern/blf_translation.c index 5d4b631688a..3aaa61d55f2 100644 --- a/source/blender/blenfont/intern/blf_translation.c +++ b/source/blender/blenfont/intern/blf_translation.c @@ -47,6 +47,8 @@ #include "DNA_userdef_types.h" /* For user settings. */ +#include "BPY_extern.h" + static const char unifont_filename[] = "droidsans.ttf.gz"; static unsigned char *unifont_ttf = NULL; static int unifont_size = 0; @@ -84,7 +86,15 @@ const char *BLF_pgettext(const char *msgctxt, const char *msgid) { #ifdef WITH_INTERNATIONAL if (msgid && msgid[0]) { - return bl_locale_pgettext(msgctxt, msgid); + const char *ret = bl_locale_pgettext(msgctxt, msgid); + /* We assume if the returned string is the same (memory level) as the msgid, no translation was found, + * and we can try py scripts' ones! + */ + if (ret == msgid) { + ret = BPY_app_translations_py_pgettext(msgctxt, msgid); + } + + return ret; } return ""; #else diff --git a/source/blender/makesrna/RNA_define.h b/source/blender/makesrna/RNA_define.h index 463e0e04679..cd6d74c3488 100644 --- a/source/blender/makesrna/RNA_define.h +++ b/source/blender/makesrna/RNA_define.h @@ -87,7 +87,7 @@ PropertyRNA *RNA_def_string(StructOrFunctionRNA *cont, const char *identifier, c PropertyRNA *RNA_def_string_file_path(StructOrFunctionRNA *cont, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description); PropertyRNA *RNA_def_string_dir_path(StructOrFunctionRNA *cont, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description); PropertyRNA *RNA_def_string_file_name(StructOrFunctionRNA *cont, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description); -PropertyRNA *RNA_def_string_translate(StructOrFunctionRNA *cont, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description); +PropertyRNA *RNA_def_string_py_translate(StructOrFunctionRNA *cont, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description); PropertyRNA *RNA_def_enum(StructOrFunctionRNA *cont, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description); PropertyRNA *RNA_def_enum_flag(StructOrFunctionRNA *cont, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description); diff --git a/source/blender/makesrna/RNA_types.h b/source/blender/makesrna/RNA_types.h index c76f9824c73..5f34fad09c6 100644 --- a/source/blender/makesrna/RNA_types.h +++ b/source/blender/makesrna/RNA_types.h @@ -110,10 +110,9 @@ typedef enum PropertySubType { PROP_FILEPATH = 1, PROP_DIRPATH = 2, PROP_FILENAME = 3, - PROP_BYTESTRING = 4, /* a string which should be represented as bytes - * in python, still NULL terminated though. */ - PROP_TRANSLATE = 5, /* a string which should be translated */ - PROP_PASSWORD = 6, /* a string which should not be displayed in UI */ + PROP_BYTESTRING = 4, /* a string which should be represented as bytes in python, still NULL terminated though. */ + /* 5 was used by "PROP_TRANSLATE" sub-type, which is now a flag. */ + PROP_PASSWORD = 6, /* a string which should not be displayed in UI */ /* numbers */ PROP_UNSIGNED = 13, @@ -144,6 +143,7 @@ typedef enum PropertySubType { } PropertySubType; /* Make sure enums are updated with thses */ +/* HIGHEST FLAG IN USE: 1 << 29 */ typedef enum PropertyFlag { /* editable means the property is editable in the user * interface, properties are editable by default except @@ -200,6 +200,11 @@ typedef enum PropertyFlag { */ PROP_ENUM_FLAG = (1 << 21), + /* A string which should be translated when converting from py string to RNA prop. + * Should only be used in some functions' properties (currently only "text" one of funcs in UI API). + */ + PROP_STRING_PY_TRANSLATE = (1 << 28), + /* need context for update function */ PROP_CONTEXT_UPDATE = (1 << 22), PROP_CONTEXT_PROPERTY_UPDATE = (1 << 22) | (1 << 27), diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c index 4b512b650a5..605fb18d5aa 100644 --- a/source/blender/makesrna/intern/makesrna.c +++ b/source/blender/makesrna/intern/makesrna.c @@ -2444,7 +2444,6 @@ static const char *rna_property_subtypename(PropertySubType type) case PROP_FILENAME: return "PROP_FILENAME"; case PROP_DIRPATH: return "PROP_DIRPATH"; case PROP_BYTESTRING: return "PROP_BYTESTRING"; - case PROP_TRANSLATE: return "PROP_TRANSLATE"; case PROP_UNSIGNED: return "PROP_UNSIGNED"; case PROP_PERCENTAGE: return "PROP_PERCENTAGE"; case PROP_FACTOR: return "PROP_FACTOR"; diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c index b94d7eb691f..ac307139860 100644 --- a/source/blender/makesrna/intern/rna_define.c +++ b/source/blender/makesrna/intern/rna_define.c @@ -2534,13 +2534,14 @@ PropertyRNA *RNA_def_string_file_name(StructOrFunctionRNA *cont_, const char *id return prop; } -PropertyRNA *RNA_def_string_translate(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, - int maxlen, const char *ui_name, const char *ui_description) +PropertyRNA *RNA_def_string_py_translate(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, + int maxlen, const char *ui_name, const char *ui_description) { ContainerRNA *cont = cont_; PropertyRNA *prop; - prop = RNA_def_property(cont, identifier, PROP_STRING, PROP_TRANSLATE); + prop = RNA_def_property(cont, identifier, PROP_STRING, PROP_NONE); + RNA_def_property_flag(prop, PROP_STRING_PY_TRANSLATE); if (maxlen != 0) RNA_def_property_string_maxlength(prop, maxlen); if (default_value) RNA_def_property_string_default(prop, default_value); RNA_def_property_ui_text(prop, ui_name, ui_description); diff --git a/source/blender/makesrna/intern/rna_rna.c b/source/blender/makesrna/intern/rna_rna.c index f86f6010e27..b5b3897ed46 100644 --- a/source/blender/makesrna/intern/rna_rna.c +++ b/source/blender/makesrna/intern/rna_rna.c @@ -46,6 +46,9 @@ EnumPropertyItem property_type_items[] = { {0, NULL, 0, NULL, NULL} }; +/* XXX Keep in sync with bpy_props.c's property_subtype_xxx_items ??? + * Currently it is not... + */ EnumPropertyItem property_subtype_items[] = { {PROP_NONE, "NONE", 0, "None", ""}, @@ -53,7 +56,7 @@ EnumPropertyItem property_subtype_items[] = { {PROP_FILEPATH, "FILEPATH", 0, "File Path", ""}, {PROP_DIRPATH, "DIRPATH", 0, "Directory Path", ""}, {PROP_FILENAME, "FILENAME", 0, "File Name", ""}, - {PROP_TRANSLATE, "TRANSLATE", 0, "Translate", ""}, + {PROP_PASSWORD, "PASSWORD", 0, "Password", "A string that is displayed hidden ('********')"}, /* numbers */ {PROP_UNSIGNED, "UNSIGNED", 0, "Unsigned", ""}, diff --git a/source/blender/makesrna/intern/rna_ui.c b/source/blender/makesrna/intern/rna_ui.c index 00124a82dd1..fad93ea9054 100644 --- a/source/blender/makesrna/intern/rna_ui.c +++ b/source/blender/makesrna/intern/rna_ui.c @@ -813,9 +813,6 @@ static void rna_def_panel(BlenderRNA *brna) "class name is \"OBJECT_PT_hello\", and bl_idname is not set by the " "script, then bl_idname = \"OBJECT_PT_hello\""); - /* panel's label indeed doesn't need PROP_TRANSLATE flag: translation of label happens in runtime - * when drawing panel and having this flag set will make runtime switching of language much more tricky - * because label will be stored translated */ prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE); RNA_def_property_string_sdna(prop, NULL, "type->label"); RNA_def_property_flag(prop, PROP_REGISTER); @@ -992,9 +989,6 @@ static void rna_def_menu(BlenderRNA *brna) "class name is \"OBJECT_MT_hello\", and bl_idname is not set by the " "script, then bl_idname = \"OBJECT_MT_hello\")"); - /* menu's label indeed doesn't need PROP_TRANSLATE flag: translation of label happens in runtime - * when drawing panel and having this flag set will make runtime switching of language much more tricky - * because label will be stored translated */ prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE); RNA_def_property_string_sdna(prop, NULL, "type->label"); RNA_def_property_flag(prop, PROP_REGISTER); diff --git a/source/blender/makesrna/intern/rna_ui_api.c b/source/blender/makesrna/intern/rna_ui_api.c index 366d0dc1fd9..93410383a58 100644 --- a/source/blender/makesrna/intern/rna_ui_api.c +++ b/source/blender/makesrna/intern/rna_ui_api.c @@ -196,7 +196,7 @@ static void api_ui_item_common(FunctionRNA *func) { PropertyRNA *prop; - RNA_def_string_translate(func, "text", "", 0, "", "Override automatic text of the item"); + RNA_def_string_py_translate(func, "text", "", 0, "", "Override automatic text of the item"); prop = RNA_def_property(func, "icon", PROP_ENUM, PROP_NONE); RNA_def_property_enum_items(prop, icon_items); @@ -466,7 +466,7 @@ void RNA_api_ui_layout(StructRNA *srna) parm = RNA_def_string(func, "type_property", "", 0, "", "Identifier of property in data giving the type of the ID-blocks to use"); RNA_def_property_flag(parm, PROP_REQUIRED); - RNA_def_string_translate(func, "text", "", 0, "", "Custom label to display in UI"); + RNA_def_string_py_translate(func, "text", "", 0, "", "Custom label to display in UI"); func = RNA_def_function(srna, "template_path_builder", "uiTemplatePathBuilder"); parm = RNA_def_pointer(func, "data", "AnyType", "", "Data from which to take property"); @@ -475,7 +475,7 @@ void RNA_api_ui_layout(StructRNA *srna) RNA_def_property_flag(parm, PROP_REQUIRED); parm = RNA_def_pointer(func, "root", "ID", "", "ID-block from which path is evaluated from"); RNA_def_property_flag(parm, PROP_REQUIRED | PROP_RNAPTR); - RNA_def_string_translate(func, "text", "", 0, "", "Custom label to display in UI"); + RNA_def_string_py_translate(func, "text", "", 0, "", "Custom label to display in UI"); func = RNA_def_function(srna, "template_modifier", "uiTemplateModifier"); RNA_def_function_flag(func, FUNC_USE_CONTEXT); diff --git a/source/blender/makesrna/intern/rna_wm.c b/source/blender/makesrna/intern/rna_wm.c index f416d3403ad..c0fb2a7238f 100644 --- a/source/blender/makesrna/intern/rna_wm.c +++ b/source/blender/makesrna/intern/rna_wm.c @@ -1330,9 +1330,6 @@ static void rna_def_operator(BlenderRNA *brna) RNA_def_property_flag(prop, PROP_REGISTER | PROP_NEVER_CLAMP); RNA_def_struct_name_property(srna, prop); - /* operator's label indeed doesn't need PROP_TRANSLATE flag: translation of label happens in runtime - * when drawing panel and having this flag set will make runtime switching of language much more tricky - * because label will be stored translated */ prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE); RNA_def_property_string_sdna(prop, NULL, "type->name"); RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */ @@ -1397,9 +1394,6 @@ static void rna_def_macro_operator(BlenderRNA *brna) RNA_def_property_flag(prop, PROP_REGISTER | PROP_NEVER_CLAMP); RNA_def_struct_name_property(srna, prop); - /* menu's label indeed doesn't need PROP_TRANSLATE flag: translation of label happens in runtime - * when drawing panel and having this flag set will make runtime switching of language much more tricky - * because label will be stored translated */ prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE); RNA_def_property_string_sdna(prop, NULL, "type->name"); RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */ diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 13cb11d1301..0f5be095e0f 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -87,6 +87,11 @@ void BPY_context_update(struct bContext *C); void BPY_id_release(struct ID *id); +/* I18n for addons */ +#ifdef WITH_INTERNATIONAL +const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *msgid); +#endif + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt index 575185ece2d..b531e08f8d4 100644 --- a/source/blender/python/intern/CMakeLists.txt +++ b/source/blender/python/intern/CMakeLists.txt @@ -49,6 +49,7 @@ set(SRC bpy_app_ffmpeg.c bpy_app_build_options.c bpy_app_handlers.c + bpy_app_translations.c bpy_driver.c bpy_interface.c bpy_interface_atexit.c @@ -72,6 +73,7 @@ set(SRC bpy_app_ffmpeg.h bpy_app_build_options.h bpy_app_handlers.h + bpy_app_translations.h bpy_driver.h bpy_intern_string.h bpy_library.h diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c index 3e2353c3571..9ffee3ec1f5 100644 --- a/source/blender/python/intern/bpy_app.c +++ b/source/blender/python/intern/bpy_app.c @@ -36,6 +36,10 @@ #include "bpy_app_ffmpeg.h" #include "bpy_app_build_options.h" +#ifdef WITH_INTERNATIONAL +# include "bpy_app_translations.h" +#endif + #include "bpy_app_handlers.h" #include "bpy_driver.h" @@ -86,7 +90,10 @@ static PyStructSequence_Field app_info_fields[] = { {(char *)"ffmpeg", (char *)"FFmpeg library information backend"}, {(char *)"build_options", (char *)"A set containing most important enabled optional build features"}, {(char *)"handlers", (char *)"Application handler callbacks"}, - {NULL} +#ifdef WITH_INTERNATIONAL + {(char *)"translations", (char *)"Application and addons internationalization API"}, +#endif + {NULL}, }; static PyStructSequence_Desc app_info_desc = { @@ -152,6 +159,9 @@ static PyObject *make_app_info(void) SetObjItem(BPY_app_ffmpeg_struct()); SetObjItem(BPY_app_build_options_struct()); SetObjItem(BPY_app_handlers_struct()); +#ifdef WITH_INTERNATIONAL + SetObjItem(BPY_app_translations_struct()); +#endif #undef SetIntItem #undef SetStrItem diff --git a/source/blender/python/intern/bpy_app_build_options.c b/source/blender/python/intern/bpy_app_build_options.c index 0036b377d3c..6db0a52bdf0 100644 --- a/source/blender/python/intern/bpy_app_build_options.c +++ b/source/blender/python/intern/bpy_app_build_options.c @@ -32,7 +32,7 @@ static PyTypeObject BlenderAppBuildOptionsType; static PyStructSequence_Field app_builtopts_info_fields[] = { - /* names mostly follow CMake options, lowecases, after WITH_ */ + /* names mostly follow CMake options, lowercase, after WITH_ */ {(char *)"bullet", NULL}, {(char *)"codec_avi", NULL}, {(char *)"codec_ffmpeg", NULL}, @@ -71,7 +71,7 @@ static PyStructSequence_Field app_builtopts_info_fields[] = { static PyStructSequence_Desc app_builtopts_info_desc = { (char *)"bpy.app.build_options", /* name */ - (char *)"This module contains information about FFmpeg blender is linked against", /* doc */ + (char *)"This module contains information about options blender is built with", /* doc */ app_builtopts_info_fields, /* fields */ (sizeof(app_builtopts_info_fields) / sizeof(PyStructSequence_Field)) - 1 }; diff --git a/source/blender/python/intern/bpy_app_translations.c b/source/blender/python/intern/bpy_app_translations.c new file mode 100644 index 00000000000..b501ed2a226 --- /dev/null +++ b/source/blender/python/intern/bpy_app_translations.c @@ -0,0 +1,638 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Bastien Montagne + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/python/intern/bpy_app_translations.c + * \ingroup pythonintern + * + * This file defines a singleton py object accessed via 'bpy.app.translations', + * which exposes various data and functions useful in i18n work. + * Most notably, it allows to extend main translations with py dicts. + */ + +#include +/* XXX Why bloody hell isn't that included in Python.h???? */ +#include + +/* Need this one before BPY_extern.h, for bool def! */ +#include "BLI_utildefines.h" + +#include "BPY_extern.h" +#include "bpy_app_translations.h" + +#ifdef WITH_INTERNATIONAL + +#include "MEM_guardedalloc.h" + +#include "BLI_string.h" +#include "BLI_ghash.h" + +#include "BLF_translation.h" + +#include "RNA_types.h" +#include "RNA_access.h" + + +typedef struct +{ + PyObject_HEAD + /* The string used to separate context from actual message in PY_TRANSLATE RNA props. */ + const char *context_separator; + /* A "named tuple" (StructSequence actually...) containing all C-defined contexts. */ + PyObject *contexts; + /* A readonly mapping {C context id: python id} (actually, a MappingProxy). */ + PyObject *contexts_C_to_py; + /* A py dict containing all registered py dicts (order is more or less random, first match wins!). */ + PyObject *py_messages; +} BlenderAppTranslations; + +/* Our singleton instance pointer */ +static BlenderAppTranslations *_translations = NULL; + +/***** Helpers for ghash *****/ +typedef struct GHashKey { + const char *msgctxt; + const char *msgid; +} GHashKey; + +static GHashKey *_ghashutil_keyalloc(const void *msgctxt, const void *msgid) +{ + GHashKey *key = MEM_mallocN(sizeof(GHashKey), "GHashPair"); + key->msgctxt = BLI_strdup(msgctxt); + key->msgid = BLI_strdup(msgid); + return key; +} + +static unsigned int _ghashutil_keyhash(const void *ptr) +{ + const GHashKey *key = ptr; + unsigned int hash = BLI_ghashutil_strhash(key->msgctxt); + return hash ^ BLI_ghashutil_strhash(key->msgid); +} + +static int _ghashutil_keycmp(const void *a, const void *b) +{ + const GHashKey *A = a; + const GHashKey *B = b; + + /* Note: comparing msgid first, most of the time it will be enough! */ + int cmp = BLI_ghashutil_strcmp(A->msgid, B->msgid); + if (cmp == 0) + return BLI_ghashutil_strcmp(A->msgctxt, B->msgctxt); + return cmp; +} + +static void _ghashutil_keyfree(void *ptr) +{ + const GHashKey *key = ptr; + + /* We assume both msgctxt and msgid were BLI_strdup'ed! */ + MEM_freeN((void *)key->msgctxt); + MEM_freeN((void *)key->msgid); + MEM_freeN((void *)key); +} + +static void _ghashutil_valfree(void *ptr) +{ + MEM_freeN(ptr); +} + +/***** Python's messages cache *****/ + +/* We cache all messages available for a given locale from all py dicts into a single ghash. + * Changing of locale is not so common, while looking for a message translation is, so let's try to optimize + * the later as much as we can! + * Note changing of locale, as well as (un)registering a message dict, invalidate that cache. + */ +static GHash *_translations_cache = NULL; + +static void _clear_translations_cache(void) +{ + if (_translations_cache) { + BLI_ghash_free(_translations_cache, _ghashutil_keyfree, _ghashutil_valfree); + } + _translations_cache = NULL; +} + +static void _build_translations_cache(PyObject *py_messages, const char *locale) +{ + PyObject *uuid_dict; + Py_ssize_t pos = 0; + char *language = NULL, *language_country = NULL, *language_variant = NULL; + + /* For each py dict, we'll search for full locale, then language+country, then language+variant, + * then only language keys... */ + BLF_locale_explode(locale, &language, NULL, NULL, &language_country, &language_variant); + + /* Clear the cached ghash if needed, and create a new one. */ + _clear_translations_cache(); + _translations_cache = BLI_ghash_new(_ghashutil_keyhash, _ghashutil_keycmp, __func__); + + /* Iterate over all py dicts. */ + while(PyDict_Next(py_messages, &pos, NULL, &uuid_dict)) { + PyObject *lang_dict; + +#if 0 + PyObject_Print(uuid_dict, stdout, 0); + printf("\n"); +#endif + + if (!PyDict_Check(uuid_dict)) + continue; + + /* Try to get first complete locale, then language+country, then language+variant, then only language */ + lang_dict = PyDict_GetItemString(uuid_dict, locale); + if (!lang_dict && language_country) + lang_dict = PyDict_GetItemString(uuid_dict, language_country); + if (!lang_dict && language_variant) + lang_dict = PyDict_GetItemString(uuid_dict, language_variant); + if (!lang_dict && language) + lang_dict = PyDict_GetItemString(uuid_dict, language); + + if (lang_dict && PyDict_Check(lang_dict)) { + PyObject *pykey, *trans; + Py_ssize_t ppos = 0; + + /* Iterate over all translations of the found language dict, and populate our ghash cache. */ + while(PyDict_Next(lang_dict, &ppos, &pykey, &trans)) { + GHashKey *key; + PyObject *tmp; + const char *msgctxt = NULL, *msgid = NULL; + + tmp = PyTuple_GetItem(pykey, 0); + if (tmp && PyUnicode_Check(tmp) && !PyUnicode_READY(tmp)) { + msgctxt = (const char *)PyUnicode_AsUTF8(tmp); + } + tmp = PyTuple_GetItem(pykey, 1); + if (tmp && PyUnicode_Check(tmp) && !PyUnicode_READY(tmp)) { + msgid = (const char *)PyUnicode_AsUTF8(tmp); + } + + if (!(msgctxt && msgid)) { + continue; + } + + key = _ghashutil_keyalloc(msgctxt, msgid); + + /* Do not overwrite existing keys! */ + if (BLI_ghash_lookup(_translations_cache, (void *)key)) { + continue; + } + + if (trans && PyUnicode_Check(trans) && !PyUnicode_READY(trans)) { + BLI_ghash_insert(_translations_cache, key, BLI_strdup((const char *)PyUnicode_AsUTF8(trans))); + } + } + } + } + + /* Clean up! */ + if (language) + MEM_freeN(language); + if (language_country) + MEM_freeN(language_country); + if (language_variant) + MEM_freeN(language_variant); +} + +const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *msgid) +{ +#define STATIC_LOCALE_SIZE 32 /* Should be more than enough! */ + + GHashKey *key; + static char locale[STATIC_LOCALE_SIZE] = ""; + const char *tmp; + + /* Just in case, should never happen! */ + if (!_translations) + return msgid; + + tmp = BLF_lang_get(); + if (strcmp(tmp, locale) || !_translations_cache) { + PyGILState_STATE _py_state; + + BLI_strncpy(locale, tmp, STATIC_LOCALE_SIZE); + + /* Locale changed or cache does not exist, refresh the whole cache! */ + /* This func may be called from C (i.e. outside of python interpreter 'context'). */ + _py_state = PyGILState_Ensure(); + + _build_translations_cache(_translations->py_messages, locale); + + PyGILState_Release(_py_state); + } + + /* And now, simply create the key (context, messageid) and find it in the cached dict! */ + key = _ghashutil_keyalloc(msgctxt ? msgctxt : BLF_I18NCONTEXT_DEFAULT, msgid); + + tmp = BLI_ghash_lookup(_translations_cache, key); + + if (tmp) + return tmp; + return msgid; + +#undef STATIC_LOCALE_SIZE +} + +PyDoc_STRVAR(app_translations_py_messages_register_doc, +".. method:: register(module_name, translations_dict)\n" +"\n" +" Registers an addon's UI translations.\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 PyObject *app_translations_py_messages_register(BlenderAppTranslations *self, PyObject *args, PyObject *kw) +{ + static const char *kwlist[] = {"module_name", "translations_dict", NULL}; + PyObject *module_name, *uuid_dict; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!:bpy.app.translations.register", (char **)kwlist, &PyUnicode_Type, + &module_name, &PyDict_Type, &uuid_dict)) + { + return NULL; + } + + if (PyDict_Contains(self->py_messages, module_name)) { + PyErr_Format(PyExc_ValueError, + "bpy.app.translations.register: translations message cache already contains some data for " + "addon '%s'", (const char *)PyUnicode_1BYTE_DATA(module_name)); + return NULL; + } + + PyDict_SetItem(self->py_messages, module_name, uuid_dict); + + /* Clear cached messages dict! */ + _clear_translations_cache(); + + /* And we are done! */ + Py_RETURN_NONE; +} + +PyDoc_STRVAR(app_translations_py_messages_unregister_doc, +".. method:: unregister(module_name)\n" +"\n" +" Unregisters an addon's UI translations.\n" +"\n" +" :arg module_name: The name identifying the addon.\n" +" :type module_name: string\n" +"\n" +); +static PyObject *app_translations_py_messages_unregister(BlenderAppTranslations *self, PyObject *args, PyObject *kw) +{ + static const char *kwlist[] = {"module_name", NULL}; + PyObject *module_name; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "O!:bpy.app.translations.unregister", (char **)kwlist, &PyUnicode_Type, + &module_name)) + { + return NULL; + } + + if (PyDict_Contains(self->py_messages, module_name)) { + PyDict_DelItem(self->py_messages, module_name); + /* Clear cached messages ghash! */ + _clear_translations_cache(); + } + + /* And we are done! */ + Py_RETURN_NONE; +} + + +/***** C-defined contexts *****/ + +static PyTypeObject BlenderAppTranslationsContextsType; + +static BLF_i18n_contexts_descriptor _contexts[] = BLF_I18NCONTEXTS_DESC; + +/* These fields are just empty placeholders, actual values get set in app_translations_struct(). + * This allows us to avoid many handwriting, and above all, to keep all context definition stuff in BLF_translation.h! + */ +static PyStructSequence_Field +app_translations_contexts_fields[sizeof(_contexts) / sizeof(BLF_i18n_contexts_descriptor)] = {{NULL}}; + +static PyStructSequence_Desc app_translations_contexts_desc = { + (char *)"bpy.app.translations.contexts", /* name */ + (char *)"This named tuple contains all pre-defined translation contexts", /* doc */ + app_translations_contexts_fields, /* fields */ + (sizeof(app_translations_contexts_fields) / sizeof(PyStructSequence_Field)) - 1 +}; + +static PyObject *app_translations_contexts_make(void) +{ + PyObject *translations_contexts; + BLF_i18n_contexts_descriptor *ctxt; + int pos = 0; + + translations_contexts = PyStructSequence_New(&BlenderAppTranslationsContextsType); + if (translations_contexts == NULL) { + return NULL; + } + +#define SetObjString(item) PyStructSequence_SET_ITEM(translations_contexts, pos++, PyUnicode_FromString((item))) + + for (ctxt = _contexts; ctxt->c_id; ctxt++) { + SetObjString(ctxt->value); + } + +#undef SetObjString + + return translations_contexts; +} + +/***** Main BlenderAppTranslations Py object definition *****/ + +PyDoc_STRVAR(app_translations_contexts_doc, + "A named tuple containing all pre-defined translation contexts." +); + +PyDoc_STRVAR(app_translations_contexts_C_to_py_doc, + "A readonly dict mapping contexts' C-identifiers to their py-identifiers." +); + +PyMemberDef app_translations_members[] = { + {(char *)"contexts", T_OBJECT_EX, offsetof(BlenderAppTranslations, contexts), READONLY, + app_translations_contexts_doc}, + {(char *)"contexts_C_to_py", T_OBJECT_EX, offsetof(BlenderAppTranslations, contexts_C_to_py), READONLY, + app_translations_contexts_C_to_py_doc}, + {NULL} +}; + +PyDoc_STRVAR(app_translations_locale_doc, "The actual locale currently in use."); +static PyObject *app_translations_locale_get(PyObject *UNUSED(self), void *UNUSED(userdata)) +{ + return PyUnicode_FromString(BLF_lang_get()); +} + +/* Note: defining as getter, as (even if quite unlikely), this *may* change during runtime... */ +PyDoc_STRVAR(app_translations_locales_doc, "All locales currently known by Blender (i.e. available as translations)."); +static PyObject *app_translations_locales_get(PyObject *UNUSED(self), void *UNUSED(userdata)) +{ + PyObject *ret; + EnumPropertyItem *it, *items = BLF_RNA_lang_enum_properties(); + int num_locales = 0, pos = 0; + + /* This is not elegant, but simple! */ + for (it = items; it->identifier; it++) { + if (it->value) + num_locales++; + } + + ret = PyTuple_New(num_locales); + +#define TupleSetItem() PyTuple_SET_ITEM(ret, pos++, PyUnicode_FromString(it->description)) + + for (it = items; it->identifier; it++) { + if (it->value) + TupleSetItem(); + } + +#undef TupleSetItem + + return ret; +} + +PyGetSetDef app_translations_getseters[] = { + /* {name, getter, setter, doc, userdata} */ + {(char *)"locale", (getter)app_translations_locale_get, NULL, app_translations_locale_doc, NULL}, + {(char *)"locales", (getter)app_translations_locales_get, NULL, app_translations_locales_doc, NULL}, + {NULL} +}; + +PyDoc_STRVAR(app_translations_pgettext_doc, +".. method:: pgettext(msgid, msgctxt)\n" +"\n" +" Try to translate the given msgid (with optional msgctxt).\n" +" NOTE: The (msgid, msgctxt) parameter orders has been switched compared to gettext function, to allow\n" +" single-parameter calls (context then defaults to BLF_I18NCONTEXT_DEFAULT)." +" NOTE: You should really rarely need to use this function in regular addon code, as all translation should be\n" +" handled by Blender internal code." +"\n" +" :arg msgid: The string to translate.\n" +" :type msgid: string\n" +" :arg msgctxt: The translation context.\n" +" :type msgctxt: string\n" +" :default msgctxt: BLF_I18NCONTEXT_DEFAULT value.\n" +" :return: The translated string (or msgid if no translation was found).\n" +"\n" +); +static PyObject *app_translations_pgettext(BlenderAppTranslations *UNUSED(self), PyObject *args, PyObject *kw) +{ + static const char *kwlist[] = {"msgid", "msgctxt", NULL}; + char *msgid, *msgctxt = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:bpy.app.translations.pgettext()", (char **)kwlist, + &msgid, &msgctxt)) + { + return NULL; + } + + return PyUnicode_FromString(BLF_pgettext(msgctxt ? msgctxt : BLF_I18NCONTEXT_DEFAULT, msgid)); +} + +PyMethodDef app_translations_methods[] = { + /* Can't use METH_KEYWORDS alone, see http://bugs.python.org/issue11587 */ + {(char *)"register", (PyCFunction)app_translations_py_messages_register, METH_VARARGS | METH_KEYWORDS, + app_translations_py_messages_register_doc}, + {(char *)"unregister", (PyCFunction)app_translations_py_messages_unregister, METH_VARARGS | METH_KEYWORDS, + app_translations_py_messages_unregister_doc}, + {(char *)"pgettext", (PyCFunction)app_translations_pgettext, METH_VARARGS | METH_KEYWORDS | METH_STATIC, + app_translations_pgettext_doc}, + {NULL} +}; + +static PyObject *app_translations_new(PyTypeObject *type, PyObject *UNUSED(args), PyObject *UNUSED(kw)) +{ +/* printf("%s (%p)\n", __func__, _translations); */ + + if (!_translations) { + _translations = (BlenderAppTranslations *)type->tp_alloc(type, 0); + if (_translations) { + PyObject *py_ctxts; + BLF_i18n_contexts_descriptor *ctxt; + + _translations->contexts = app_translations_contexts_make(); + + py_ctxts = PyDict_New(); + for (ctxt = _contexts; ctxt->c_id; ctxt++) { + PyObject *val = PyUnicode_FromString(ctxt->py_id); + PyDict_SetItemString(py_ctxts, ctxt->c_id, val); + Py_DECREF(val); + } + _translations->contexts_C_to_py = PyDictProxy_New(py_ctxts); + Py_DECREF(py_ctxts); /* The actual dict is only owned by its proxy */ + + _translations->py_messages = PyDict_New(); + } + } + + return (PyObject *)_translations; +} + +PyDoc_STRVAR(app_translations_doc, +" This object contains some data/methods regarding internationalization in Blender, and allows every py script\n" +" to feature translations for its own UI messages.\n" +"\n" +" WARNING: Most of this object should only be useful if you actually manipulate i18n stuff from Python.\n" +" If you are a regular addon, you should only bother about :contexts: and :context_sep: members, and the \n" +" :register:/:unregister: functions!" +"\n" +" To add translations to your python script, you must define a dictionary formatted like that:\n" +" {locale: {msg_key: msg_translation, ...}, ...}\n" +" where:\n" +" locale is either a lang iso code (e.g. 'fr'), a lang+country code (e.g. 'pt_BR'),\n" +" a lang+variant code (e.g. 'sr@latin'), or a full code (e.g. 'uz_UZ@cyrilic').\n" +" msg_key is a tuple (context, org message) - use, as much as possible, the predefined :contexts:.\n" +" msg_translation is the translated message in given language!" +" Then, call bpy.app.translations.register(__name__, your_dict) in your register() function, and \n" +" bpy.app.translations.unregister(__name__) in your unregister() one.\n" +"\n" +" bl_i18n_utils module has several functions to help you collect strings to translate, and generate the needed\n" +" python code (the translation dictionary), as well as optional intermediary po files if you want some...\n" +" See its documentation for more details.\n" +"\n" +); +static PyTypeObject BlenderAppTranslationsType = { + PyVarObject_HEAD_INIT(NULL, 0) + /* tp_name */ + (char *)"bpy.app._translations_type", + /* tp_basicsize */ + sizeof(BlenderAppTranslations), + 0, /* tp_itemsize */ + /* methods */ + /* No destructor, this is a singleton! */ + NULL, /* tp_dealloc */ + NULL, /* printfunc tp_print; */ + NULL, /* getattrfunc tp_getattr; */ + NULL, /* setattrfunc tp_setattr; */ + NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */ + NULL, /* tp_repr */ + + /* Method suites for standard classes */ + NULL, /* PyNumberMethods *tp_as_number; */ + NULL, /* PySequenceMethods *tp_as_sequence; */ + NULL, /* PyMappingMethods *tp_as_mapping; */ + + /* More standard operations (here for binary compatibility) */ + NULL, /* hashfunc tp_hash; */ + NULL, /* ternaryfunc tp_call; */ + NULL, /* reprfunc tp_str; */ + NULL, /* getattrofunc tp_getattro; */ + NULL, /* setattrofunc tp_setattro; */ + + /* Functions to access object as input/output buffer */ + NULL, /* PyBufferProcs *tp_as_buffer; */ + + /*** Flags to define presence of optional/expanded features ***/ + Py_TPFLAGS_DEFAULT, /* long tp_flags; */ + + app_translations_doc, /* char *tp_doc; Documentation string */ + + /*** Assigned meaning in release 2.0 ***/ + /* call function for all accessible objects */ + NULL, /* traverseproc tp_traverse; */ + + /* delete references to contained objects */ + NULL, /* inquiry tp_clear; */ + + /*** Assigned meaning in release 2.1 ***/ + /*** rich comparisons ***/ + NULL, /* richcmpfunc tp_richcompare; */ + + /*** weak reference enabler ***/ + 0, /* long tp_weaklistoffset */ + + /*** Added in release 2.2 ***/ + /* Iterators */ + NULL, /* getiterfunc tp_iter; */ + NULL, /* iternextfunc tp_iternext; */ + + /*** Attribute descriptor and subclassing stuff ***/ + app_translations_methods, /* struct PyMethodDef *tp_methods; */ + app_translations_members, /* struct PyMemberDef *tp_members; */ + app_translations_getseters, /* struct PyGetSetDef *tp_getset; */ + NULL, /* struct _typeobject *tp_base; */ + NULL, /* PyObject *tp_dict; */ + NULL, /* descrgetfunc tp_descr_get; */ + NULL, /* descrsetfunc tp_descr_set; */ + 0, /* long tp_dictoffset; */ + NULL, /* initproc tp_init; */ + NULL, /* allocfunc tp_alloc; */ + /* newfunc tp_new; */ + (newfunc)app_translations_new, + /* Low-level free-memory routine */ + NULL, /* freefunc tp_free; */ + /* For PyObject_IS_GC */ + NULL, /* inquiry tp_is_gc; */ + NULL, /* PyObject *tp_bases; */ + /* method resolution order */ + NULL, /* PyObject *tp_mro; */ + NULL, /* PyObject *tp_cache; */ + NULL, /* PyObject *tp_subclasses; */ + NULL, /* PyObject *tp_weaklist; */ + NULL +}; + +PyObject *BPY_app_translations_struct(void) +{ + PyObject *ret; + + /* Let's finalize our contexts structseq definition! */ + { + BLF_i18n_contexts_descriptor *ctxt; + PyStructSequence_Field *desc; + + /* We really populate the contexts' fields here! */ + for (ctxt = _contexts, desc = app_translations_contexts_desc.fields; ctxt->c_id; ctxt++, desc++) { + desc->name = (char *)ctxt->py_id; + desc->doc = NULL; + } + desc->name = desc->doc = NULL; /* End sentinel! */ + + PyStructSequence_InitType(&BlenderAppTranslationsContextsType, &app_translations_contexts_desc); + } + + if (PyType_Ready(&BlenderAppTranslationsType) < 0) + return NULL; + + ret = PyObject_CallObject((PyObject *)&BlenderAppTranslationsType, NULL); + + /* prevent user from creating new instances */ + BlenderAppTranslationsType.tp_new = NULL; + /* without this we can't do set(sys.modules) [#29635] */ + BlenderAppTranslationsType.tp_hash = (hashfunc)_Py_HashPointer; + + return ret; +} + +#else /* WITH_INTERNATIONAL */ + +PyObject *BPY_app_translations_struct(void) +{ + Py_RETURN_NONE; +} + +#endif /* WITH_INTERNATIONAL */ diff --git a/source/blender/python/intern/bpy_app_translations.h b/source/blender/python/intern/bpy_app_translations.h new file mode 100644 index 00000000000..704307574d0 --- /dev/null +++ b/source/blender/python/intern/bpy_app_translations.h @@ -0,0 +1,32 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Bastien Montagne + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/python/intern/bpy_app_translations.h + * \ingroup pythonintern + */ + +#ifndef __BPY_APP_TRANSLATIONS_H__ +#define __BPY_APP_TRANSLATIONS_H__ + +PyObject *BPY_app_translations_struct(void); + +#endif /* __BPY_APP_TRANSLATIONS_H__ */ diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index f3fa0c9a0a9..f5033a97861 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -74,13 +74,15 @@ static EnumPropertyItem property_flag_enum_items[] = { {0, NULL, 0, NULL, NULL}}; /* subtypes */ +/* XXX Keep in sync with rna_rna.c's property_subtype_items ??? + * Currently it is not... + */ static EnumPropertyItem property_subtype_string_items[] = { {PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""}, {PROP_DIRPATH, "DIR_PATH", 0, "Directory Path", ""}, {PROP_FILENAME, "FILE_NAME", 0, "Filename", ""}, {PROP_BYTESTRING, "BYTE_STRING", 0, "Byte String", ""}, - {PROP_TRANSLATE, "TRANSLATE", 0, "Translate", ""}, - {PROP_PASSWORD, "PASSWORD", 0, "Password", 0}, + {PROP_PASSWORD, "PASSWORD", 0, "Password", "A string that is displayed hidden ('********')"}, {PROP_NONE, "NONE", 0, "None", ""}, {0, NULL, 0, NULL, NULL}}; diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 94f262f57f5..611095bfe20 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -1623,8 +1623,12 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyOb } } else { - /* Unicode String */ +#ifdef WITH_INTERNATIONAL + bool do_translate = RNA_property_flag(prop) & PROP_STRING_PY_TRANSLATE; +#else + bool do_translate = false; +#endif /* WITH_INTERNATIONAL */ #ifdef USE_STRING_COERCE PyObject *value_coerce = NULL; @@ -1634,17 +1638,16 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyOb } else { param = _PyUnicode_AsString(value); -#ifdef WITH_INTERNATIONAL - if (subtype == PROP_TRANSLATE) { - param = IFACE_(param); - } -#endif /* WITH_INTERNATIONAL */ - } #else /* USE_STRING_COERCE */ param = _PyUnicode_AsString(value); #endif /* USE_STRING_COERCE */ + /* Any half-brained compiler should be able to optimize this out when WITH_INTERNATIONAL is off */ + if (do_translate) { + param = IFACE_(param); + } + if (param == NULL) { if (PyUnicode_Check(value)) { /* there was an error assigning a string type,