Description: Display names of days and months in the current language
Bug-Ubuntu: http://bugs.launchpad.net/bugs/1072019
Forwarded: https://bugzilla.gnome.org/show_bug.cgi?id=687945
Author: Gunnar Hjalmarsson <gunnarhj@ubuntu.com>
Last-Update: 2012-11-14

Index: glib2.0-2.34.2/glib/gdatetime.c
===================================================================
--- glib2.0-2.34.2.orig/glib/gdatetime.c	2012-08-17 06:18:08.000000000 +0200
+++ glib2.0-2.34.2/glib/gdatetime.c	2012-11-14 06:23:09.554580208 +0100
@@ -353,6 +353,11 @@
 
 #endif  /* HAVE_LANGINFO_TIME */
 
+/* Conversion of names of weekdays and months */
+#if defined (HAVE_NEWLOCALE) && defined (HAVE_USELOCALE)
+#define USE_XLOCALE 1
+#endif
+
 static inline gint
 ymd_to_days (gint year,
              gint month,
@@ -2167,6 +2172,40 @@
   return success;
 }
 
+/* g_date_time_format() subroutine that makes the locale for language
+ * be used when replacing a conversion specification. Applicable to
+ * %a, %A, %b, %B, and %h, i.e. names of days and months.
+ */
+static locale_t
+g_date_time_use_locale_for_language ()
+{
+#ifdef USE_XLOCALE
+  static locale_t locale_obj;
+  static gboolean is_warning = FALSE;
+
+  if (is_warning)
+    return NULL;
+
+  if (!locale_obj) {
+    const gchar *lang_locale = getenv ("LC_ALL");
+    if (lang_locale == NULL || strlen (lang_locale) == 0)
+      lang_locale = getenv ("LC_MESSAGES");
+    if (lang_locale == NULL || strlen (lang_locale) == 0)
+      lang_locale = getenv ("LANG");
+    if (lang_locale == NULL || strlen (lang_locale) == 0) {
+      g_warning ("LC_MESSAGES or LANG not set -- couldn't apply locale for language");
+      is_warning = TRUE;
+      return NULL;
+    }
+    locale_obj = newlocale (LC_TIME_MASK, lang_locale, NULL);
+  }
+
+  return uselocale (locale_obj);
+#else
+  return NULL;
+#endif
+}
+
 /* g_date_time_format() subroutine that takes a UTF-8 format
  * string and produces a locale-encoded date/time string.
  */
@@ -2183,6 +2222,7 @@
   gboolean  pad_set = FALSE;
   gchar    *pad = "";
   gchar    *ampm;
+  locale_t  prev_locale;
   const gchar *tz;
 
   while (*format)
@@ -2220,16 +2260,24 @@
       switch (c)
 	{
 	case 'a':
+	  prev_locale = g_date_time_use_locale_for_language ();
 	  g_string_append (outstr, WEEKDAY_ABBR (datetime));
+	  uselocale (prev_locale);
 	  break;
 	case 'A':
+	  prev_locale = g_date_time_use_locale_for_language ();
 	  g_string_append (outstr, WEEKDAY_FULL (datetime));
+	  uselocale (prev_locale);
 	  break;
 	case 'b':
+	  prev_locale = g_date_time_use_locale_for_language ();
 	  g_string_append (outstr, MONTH_ABBR (datetime));
+	  uselocale (prev_locale);
 	  break;
 	case 'B':
+	  prev_locale = g_date_time_use_locale_for_language ();
 	  g_string_append (outstr, MONTH_FULL (datetime));
+	  uselocale (prev_locale);
 	  break;
 	case 'c':
 	  {
@@ -2265,7 +2313,9 @@
 			 g_date_time_get_week_numbering_year (datetime));
 	  break;
 	case 'h':
+	  prev_locale = g_date_time_use_locale_for_language ();
 	  g_string_append (outstr, MONTH_ABBR (datetime));
+	  uselocale (prev_locale);
 	  break;
 	case 'H':
 	  format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
@@ -2472,28 +2522,33 @@
  * many formats is locale-dependent and may not match the strftime()
  * output exactly.
  *
+ * Also, unlike strftime(), this function replaces conversions for names
+ * of weekdays and months using the locale of the environment variable
+ * that determines the display language for menus and messages. This
+ * behavior implies that uselocale() was present when GLib was compiled.
+ *
  * The following format specifiers are supported:
  *
  * <variablelist>
  *  <varlistentry><term>
  *    <literal>\%a</literal>:
  *   </term><listitem><simpara>
- *    the abbreviated weekday name according to the current locale
+ *    the abbreviated weekday name according to the locale for the display language
  *  </simpara></listitem></varlistentry>
  *  <varlistentry><term>
  *    <literal>\%A</literal>:
  *   </term><listitem><simpara>
- *    the full weekday name according to the current locale
+ *    the full weekday name according to the locale for the display language
  *  </simpara></listitem></varlistentry>
  *  <varlistentry><term>
  *    <literal>\%b</literal>:
  *   </term><listitem><simpara>
- *    the abbreviated month name according to the current locale
+ *    the abbreviated month name according to the locale for the display language
  *  </simpara></listitem></varlistentry>
  *  <varlistentry><term>
  *    <literal>\%B</literal>:
  *   </term><listitem><simpara>
- *    the full month name according to the current locale
+ *    the full month name according to the locale for the display language
  *  </simpara></listitem></varlistentry>
  *  <varlistentry><term>
  *    <literal>\%c</literal>:

