When quoting a message in a reply, make sure that the language for the
day of the week matches the language of the quoting text generated by
evolution.

Do the same for other similar strings such as the name of the month
(not being actively used in the application yet).

A bug is triggered, for example, when LC_MESSAGE is set to "C" (not
infrequent at all) and the other locale variables are set to the user
first language.

This third version of the patch introduces an auxiliary function,
so that the original generic function is not modified and also
includes fixes as from a review by Milan Crha (Redhat).

A patch is also required for the evolution code.

Signed-off-by: Guido Trentalancia <guido@trentalancia.net>
---
 libedataserver/e-data-server-util.c |   84 ++++++++++++++++++++++++++++++++++++
 libedataserver/e-data-server-util.h |    4 +
 2 files changed, 88 insertions(+)

diff -pru evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.c evolution-data-server-3.20.1/libedataserver/e-data-server-util.c
--- evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.c	2016-04-15 16:46:52.220964656 +0200
+++ evolution-data-server-3.20.1/libedataserver/e-data-server-util.c	2016-04-15 19:49:48.920640643 +0200
@@ -25,6 +25,7 @@
 #include <sys/stat.h>
 #include <time.h>
 #include <unistd.h>
+#include <locale.h>
 
 #ifdef G_OS_WIN32
 #include <mbstring.h>
@@ -766,6 +767,89 @@ e_utf8_strftime (gchar *string,
 	if (!ret) {
 		g_free (locale_fmt);
 		return 0;
+	}
+
+	buf = g_locale_to_utf8 (string, ret, NULL, &sz, NULL);
+	if (!buf) {
+		g_free (locale_fmt);
+		return 0;
+	}
+
+	if (sz >= max) {
+		gchar *tmp = buf + max - 1;
+		tmp = g_utf8_find_prev_char (buf, tmp);
+		if (tmp)
+			sz = tmp - buf;
+		else
+			sz = 0;
+	}
+
+	memcpy (string, buf, sz);
+	string[sz] = '\0';
+
+	g_free (locale_fmt);
+	g_free (buf);
+
+	return sz;
+}
+
+/**
+ * e_utf8_strftime_matched_lang:
+ * @string: The string array to store the result in.
+ * @max: The size of array @s.
+ * @fmt: The formatting to use on @tm.
+ * @tm: The time value to format.
+ *
+ * The UTF-8 equivalent of e_strftime (), which
+ * also makes sure that the language for the day of
+ * the week or month matches the application
+ * language (used in the string produced when
+ * quoting messages).
+ *
+ * Returns: The number of characters placed in @s.
+ **/
+gsize
+e_utf8_strftime_matched_lang (gchar *string,
+                              gsize max,
+                              const gchar *fmt,
+                              const struct tm *tm)
+{
+	gsize sz, ret;
+	gchar *locale_fmt, *buf;
+	gchar *ctime, *cmessages, *saved_locale;
+
+	g_return_val_if_fail (string != NULL, 0);
+	g_return_val_if_fail (fmt != NULL, 0);
+	g_return_val_if_fail (tm != NULL, 0);
+
+	/* Use LC_MESSAGES instead of LC_TIME for the day
+	 * of the week or month in quoted messages, so
+	 * that the language matches that of the quoting
+	 * message.
+	 */
+	ctime = setlocale(LC_TIME, NULL);
+	saved_locale = strdup(ctime);
+	g_return_val_if_fail (saved_locale != NULL, 0);
+	cmessages = setlocale(LC_MESSAGES, NULL);
+	setlocale (LC_TIME, cmessages);
+
+	locale_fmt = g_locale_from_utf8 (fmt, -1, NULL, &sz, NULL);
+	if (!locale_fmt) {
+		g_free(saved_locale);
+		return 0;
+	}
+
+	ret = e_strftime (string, max, locale_fmt, tm);
+
+	/* Restore LC_TIME, if it has been changed to match
+	 * LC_MESSAGES.
+	 */
+	setlocale (LC_TIME, saved_locale);
+	g_free(saved_locale);
+
+	if (!ret) {
+		g_free (locale_fmt);
+		return 0;
 	}
 
 	buf = g_locale_to_utf8 (string, ret, NULL, &sz, NULL);
diff -pru evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.h evolution-data-server-3.20.1/libedataserver/e-data-server-util.h
--- evolution-data-server-3.20.1-orig/libedataserver/e-data-server-util.h	2016-04-15 18:09:04.331803077 +0200
+++ evolution-data-server-3.20.1/libedataserver/e-data-server-util.h	2016-04-15 18:15:15.759723851 +0200
@@ -72,6 +72,10 @@ gsize		e_utf8_strftime			(gchar *string,
 						 gsize max,
 						 const gchar *fmt,
 						 const struct tm *tm);
+gsize		e_utf8_strftime_matched_lang	(gchar *string,
+						 gsize max,
+						 const gchar *fmt,
+						 const struct tm *tm);
 gsize		e_strftime			(gchar *string,
 						 gsize max,
 						 const gchar *fmt,
