From cef730d969b35d7cc25006acded05c361602b319 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sun, 20 Jan 2013 17:29:07 +0000 Subject: Python i18n API. Many thanks to Campbell and Brecht for the reviews and suggestions! This commit adds: * A new bpy.app.translations module giving some info about locales/translation stuff (current active locale, all locales currently known by blender, all translation contexts currently defined, etc.). * The ability for addons to feature translations, using the (un)register functions of above module. * Also cleans up "translate py string when storing into RNA prop" by removing "PROP_TRANSLATE" string's subtype, and adding a PROP_STRING_PY_TRANSLATE flag instead (this way it is no more exposed to python...). Addon translations work with py dictionaries: each addon features a dict {lang: {(context, message): translation, ...}, ...}, which is registered when the addon is enabled (and unregistered when disabled). Then, when a key (context, message) is not found in regular mo catalog, a cache dict for current locale is built from all registered addon translations, and key is searched in it. Note: currently addons writers have to do all the work by hand, will add something (probably extend "edit translation" addon) to automate messages extraction from addons soon(ish)! To get a look to expected behavior from addons, have a look at render_copy_settings/__init__.py and render_copy_settings/translations.py (rather stupid example currently, but...). Once we have a complete process, I'll also update relevant wiki pages. --- intern/locale/boost_locale_wrapper.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'intern/locale/boost_locale_wrapper.cpp') 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 -- cgit v1.2.3