Currently, gettext doesn't work for out-of-tree applications. This is because GNUnet forcibly set the text domain to "GNUnet" (which apparently is also incorrect), so applications can't be localized unless their localizations are distributed in-tree by GNUnet itself.
The attached patch tries to fix this by adding two more fields to GNUNET_OS_ProjectData: one field is the gettext domain of the application. As the documentation says, if it's NULL gettext is disabled so that applications can use their preferred localization method without having gettext interfering; the other field is essentially the locale directory, so applications can specify a different path if they want to, instead of having GNUnet infer it for them. Because some GNUnet libraries also use gettext internally (the util lib is a prominent example), gettext has to be initialized before the application takes over. I placed such initialization in `GNUNET_OS_init' and `GNUNET_OS_project_data_get' because those are two functions which are very likely to be called (especially the second one, since it's used in `GNUNET_PROGRAM_run2'.) If there is a better place (or some places where this is not enough) I can change it and resubmit it for review. I also changed gnunet-ext to keep it consitent with the patch. In particular, it adds a header which is required for a successful compilation, so you might want to at least make that change. Thank you, A.V. P.S. I'm still not subscribed to the list... yet.
>From ab468fc8110316f3ec8c4651a95b058b82812e86 Mon Sep 17 00:00:00 2001 From: Alessio Vanni <[email protected]> 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
>From 454fcc15f63bbf65eaa827af81d808e457ebe732 Mon Sep 17 00:00:00 2001 From: Alessio Vanni <[email protected]> Date: Sat, 31 Aug 2019 12:07:43 +0200 Subject: [PATCH] Add missing header and set new gettext fields in project data --- src/ext/gnunet-ext.c | 6 ++++++ src/ext/gnunet-service-ext.c | 4 ++++ src/ext/test_ext_api.c | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/src/ext/gnunet-ext.c b/src/ext/gnunet-ext.c index f18e351..00195e0 100644 --- a/src/ext/gnunet-ext.c +++ b/src/ext/gnunet-ext.c @@ -34,6 +34,10 @@ #define SOCKTYPE int #endif +#if HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif + #include <gnunet/gettext.h> #include <gnunet/gnunet_util_lib.h> #include "gnunet_ext_service.h" @@ -56,6 +60,8 @@ static const struct GNUNET_OS_ProjectData gnunetext_pd = .user_config_file = "~/.config/gnunet-ext.conf", .version = "1.0", .is_gnu = 1, + .gettext_domain = PACKAGE, + .gettext_path = NULL, }; /** diff --git a/src/ext/gnunet-service-ext.c b/src/ext/gnunet-service-ext.c index aa43933..4ebcdf2 100644 --- a/src/ext/gnunet-service-ext.c +++ b/src/ext/gnunet-service-ext.c @@ -34,6 +34,10 @@ #define SOCKTYPE int #endif +#if HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif + #include <gnunet/gnunet_util_lib.h> #include "gnunet_protocols_ext.h" diff --git a/src/ext/test_ext_api.c b/src/ext/test_ext_api.c index e69dca0..6a95d04 100644 --- a/src/ext/test_ext_api.c +++ b/src/ext/test_ext_api.c @@ -32,6 +32,10 @@ #define SOCKTYPE int #endif +#if HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif + #include <gnunet/gnunet_util_lib.h> #include "gnunet_ext_service.h" -- 2.21.0
_______________________________________________ GNUnet-developers mailing list [email protected] https://lists.gnu.org/mailman/listinfo/gnunet-developers
