Initial commit
[platform/upstream/glib2.0.git] / debian / patches / 01_gettext-desktopfiles.patch
1 # Description: If a .desktop file does not have inline translations, fall back to calling gettext.
2 # Ubuntu: https://launchpad.net/bugs/3935
3 # Upstream: http://bugzilla.gnome.org/show_bug.cgi?id=569829
4 Index: glib2.0-2.19.5/glib/gkeyfile.c
5 ===================================================================
6 --- glib2.0-2.19.5.orig/glib/gkeyfile.c 2009-01-30 15:34:03.000000000 +0100
7 +++ glib2.0-2.19.5/glib/gkeyfile.c      2009-01-30 15:34:32.000000000 +0100
8 @@ -83,6 +83,7 @@
9    GKeyFileFlags flags;
10  
11    gchar **locales;
12 +  gchar  *gettext_domain;
13  };
14  
15  typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
16 @@ -209,6 +210,7 @@
17    key_file->list_separator = ';';
18    key_file->flags = 0;
19    key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
20 +  key_file->gettext_domain = NULL;
21  }
22  
23  static void
24 @@ -228,6 +230,12 @@
25        key_file->parse_buffer = NULL;
26      }
27  
28 +  if (key_file->gettext_domain)
29 +    {
30 +       g_free (key_file->gettext_domain);
31 +       key_file->gettext_domain = NULL;
32 +    }
33 +
34    tmp = key_file->groups;
35    while (tmp != NULL)
36      {
37 @@ -448,6 +456,11 @@
38        return FALSE;
39      }
40  
41 +  key_file->gettext_domain = g_key_file_get_string (key_file,
42 +                                                    G_KEY_FILE_DESKTOP_GROUP,
43 +                                                    G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN,
44 +                                                    NULL);
45 +
46    return TRUE;
47  }
48  
49 @@ -554,6 +567,11 @@
50        return FALSE;
51      }
52  
53 +  key_file->gettext_domain = g_key_file_get_string (key_file,
54 +                                                    G_KEY_FILE_DESKTOP_GROUP,
55 +                                                    G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN,
56 +                                                    NULL);
57 +
58    return TRUE;
59  }
60  
61 @@ -1662,6 +1680,8 @@
62    GError *key_file_error;
63    gchar **languages;
64    gboolean free_languages = FALSE;
65 +  gboolean try_gettext = FALSE;
66 +  const gchar *msg_locale;
67    gint i;
68  
69    g_return_val_if_fail (key_file != NULL, NULL);
70 @@ -1692,6 +1712,23 @@
71        free_languages = FALSE;
72      }
73    
74 +  /* we're only interested in gettext translation if we don't have a
75 +   * translation in the .desktop file itself and if the key is one of the keys
76 +   * we know we want to translate: Name, GenericName, Comment.  Blindly doing
77 +   * this for all keys can give strange result for the icons, since the Icon is
78 +   * a locale string in the spec, eg.  We also only get translation in the mo
79 +   * file if the requested locale is the LC_MESSAGES one. Ideally, we should do
80 +   * more and change LC_MESSAGES to use the requested locale, but there's no
81 +   * guarantee it's installed on the system and it might have some
82 +   * side-effects.  Since this is a corner case, let's ignore it. */
83 +
84 +  msg_locale = setlocale (LC_MESSAGES, NULL);
85 +  try_gettext = msg_locale && key_file->gettext_domain &&
86 +                strcmp (group_name, G_KEY_FILE_DESKTOP_GROUP) == 0 &&
87 +                (strcmp (key, G_KEY_FILE_DESKTOP_KEY_NAME) == 0 ||
88 +                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME) == 0 ||
89 +                 strcmp (key, G_KEY_FILE_DESKTOP_KEY_COMMENT) == 0);
90 +
91    for (i = 0; languages[i]; i++)
92      {
93        candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
94 @@ -1708,6 +1745,39 @@
95        translated_value = NULL;
96     }
97  
98 +  /* Fallback to gettext */
99 +  if (try_gettext && !translated_value)
100 +    {
101 +      gchar *orig_value = g_key_file_get_string (key_file, group_name, key, NULL);
102 +
103 +      if (orig_value)
104 +        {
105 +          gboolean codeset_set;
106 +          const gchar *translated;
107 +          gboolean has_gettext;
108 +
109 +          codeset_set = bind_textdomain_codeset (key_file->gettext_domain, "UTF-8") != NULL;
110 +          translated = NULL;
111 +
112 +          translated = g_dgettext (key_file->gettext_domain,
113 +                                   orig_value);
114 +          has_gettext = translated != orig_value;
115 +
116 +          g_free (orig_value);
117 +
118 +          if (has_gettext)
119 +            {
120 +              if (codeset_set)
121 +                translated_value = g_strdup (translated);
122 +              else
123 +                translated_value = g_locale_to_utf8 (translated,
124 +                                                     -1, NULL, NULL, NULL);
125 +            }
126 +          else
127 +            translated_value = NULL;
128 +        }
129 +    }
130 +
131    /* Fallback to untranslated key
132     */
133    if (!translated_value)
134 Index: glib2.0-2.19.5/glib/gkeyfile.h
135 ===================================================================
136 --- glib2.0-2.19.5.orig/glib/gkeyfile.h 2009-01-30 15:34:03.000000000 +0100
137 +++ glib2.0-2.19.5/glib/gkeyfile.h      2009-01-30 15:34:32.000000000 +0100
138 @@ -240,6 +240,7 @@
139  #define G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY   "StartupNotify"
140  #define G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS "StartupWMClass"
141  #define G_KEY_FILE_DESKTOP_KEY_URL              "URL"
142 +#define G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN   "X-GNOME-Gettext-Domain"
143  
144  #define G_KEY_FILE_DESKTOP_TYPE_APPLICATION     "Application"
145  #define G_KEY_FILE_DESKTOP_TYPE_LINK            "Link"