>From ab468fc8110316f3ec8c4651a95b058b82812e86 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sat, 31 Aug 2019 00:23:19 +0200 Subject: [PATCH] Improve how gettext is used --- src/include/gnunet_os_lib.h | 12 ++++++++++++ src/include/platform.h | 2 +- src/util/os_installation.c | 26 ++++++++++++++++++++++++++ src/util/program.c | 21 +++++++++++++-------- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/include/gnunet_os_lib.h b/src/include/gnunet_os_lib.h index d43711a99..b632ab262 100644 --- a/src/include/gnunet_os_lib.h +++ b/src/include/gnunet_os_lib.h @@ -276,6 +276,18 @@ struct GNUNET_OS_ProjectData * Non-zero means this project is part of GNU. */ int is_gnu; + + /** + * Gettext domain for localisation, e.g. the PACKAGE macro. + * Setting this field to NULL disables gettext. + */ + char *gettext_domain; + + /** + * Gettext directory, e.g. the LOCALEDIR macro. + * If this field is NULL, the path is automatically inferred. + */ + char *gettext_path; }; diff --git a/src/include/platform.h b/src/include/platform.h index 4636ddd73..0e3144ee8 100644 --- a/src/include/platform.h +++ b/src/include/platform.h @@ -205,7 +205,7 @@ /** * GNU gettext support macro. */ -#define _(String) dgettext("gnunet",String) +#define _(String) dgettext(PACKAGE,String) #define LIBEXTRACTOR_GETTEXT_DOMAIN "libextractor" #else #include "libintlemu.h" diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 9dcfa5ef1..f51bfd287 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -64,6 +64,9 @@ static const struct GNUNET_OS_ProjectData default_pd = { .homepage = "http://www.gnu.org/s/gnunet/", .config_file = "gnunet.conf", .user_config_file = "~/.config/gnunet.conf", + .is_gnu = 1, + .gettext_domain = PACKAGE, + .gettext_path = NULL, }; /** @@ -72,6 +75,13 @@ static const struct GNUNET_OS_ProjectData default_pd = { */ static const struct GNUNET_OS_ProjectData *current_pd = &default_pd; +/** + * Wether or not gettext has been initialized for the library. + * Note that the gettext initialization done within + * GNUNET_PROGRAM_run2 is for the specific application. + */ +static int gettextinit = 0; + /** * Return default project data used by 'libgnunetutil' for GNUnet. */ @@ -88,6 +98,14 @@ GNUNET_OS_project_data_default (void) const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get () { + if (0 == gettextinit) + { + char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); + if (NULL != path) + BINDTEXTDOMAIN (PACKAGE, path); + GNUNET_free(path); + gettextinit = 1; + } return current_pd; } @@ -100,6 +118,14 @@ GNUNET_OS_project_data_get () void GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd) { + if (0 == gettextinit) + { + char *path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); + if (NULL != path) + BINDTEXTDOMAIN (PACKAGE, path); + GNUNET_free(path); + gettextinit = 1; + } GNUNET_assert (NULL != pd); current_pd = pd; } diff --git a/src/util/program.c b/src/util/program.c index 1462a03a8..73beb8d57 100644 --- a/src/util/program.c +++ b/src/util/program.c @@ -200,14 +200,19 @@ GNUNET_PROGRAM_run2 (int argc, cc.cfg = cfg = GNUNET_CONFIGURATION_create (); /* prepare */ #if ENABLE_NLS - setlocale (LC_ALL, ""); - path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); - if (NULL != path) - { - BINDTEXTDOMAIN ("GNUnet", path); - GNUNET_free (path); - } - textdomain ("GNUnet"); + if (NULL != pd->gettext_domain) + { + setlocale (LC_ALL, ""); + path = (NULL == pd->gettext_path) + ? GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR) + : GNUNET_strdup (pd->gettext_path); + if (NULL != path) + { + BINDTEXTDOMAIN (pd->gettext_domain, path); + GNUNET_free (path); + } + textdomain (pd->gettext_domain); + } #endif cnt = 0; while (NULL != options[cnt].name) -- 2.21.0