2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-09-20 08:17:26 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup blt
|
2014-01-19 23:14:24 +11:00
|
|
|
*
|
|
|
|
|
* Manages translation files and provides translation functions.
|
|
|
|
|
* (which are optional and can be disabled as a preference).
|
2011-09-20 08:17:26 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2011-09-20 08:17:26 +00:00
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
2011-12-22 18:25:59 +00:00
|
|
|
|
2011-09-20 08:17:26 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2011-11-09 14:13:17 +00:00
|
|
|
#include "DNA_userdef_types.h" /* For user settings. */
|
|
|
|
|
|
2015-05-19 11:34:52 +10:00
|
|
|
#ifdef WITH_PYTHON
|
2024-09-24 17:07:49 +02:00
|
|
|
# include "BPY_extern.hh"
|
2015-05-19 11:34:52 +10:00
|
|
|
#endif
|
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.
2013-01-20 17:29:07 +00:00
|
|
|
|
2013-01-21 08:08:20 +00:00
|
|
|
#ifdef WITH_INTERNATIONAL
|
2019-01-26 23:08:11 +11:00
|
|
|
# include "BLI_threads.h"
|
2024-12-14 17:32:49 +01:00
|
|
|
# include "blender_locale.h"
|
2013-01-21 08:08:20 +00:00
|
|
|
#endif /* WITH_INTERNATIONAL */
|
2011-09-20 08:17:26 +00:00
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
bool BLT_is_default_context(const char *msgctxt)
|
2013-03-19 19:37:22 +00:00
|
|
|
{
|
|
|
|
|
/* We use the "short" test, a more complete one could be:
|
2015-08-16 17:32:01 +10:00
|
|
|
* return (!msgctxt || !msgctxt[0] || STREQ(msgctxt, BLT_I18NCONTEXT_DEFAULT_BPYRNA))
|
2013-03-19 19:37:22 +00:00
|
|
|
*/
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: trying without the void string check for now, it *should* not be necessary... */
|
2015-08-16 17:32:01 +10:00
|
|
|
return (!msgctxt || msgctxt[0] == BLT_I18NCONTEXT_DEFAULT_BPYRNA[0]);
|
2013-03-19 19:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
const char *BLT_pgettext(const char *msgctxt, const char *msgid)
|
2011-09-20 08:17:26 +00:00
|
|
|
{
|
2011-09-26 10:35:47 +00:00
|
|
|
#ifdef WITH_INTERNATIONAL
|
2013-03-08 15:34:23 +00:00
|
|
|
const char *ret = msgid;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-08 15:34:23 +00:00
|
|
|
if (msgid && msgid[0]) {
|
2015-08-16 17:32:01 +10:00
|
|
|
if (BLT_is_default_context(msgctxt)) {
|
|
|
|
|
msgctxt = BLT_I18NCONTEXT_DEFAULT;
|
2013-01-21 10:52:34 +00:00
|
|
|
}
|
|
|
|
|
ret = bl_locale_pgettext(msgctxt, msgid);
|
2023-07-20 09:49:10 +02:00
|
|
|
/* 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!
|
|
|
|
|
*/
|
2015-05-19 11:34:52 +10:00
|
|
|
# ifdef WITH_PYTHON
|
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.
2013-01-20 17:29:07 +00:00
|
|
|
if (ret == msgid) {
|
|
|
|
|
ret = BPY_app_translations_py_pgettext(msgctxt, msgid);
|
|
|
|
|
}
|
2015-05-19 11:34:52 +10:00
|
|
|
# endif
|
2012-11-11 16:54:26 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-08 15:34:23 +00:00
|
|
|
return ret;
|
2011-09-20 08:17:26 +00:00
|
|
|
#else
|
2012-11-11 16:54:26 +00:00
|
|
|
(void)msgctxt;
|
2011-09-20 08:17:26 +00:00
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2011-11-09 14:13:17 +00:00
|
|
|
|
2023-07-20 20:11:08 +10:00
|
|
|
bool BLT_translate()
|
2015-04-10 15:28:47 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2023-06-07 11:31:34 +02:00
|
|
|
return BLI_thread_is_main();
|
2015-04-10 15:28:47 +02:00
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 20:11:08 +10:00
|
|
|
bool BLT_translate_iface()
|
2011-11-09 14:13:17 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2018-09-13 11:20:49 +02:00
|
|
|
return BLT_translate() && (U.transopts & USER_TR_IFACE);
|
2011-11-09 14:13:17 +00:00
|
|
|
#else
|
2013-01-21 08:08:20 +00:00
|
|
|
return false;
|
2011-11-09 14:13:17 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 20:11:08 +10:00
|
|
|
bool BLT_translate_tooltips()
|
2011-11-09 14:13:17 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2018-09-13 11:20:49 +02:00
|
|
|
return BLT_translate() && (U.transopts & USER_TR_TOOLTIPS);
|
2011-11-09 14:13:17 +00:00
|
|
|
#else
|
2013-01-21 08:08:20 +00:00
|
|
|
return false;
|
2011-11-09 14:13:17 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 19:49:03 +01:00
|
|
|
bool BLT_translate_reports()
|
|
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
|
|
|
|
return BLT_translate() && (U.transopts & USER_TR_REPORTS);
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 20:11:08 +10:00
|
|
|
bool BLT_translate_new_dataname()
|
2013-03-20 18:42:09 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2018-09-13 11:20:49 +02:00
|
|
|
return BLT_translate() && (U.transopts & USER_TR_NEWDATANAME);
|
2013-03-20 18:42:09 +00:00
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
const char *BLT_translate_do(const char *msgctxt, const char *msgid)
|
2015-04-10 15:28:47 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2015-08-16 17:32:01 +10:00
|
|
|
if (BLT_translate()) {
|
|
|
|
|
return BLT_pgettext(msgctxt, msgid);
|
2015-04-10 15:28:47 +02:00
|
|
|
}
|
2020-08-07 13:37:22 +02:00
|
|
|
|
|
|
|
|
return msgid;
|
|
|
|
|
|
2015-04-10 15:28:47 +02:00
|
|
|
#else
|
|
|
|
|
(void)msgctxt;
|
|
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
const char *BLT_translate_do_iface(const char *msgctxt, const char *msgid)
|
2011-11-09 14:13:17 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2015-08-16 17:32:01 +10:00
|
|
|
if (BLT_translate_iface()) {
|
|
|
|
|
return BLT_pgettext(msgctxt, msgid);
|
2012-03-16 15:39:25 +00:00
|
|
|
}
|
2020-08-07 13:37:22 +02:00
|
|
|
|
|
|
|
|
return msgid;
|
|
|
|
|
|
2011-11-09 14:13:17 +00:00
|
|
|
#else
|
2012-11-11 16:54:26 +00:00
|
|
|
(void)msgctxt;
|
2011-11-09 14:13:17 +00:00
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
const char *BLT_translate_do_tooltip(const char *msgctxt, const char *msgid)
|
2011-11-09 14:13:17 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2015-08-16 17:32:01 +10:00
|
|
|
if (BLT_translate_tooltips()) {
|
|
|
|
|
return BLT_pgettext(msgctxt, msgid);
|
2012-03-16 15:39:25 +00:00
|
|
|
}
|
2020-08-07 13:37:22 +02:00
|
|
|
|
|
|
|
|
return msgid;
|
|
|
|
|
|
2011-11-09 14:13:17 +00:00
|
|
|
#else
|
2012-11-11 16:54:26 +00:00
|
|
|
(void)msgctxt;
|
2011-11-09 14:13:17 +00:00
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2013-03-20 18:42:09 +00:00
|
|
|
|
2024-01-11 19:49:03 +01:00
|
|
|
const char *BLT_translate_do_report(const char *msgctxt, const char *msgid)
|
|
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
|
|
|
|
if (BLT_translate_reports()) {
|
|
|
|
|
return BLT_pgettext(msgctxt, msgid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msgid;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
(void)msgctxt;
|
|
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
const char *BLT_translate_do_new_dataname(const char *msgctxt, const char *msgid)
|
2013-03-20 18:42:09 +00:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_INTERNATIONAL
|
2015-08-16 17:32:01 +10:00
|
|
|
if (BLT_translate_new_dataname()) {
|
|
|
|
|
return BLT_pgettext(msgctxt, msgid);
|
2013-03-20 18:42:09 +00:00
|
|
|
}
|
2020-08-07 13:37:22 +02:00
|
|
|
|
|
|
|
|
return msgid;
|
|
|
|
|
|
2013-03-20 18:42:09 +00:00
|
|
|
#else
|
|
|
|
|
(void)msgctxt;
|
|
|
|
|
return msgid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|