From 5f09e302a97d1b5b8ed66a6ceb316dbbf2a340fe Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 15 Apr 2021 17:36:39 +0200 Subject: [PATCH] pipewire: add i18n initialization Initialize the i18n support. Add two methods to call the i18n interface. Add defines for _() and N_() for translations. --- meson.build | 2 +- src/pipewire/pipewire.c | 32 +++++++++++++++++++++++++++++++- src/pipewire/pipewire.h | 7 +++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 7fdd841de..8fa02a6cb 100644 --- a/meson.build +++ b/meson.build @@ -186,7 +186,7 @@ cdata.set('PIPEWIRE_API_VERSION', '"@0@"'.format(apiversion)) cdata.set('PIPEWIRE_DATADIR', '"@0@"'.format(pipewire_datadir)) cdata.set('LOCALEDIR', '"@0@"'.format(pipewire_localedir)) cdata.set('LIBDIR', '"@0@"'.format(pipewire_libdir)) -cdata.set('GETTEXT_PACKAGE', '"pipewire"') +cdata.set('GETTEXT_PACKAGE', '"@0@"'.format(meson.project_name())) cdata.set('PIPEWIRE_LICENSE', '"MIT"') cdata.set('PIPEWIRE_PACKAGE_ORIGIN', '"Unknown package origin"') cdata.set('PIPEWIRE_PACKAGE_NAME', '"PipeWire source release"') diff --git a/src/pipewire/pipewire.c b/src/pipewire/pipewire.c index 4c4487e13..1b2a8f8ea 100644 --- a/src/pipewire/pipewire.c +++ b/src/pipewire/pipewire.c @@ -49,6 +49,8 @@ #define SUPPORTLIB "support/libspa-support" +static struct spa_i18n *_pipewire_i18n = NULL; + struct plugin { struct spa_list link; char *filename; @@ -376,6 +378,18 @@ static const char *i18n_ntext(void *object, const char *msgid, const char *msgid return dngettext(support->i18n_domain, msgid, msgid_plural, n); } +static void init_i18n(struct support *support) +{ + /* Load locale from the environment. */ + setlocale(LC_ALL, ""); + /* Set LC_NUMERIC to C so that floating point strings are consistently + * formatted and parsed across locales. */ + setlocale(LC_NUMERIC, "C"); + bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); + bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); + pw_set_domain(GETTEXT_PACKAGE); +} + static void *add_i18n(struct support *support) { static struct spa_i18n_methods i18n_methods = { @@ -387,11 +401,25 @@ static void *add_i18n(struct support *support) SPA_TYPE_INTERFACE_I18N, SPA_VERSION_I18N, &i18n_methods, support); + _pipewire_i18n = (struct spa_i18n*) &support->i18n_iface; + support->support[support->n_support++] = - SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_I18N, &support->i18n_iface); + SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_I18N, _pipewire_i18n); + return 0; } +SPA_EXPORT +const char *pw_gettext(const char *msgid) +{ + return spa_i18n_text(_pipewire_i18n, msgid); +} +SPA_EXPORT +const char *pw_bgettext(const char *msgid, const char *msgid_plural, unsigned long int n) +{ + return spa_i18n_ntext(_pipewire_i18n, msgid, msgid_plural, n); +} + #ifdef HAVE_SYSTEMD static struct spa_log *load_journal_logger(struct support *support) { @@ -465,6 +493,8 @@ void pw_init(int *argc, char **argv[]) if ((str = getenv("PIPEWIRE_DEBUG"))) configure_debug(support, str); + init_i18n(support); + if ((str = getenv("SPA_PLUGIN_DIR")) == NULL) str = PLUGINDIR; support->plugin_dir = str; diff --git a/src/pipewire/pipewire.h b/src/pipewire/pipewire.h index 1e9271eab..02c236b4d 100644 --- a/src/pipewire/pipewire.h +++ b/src/pipewire/pipewire.h @@ -30,6 +30,7 @@ extern "C" { #endif #include +#include #include #include @@ -154,6 +155,12 @@ struct spa_handle *pw_load_spa_handle(const char *lib, int pw_unload_spa_handle(struct spa_handle *handle); +const char *pw_gettext(const char *msgid); +const char *pw_ngettext(const char *msgid, const char *msgid_plural, unsigned long int n); + +#define _(String) (pw_gettext(String)) +#define N_(String) (String) + #ifdef __cplusplus } #endif