[Evolution-hackers] [PATCH v2 2/2] evolution-data-server: the language for the day of the week or month in quoted messages should match the message language



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 second version of the patch introduces an auxiliary function,
so that the original generic function is not modified. Because of
such different approach (better style), a patch is also required
for the evolution code.

Signed-off-by: Guido Trentalancia <guido trentalancia net>
---
 libedataserver/e-data-server-util.c |   85 ++++++++++++++++++++++++++++++++++++
 libedataserver/e-data-server-util.h |    4 +
 2 files changed, 89 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 18:25:02.257488460 +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,90 @@ 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;
+       char *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 (%a and %A) in quoted messages, so
+        * that the language matches that of the quoting
+        * message.
+        */
+       if (!strcasecmp(fmt, "%a")) {
+               ctime = setlocale(LC_TIME, NULL);
+               saved_locale = strdup(ctime);
+               cmessages = setlocale(LC_MESSAGES, NULL);
+               setlocale (LC_TIME, cmessages);
+       }
+
+       locale_fmt = g_locale_from_utf8 (fmt, -1, NULL, &sz, NULL);
+       if (!locale_fmt)
+               return 0;
+
+       ret = e_strftime (string, max, locale_fmt, tm);
+
+       /* Restore LC_TIME, if it has been changed to match
+        * LC_MESSAGES.
+        */
+       if (!strcasecmp(fmt, "%a")) {
+               setlocale (LC_TIME, saved_locale);
+               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,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]