[glom] Use the LC_TIME environment variable to get translated date formats.



commit a9c730541705fbbbe90ee6333d090927c47c6e5c
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Jan 16 09:27:08 2015 +0100

    Use the LC_TIME environment variable to get translated date formats.
    
    So we have the correct 4 digit date formats for those too,
    even where the default behaviour is incorrect.
    
    Also add a regression test for this.
    
    Bug #742968

 Makefile_tests.am                              |    4 ++-
 configure.ac                                   |    1 +
 glom/libglom/data_structure/glomconversions.cc |   47 ++++++++++++++++++++++--
 tests/test_glom_date_in_locales_lc_time.sh.in  |   41 +++++++++++++++++++++
 4 files changed, 88 insertions(+), 5 deletions(-)
---
diff --git a/Makefile_tests.am b/Makefile_tests.am
index f7218a8..6e2cb9b 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -112,6 +112,7 @@ dist_noinst_SCRIPTS = tests/test_dtd_file_validation.sh \
        tests/test_xslt_file_validation.sh \
        tests/test_glade_toplevels_instantiation.sh \
        tests/test_glom_date_in_locales.sh.in \
+       tests/test_glom_date_in_locales_lc_time.sh.in \
        tests/test_selfhosting_new_from_example_in_locales.sh \
        tests/test_document_load_and_save_all.sh \
        tests/test_field_file_format_in_locales.sh
@@ -388,7 +389,8 @@ if GLOM_ENABLE_UI_TESTS
 TESTS += \
        tests/test_glade_toplevels_instantiation.sh \
        tests/test_glade_derived_instantiation \
-       tests/test_glom_date_in_locales.sh
+       tests/test_glom_date_in_locales.sh \
+       tests/test_glom_date_in_locales_lc_time.sh
 endif #GLOM_ENABLE_UI_TESTS
 
 
diff --git a/configure.ac b/configure.ac
index dbdad92..6e2992c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -406,5 +406,6 @@ AC_CONFIG_FILES([Makefile
                  win32/glom.iss])
 
 AC_CONFIG_FILES([tests/test_glom_date_in_locales.sh], [chmod u+x tests/test_glom_date_in_locales.sh])
+AC_CONFIG_FILES([tests/test_glom_date_in_locales_lc_time.sh], [chmod u+x 
tests/test_glom_date_in_locales_lc_time.sh])
 
 AC_OUTPUT
diff --git a/glom/libglom/data_structure/glomconversions.cc b/glom/libglom/data_structure/glomconversions.cc
index 52dc0a2..7716273 100644
--- a/glom/libglom/data_structure/glomconversions.cc
+++ b/glom/libglom/data_structure/glomconversions.cc
@@ -88,10 +88,39 @@ static inline const char* glom_get_locale_date_format()
 {
   if(!c_locale_date_format)
   {
-    //TODO: Find some way to get the %x translation that's appropriate for the value of LC_NUMERIC in the 
locale,
-    //because this just assumes that the main locale name will be correct,
-    //so this can fail, for instance, if gettext() is looking for the translation for en_US.UTF-8
-    //but LC_NUMERIC is set to en_GB.UTF-8 (which uses 2 digits when using %x).
+    //Get the current LC_TIME value:
+    //We copy the string because setlocale() probably returns the same array
+    //each time.
+    //Even when the LC_TIME environment variable is not set, we still seem
+    //to get a useful value here, based on LC_ALL, for instance.
+    char* lc_time = setlocale(LC_TIME, NULL);
+    if(lc_time)
+      lc_time = g_strdup(lc_time);
+
+    std::cout << "DEBUG: LC_TIME: " << lc_time << std::endl;
+
+    char* old_lc_messages = 0;
+    bool changed_lc_messages = false;
+
+    if(lc_time)
+    {
+      //We call setlocale() so we get the %x translation that's appropriate for the
+      //value of LC_TIME's locale,
+      //Otherwise _() would fail if, for instance, gettext() just looked for the
+      //translation for en_US.UTF-8, but LC_NUMERIC was set to en_GB.UTF-8
+      //(which uses 2 digits when using %x).
+
+      //Get the current LC_MESSAGES value:
+      old_lc_messages = g_strdup(setlocale(LC_MESSAGES, NULL));
+      if(old_lc_messages)
+        old_lc_messages = g_strdup(old_lc_messages);
+
+      //Change LC_MESSAGES to change how gettext works():
+      if(g_strcmp0(lc_time, old_lc_messages) != 0) {
+        setlocale(LC_MESSAGES, lc_time);
+        changed_lc_messages = true;
+      }
+    }
 
     /* TRANSLATORS: Please only translate this string if you know that strftime() 
      * shows only 2 year digits when using format "x". We want to always display 
@@ -102,6 +131,16 @@ static inline const char* glom_get_locale_date_format()
      * Thanks.
      * xgettext:no-c-format */
     c_locale_date_format = _("%x");
+
+
+    if(changed_lc_messages)
+    {
+      //Change LC_MESSAGES back:
+      setlocale(LC_MESSAGES, old_lc_messages);
+    }
+
+    g_free(old_lc_messages);
+    g_free(lc_time);
   }
 
   //std::cout << G_STRFUNC << ": c_locale_date_format=" << c_locale_date_format << std::endl;
diff --git a/tests/test_glom_date_in_locales_lc_time.sh.in b/tests/test_glom_date_in_locales_lc_time.sh.in
new file mode 100644
index 0000000..a0ff71b
--- /dev/null
+++ b/tests/test_glom_date_in_locales_lc_time.sh.in
@@ -0,0 +1,41 @@
+#/bin/sh -e
+
+# This test checks that dates are presented and interpreted correctly,
+# even when the LC_TIME's locale would need to use a translation of the "%x" date
+# format from .po file, but the main locale doesn't need a translation of the "%x"
+# date format.
+#
+# For instance, LANG="en_US.UTF-8" (uses "%x", giving 4 digit years),
+# with LC_TIME="es_ES.UTF-8" (should not use "%x", because it gives 2 digit years).
+#
+# This test requires these locales to be installed and configured.
+# That might be a problem on some systems, so feel free to use a patch to edit this file or disable it 
altogether.
+#
+# These are chosen based on problems found previously,
+# and the ones with good translations shown here: http://l10n.gnome.org/module/glom/
+# TODO: Get a list from po/*.po ?
+#
+# On debian/Ubuntu do this: 
+#  sudo apt-get install language-pack-de language-pack-es language-pack-fi language-pack-fr language-pack-hu 
language-pack-it language-pack-pt language-pack-sl language-pack-da language-pack-cs language-pack-nb 
language-pack-sv
+#
+# These are apparently not available on Fedora:  "da_DK.UTF-8" "cs_CZ.UTF-8" "nb_NO.UTF-8" "sv_SE.UTF-8"
+
+function test_date {
+  lang="$1"
+  lc_time="$2"       
+
+  echo testing with LANG="$lang" and LC_TIME="$lc_time"
+  export LANG="$lang"
+
+  # We don't set LANGUAGE because that seems to affect, for instance, printf(),
+  # ignoring LC_TIME. Maybe we need to handle that too.
+  #  export LANGUAGE="$lang"
+  export LC_TIME="$lc_time"
+
+  @abs_top_builddir@/glom/glom --debug-date-check || exit 1
+}
+
+test_date "en_US.UTF-8" "en_GB.UTF-8"
+test_date "en_US.UTF-8" "es_ES.UTF-8"
+
+


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