File: 01_gettext-desktopfiles.patch

package info (click to toggle)
glib2.0 2.58.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 48,444 kB
  • sloc: ansic: 452,195; xml: 16,781; python: 6,149; makefile: 3,776; sh: 1,499; perl: 1,140; cpp: 9
file content (171 lines) | stat: -rw-r--r-- 6,456 bytes parent folder | download | duplicates (3)
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
From: Debian GNOME Maintainers
 <pkg-gnome-maintainers@lists.alioth.debian.org>
Date: Wed, 13 Jun 2007 10:52:27 +0200
Subject: If a .desktop file does not have inline translations,
 fall back to calling gettext

Patch from OpenSUSE via Ubuntu, original author unknown. Martin Pitt and
Vincent Untz appear to be the main authors.

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=569829
Bug-Ubuntu: https://launchpad.net/bugs/3935
Applied-upstream: no, rejected because "this will be solved soon" (in 2013)
---
 glib/gkeyfile.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 glib/gkeyfile.h |  4 ++++
 2 files changed, 76 insertions(+)

diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index 4e9b535..dc06616 100644
--- a/glib/gkeyfile.c
+++ b/glib/gkeyfile.c
@@ -511,6 +511,7 @@ struct _GKeyFile
   GKeyFileFlags flags;
 
   gchar **locales;
+  gchar  *gettext_domain;
 
   volatile gint ref_count;
 };
@@ -635,6 +636,7 @@ g_key_file_init (GKeyFile *key_file)
   key_file->list_separator = ';';
   key_file->flags = 0;
   key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
+  key_file->gettext_domain = NULL;
 }
 
 static void
@@ -654,6 +656,12 @@ g_key_file_clear (GKeyFile *key_file)
       key_file->parse_buffer = NULL;
     }
 
+  if (key_file->gettext_domain)
+    {
+       g_free (key_file->gettext_domain);
+       key_file->gettext_domain = NULL;
+    }
+
   tmp = key_file->groups;
   while (tmp != NULL)
     {
@@ -873,6 +881,11 @@ g_key_file_load_from_fd (GKeyFile       *key_file,
       return FALSE;
     }
 
+  key_file->gettext_domain = g_key_file_get_string (key_file,
+                                                    G_KEY_FILE_DESKTOP_GROUP,
+                                                    G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN,
+                                                    NULL);
+
   return TRUE;
 }
 
@@ -929,6 +942,11 @@ g_key_file_load_from_file (GKeyFile       *key_file,
       return FALSE;
     }
 
+  key_file->gettext_domain = g_key_file_get_string (key_file,
+                                                    G_KEY_FILE_DESKTOP_GROUP,
+                                                    G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN,
+                                                    NULL);
+
   return TRUE;
 }
 
@@ -2212,6 +2230,8 @@ g_key_file_get_locale_string (GKeyFile     *key_file,
   GError *key_file_error;
   gchar **languages;
   gboolean free_languages = FALSE;
+  gboolean try_gettext = FALSE;
+  const gchar *msg_locale;
   gint i;
 
   g_return_val_if_fail (key_file != NULL, NULL);
@@ -2233,6 +2253,25 @@ g_key_file_get_locale_string (GKeyFile     *key_file,
       free_languages = FALSE;
     }
   
+  /* we're only interested in gettext translation if we don't have a
+   * translation in the .desktop file itself and if the key is one of the keys
+   * we know we want to translate: Name, GenericName, Comment, Keywords.
+   * Blindly doing this for all keys can give strange result for the icons,
+   * since the Icon is a locale string in the spec, eg.  We also only get
+   * translation in the mo file if the requested locale is the LC_MESSAGES one.
+   * Ideally, we should do more and change LC_MESSAGES to use the requested
+   * locale, but there's no guarantee it's installed on the system and it might
+   * have some side-effects.  Since this is a corner case, let's ignore it. */
+  msg_locale = setlocale (LC_MESSAGES, NULL);
+  try_gettext = msg_locale && key_file->gettext_domain &&
+                (strcmp (group_name, G_KEY_FILE_DESKTOP_GROUP) == 0 ||
+                 g_str_has_prefix (group_name, G_KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX)) &&
+                (strcmp (key, G_KEY_FILE_DESKTOP_KEY_NAME) == 0 ||
+                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_FULLNAME) == 0 ||
+                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME) == 0 ||
+                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_KEYWORDS) == 0 ||
+                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_COMMENT) == 0);
+
   for (i = 0; languages[i]; i++)
     {
       candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
@@ -2249,6 +2288,39 @@ g_key_file_get_locale_string (GKeyFile     *key_file,
       translated_value = NULL;
    }
 
+  /* Fallback to gettext */
+  if (try_gettext && !translated_value)
+    {
+      gchar *orig_value = g_key_file_get_string (key_file, group_name, key, NULL);
+
+      if (orig_value)
+        {
+          gboolean codeset_set;
+          const gchar *translated;
+          gboolean has_gettext;
+
+          codeset_set = bind_textdomain_codeset (key_file->gettext_domain, "UTF-8") != NULL;
+          translated = NULL;
+
+          translated = g_dgettext (key_file->gettext_domain,
+                                   orig_value);
+          has_gettext = translated != orig_value;
+
+          g_free (orig_value);
+
+          if (has_gettext)
+            {
+              if (codeset_set)
+                translated_value = g_strdup (translated);
+              else
+                translated_value = g_locale_to_utf8 (translated,
+                                                     -1, NULL, NULL, NULL);
+            }
+          else
+            translated_value = NULL;
+        }
+    }
+
   /* Fallback to untranslated key
    */
   if (!translated_value)
diff --git a/glib/gkeyfile.h b/glib/gkeyfile.h
index 7a10048..aef9113 100644
--- a/glib/gkeyfile.h
+++ b/glib/gkeyfile.h
@@ -298,6 +298,7 @@ gboolean  g_key_file_remove_group           (GKeyFile             *key_file,
 
 /* Defines for handling freedesktop.org Desktop files */
 #define G_KEY_FILE_DESKTOP_GROUP                "Desktop Entry"
+#define G_KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX  "Desktop Action"
 
 #define G_KEY_FILE_DESKTOP_KEY_TYPE             "Type"
 #define G_KEY_FILE_DESKTOP_KEY_VERSION          "Version"
@@ -320,6 +321,9 @@ gboolean  g_key_file_remove_group           (GKeyFile             *key_file,
 #define G_KEY_FILE_DESKTOP_KEY_URL              "URL"
 #define G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE "DBusActivatable"
 #define G_KEY_FILE_DESKTOP_KEY_ACTIONS          "Actions"
+#define G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN   "X-GNOME-Gettext-Domain"
+#define G_KEY_FILE_DESKTOP_KEY_FULLNAME         "X-GNOME-FullName"
+#define G_KEY_FILE_DESKTOP_KEY_KEYWORDS         "Keywords"
 
 #define G_KEY_FILE_DESKTOP_TYPE_APPLICATION     "Application"
 #define G_KEY_FILE_DESKTOP_TYPE_LINK            "Link"