1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
From: Robert Ancell <robert.ancell@canonical.com>
Date: Wed, 18 Aug 2010 16:26:15 +1000
Subject: Use gettext for translations in .policy files
Bug: http://bugs.freedesktop.org/show_bug.cgi?id=29639
Bug-Ubuntu: https://launchpad.net/bugs/619632
---
src/polkitbackend/polkitbackendactionpool.c | 49 +++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/src/polkitbackend/polkitbackendactionpool.c b/src/polkitbackend/polkitbackendactionpool.c
index 4270d4e..e2dbf9e 100644
--- a/src/polkitbackend/polkitbackendactionpool.c
+++ b/src/polkitbackend/polkitbackendactionpool.c
@@ -24,6 +24,8 @@
#include <pwd.h>
#include <string.h>
#include <expat.h>
+#include <locale.h>
+#include <glib/gi18n.h>
#include <polkit/polkit.h>
#include <polkit/polkitprivate.h>
@@ -44,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;
@@ -65,7 +69,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);
@@ -85,6 +91,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
@@ -385,9 +392,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,
@@ -603,11 +612,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;
@@ -629,8 +643,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);
@@ -650,6 +668,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
@@ -737,6 +757,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)
@@ -745,6 +769,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)
@@ -847,6 +875,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
@@ -863,6 +892,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
@@ -960,6 +990,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)
{
@@ -990,7 +1022,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;
@@ -1093,6 +1127,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.
*
@@ -1103,11 +1138,25 @@ 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)
+ {
+ gchar *old_locale;
+
+ old_locale = g_strdup (setlocale (LC_ALL, NULL));
+ setlocale (LC_ALL, lang);
+ result = dgettext (domain, untranslated);
+ setlocale (LC_ALL, old_locale);
+ g_free (old_locale);
+
+ goto out;
+ }
if (lang == NULL)
{
|