From 167e8274932bed4e723c35126f54c3f4aa8ea1ac Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 18 Aug 2010 16:26:15 +1000 Subject: [PATCH] Use gettext for translations in .policy files --- src/polkitbackend/polkitbackendactionpool.c | 41 ++++++++++++++++++++ .../polkitbackendinteractiveauthority.c | 31 ++++++++++----- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/polkitbackend/polkitbackendactionpool.c b/src/polkitbackend/polkitbackendactionpool.c index 30e9540..bd8c6f4 100644 --- a/src/polkitbackend/polkitbackendactionpool.c +++ b/src/polkitbackend/polkitbackendactionpool.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -45,7 +46,9 @@ typedef struct gchar *vendor_url; gchar *icon_name; gchar *description; + gchar *description_domain; gchar *message; + gchar *message_domain; PolkitImplicitAuthorization implicit_authorization_any; PolkitImplicitAuthorization implicit_authorization_inactive; @@ -67,7 +70,9 @@ parsed_action_free (ParsedAction *action) g_free (action->vendor_url); g_free (action->icon_name); g_free (action->description); + g_free (action->description_domain); g_free (action->message); + g_free (action->message_domain); g_hash_table_unref (action->localized_description); g_hash_table_unref (action->localized_message); @@ -87,6 +92,7 @@ static void ensure_all_files (PolkitBackendActionPool *pool); static const gchar *_localize (GHashTable *translations, const gchar *untranslated, + const gchar *domain, const gchar *lang); typedef struct @@ -387,9 +393,11 @@ polkit_backend_action_pool_get_action (PolkitBackendActionPool *pool, description = _localize (parsed_action->localized_description, parsed_action->description, + parsed_action->description_domain, locale); message = _localize (parsed_action->localized_message, parsed_action->message, + parsed_action->message_domain, locale); ret = polkit_action_description_new (action_id, @@ -605,11 +613,16 @@ typedef struct { GHashTable *policy_messages; char *policy_description_nolang; + char *policy_description_domain; char *policy_message_nolang; + char *policy_message_domain; /* the value of xml:lang for the thing we're reading in _cdata() */ char *elem_lang; + /* the value of gettext-domain for the thing we're reading in _cdata() */ + char *elem_domain; + char *annotate_key; GHashTable *annotations; @@ -631,8 +644,12 @@ pd_unref_action_data (ParserData *pd) g_free (pd->policy_description_nolang); pd->policy_description_nolang = NULL; + g_free (pd->policy_description_domain); + pd->policy_description_domain = NULL; g_free (pd->policy_message_nolang); pd->policy_message_nolang = NULL; + g_free (pd->policy_message_domain); + pd->policy_message_domain = NULL; if (pd->policy_descriptions != NULL) { g_hash_table_unref (pd->policy_descriptions); @@ -652,6 +669,8 @@ pd_unref_action_data (ParserData *pd) } g_free (pd->elem_lang); pd->elem_lang = NULL; + g_free (pd->elem_domain); + pd->elem_domain = NULL; } static void @@ -739,6 +758,10 @@ _start (void *data, const char *el, const char **attr) { pd->elem_lang = g_strdup (attr[1]); } + if (num_attr == 2 && strcmp (attr[0], "gettext-domain") == 0) + { + pd->elem_domain = g_strdup (attr[1]); + } state = STATE_IN_ACTION_DESCRIPTION; } else if (strcmp (el, "message") == 0) @@ -747,6 +770,10 @@ _start (void *data, const char *el, const char **attr) { pd->elem_lang = g_strdup (attr[1]); } + if (num_attr == 2 && strcmp (attr[0], "gettext-domain") == 0) + { + pd->elem_domain = g_strdup (attr[1]); + } state = STATE_IN_ACTION_MESSAGE; } else if (strcmp (el, "vendor") == 0 && num_attr == 0) @@ -849,6 +876,7 @@ _cdata (void *data, const char *s, int len) { g_free (pd->policy_description_nolang); pd->policy_description_nolang = str; + pd->policy_description_domain = g_strdup (pd->elem_domain); str = NULL; } else @@ -865,6 +893,7 @@ _cdata (void *data, const char *s, int len) { g_free (pd->policy_message_nolang); pd->policy_message_nolang = str; + pd->policy_message_domain = g_strdup (pd->elem_domain); str = NULL; } else @@ -962,6 +991,8 @@ _end (void *data, const char *el) g_free (pd->elem_lang); pd->elem_lang = NULL; + g_free (pd->elem_domain); + pd->elem_domain = NULL; switch (pd->state) { @@ -993,7 +1024,9 @@ _end (void *data, const char *el) action->vendor_url = g_strdup (vendor_url); action->icon_name = g_strdup (icon_name); action->description = g_strdup (pd->policy_description_nolang); + action->description_domain = g_strdup (pd->policy_description_domain); action->message = g_strdup (pd->policy_message_nolang); + action->message_domain = g_strdup (pd->policy_message_domain); action->localized_description = pd->policy_descriptions; action->localized_message = pd->policy_messages; @@ -1098,6 +1131,7 @@ error: * _localize: * @translations: a mapping from xml:lang to the value, e.g. 'da' -> 'Smadre', 'en_CA' -> 'Punch, Aye!' * @untranslated: the untranslated value, e.g. 'Punch' + * @domain: the gettext domain for this string. Make be NULL. * @lang: the locale we're interested in, e.g. 'da_DK', 'da', 'en_CA', 'en_US'; basically just $LANG * with the encoding cut off. Maybe be NULL. * @@ -1108,11 +1142,18 @@ error: static const gchar * _localize (GHashTable *translations, const gchar *untranslated, + const gchar *domain, const gchar *lang) { const gchar *result; gchar lang2[256]; guint n; + + if (domain != NULL) + { + result = dgettext (domain, untranslated); + goto out; + } if (lang == NULL) { diff --git a/src/polkitbackend/polkitbackendinteractiveauthority.c b/src/polkitbackend/polkitbackendinteractiveauthority.c index 31e60df..b11cf31 100644 --- a/src/polkitbackend/polkitbackendinteractiveauthority.c +++ b/src/polkitbackend/polkitbackendinteractiveauthority.c @@ -412,8 +412,19 @@ polkit_backend_interactive_authority_enumerate_actions (PolkitBackendAuthority interactive_authority = POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority); priv = POLKIT_BACKEND_INTERACTIVE_AUTHORITY_GET_PRIVATE (interactive_authority); + /* Set LANG and locale so gettext() + friends work when running the code in the extensions */ + if (setlocale (LC_ALL, interactivee) == NULL) + { + g_printerr ("Invalid locale '%s'\n", interactivee); + } + g_setenv ("LANG", interactivee, TRUE); + actions = polkit_backend_action_pool_get_all_actions (priv->action_pool, interactivee); + /* Back to C! */ + setlocale (LC_ALL, "C"); + g_setenv ("LANG", "C", TRUE); + return actions; } @@ -1728,12 +1739,6 @@ get_localized_data_for_challenge (PolkitBackendInteractiveAuthority *authority, *out_localized_icon_name = NULL; *out_localized_details = NULL; - action_desc = polkit_backend_action_pool_get_action (priv->action_pool, - action_id, - locale); - if (action_desc == NULL) - goto out; - /* Set LANG and locale so gettext() + friends work when running the code in the extensions */ if (setlocale (LC_ALL, locale) == NULL) { @@ -1741,6 +1746,12 @@ get_localized_data_for_challenge (PolkitBackendInteractiveAuthority *authority, } g_setenv ("LANG", locale, TRUE); + action_desc = polkit_backend_action_pool_get_action (priv->action_pool, + action_id, + locale); + if (action_desc == NULL) + goto out; + /* call into extension points to get localized auth dialog data - the list is sorted by priority */ action_lookup_list = get_action_lookup_list (); for (l = action_lookup_list; l != NULL; l = l->next) @@ -1769,10 +1780,6 @@ get_localized_data_for_challenge (PolkitBackendInteractiveAuthority *authority, action_desc); } - /* Back to C! */ - setlocale (LC_ALL, "C"); - g_setenv ("LANG", "C", TRUE); - /* fall back to action description */ if (message == NULL) { @@ -1784,6 +1791,10 @@ get_localized_data_for_challenge (PolkitBackendInteractiveAuthority *authority, } out: + /* Back to C! */ + setlocale (LC_ALL, "C"); + g_setenv ("LANG", "C", TRUE); + if (message == NULL) message = g_strdup (""); if (icon_name == NULL) -- 1.7.1