[california] Localize Week view day header labels: Bug #731723



commit 5b7a27a79bb3e84392b9d1bd4252176184e4cd9c
Author: Jim Nelson <jim yorba org>
Date:   Wed Jun 18 18:26:39 2014 -0700

    Localize Week view day header labels: Bug #731723
    
    The ordering of the Week view day header labels needs to be available
    for localization.
    
    Because strftime() returns unsightly leading zeroes for some of the
    new formats, a utility method to strip them (and some tests) are
    included.

 src/Makefile.am                 |    1 +
 src/calendar/calendar-date.vala |   13 ++++++++++-
 src/calendar/calendar.vala      |   21 ++++++++++++++++++
 src/tests/tests-string.vala     |   44 +++++++++++++++++++++++++++++++++++++++
 src/tests/tests.vala            |    1 +
 src/util/util-string.vala       |   36 +++++++++++++++++++++++++++++++
 src/view/week/week-grid.vala    |    6 +++-
 7 files changed, 119 insertions(+), 3 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 2b50cad..7498275 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -107,6 +107,7 @@ california_VALASOURCES = \
        tests/tests-calendar-month-span.vala \
        tests/tests-calendar-wall-time.vala \
        tests/tests-quick-add.vala \
+       tests/tests-string.vala \
        \
        toolkit/toolkit.vala \
        toolkit/toolkit-button-connector.vala \
diff --git a/src/calendar/calendar-date.vala b/src/calendar/calendar-date.vala
index ce762f0..3181742 100644
--- a/src/calendar/calendar-date.vala
+++ b/src/calendar/calendar-date.vala
@@ -37,6 +37,10 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
          */
         ABBREV,
         /**
+         * Indicates the returned string should be as compact as possible (implies { link ABBREV}.
+         */
+        COMPACT,
+        /**
          * Indicates that the year should be included in the return date string.
          */
         INCLUDE_YEAR,
@@ -338,6 +342,7 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
      * or INCLUDE_YEAR flag is specified.
      */
     public string to_pretty_string(PrettyFlag flags) {
+        bool compact = (flags & PrettyFlag.COMPACT) != 0;
         bool abbrev = (flags & PrettyFlag.ABBREV) != 0;
         bool with_year = (flags & PrettyFlag.INCLUDE_YEAR) != 0;
         bool no_today = (flags & PrettyFlag.NO_TODAY) != 0;
@@ -352,6 +357,11 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
                 fmt = with_year ? FMT_PRETTY_DATE_ABBREV_NO_DOW : FMT_PRETTY_DATE_ABBREV_NO_DOW_NO_YEAR;
             else
                 fmt = with_year ? FMT_PRETTY_DATE_ABBREV : FMT_PRETTY_DATE_ABBREV_NO_YEAR;
+        } else if (compact) {
+            if (no_dow)
+                fmt = with_year ? FMT_PRETTY_DATE_COMPACT_NO_DOW : FMT_PRETTY_DATE_COMPACT_NO_DOW_NO_YEAR;
+            else
+                fmt = with_year ? FMT_PRETTY_DATE_COMPACT : FMT_PRETTY_DATE_COMPACT_NO_YEAR;
         } else {
             if (no_dow)
                 fmt = with_year ? FMT_PRETTY_DATE_NO_DOW : FMT_PRETTY_DATE_NO_DOW_NO_YEAR;
@@ -359,7 +369,8 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
                 fmt = with_year ? FMT_PRETTY_DATE : FMT_PRETTY_DATE_NO_YEAR;
         }
         
-        return String.reduce_whitespace(format(fmt));
+        // Return string with leading zeros (from common separators) and extraneous whitespace removed
+        return String.reduce_whitespace(String.remove_leading_chars(format(fmt), '0', " /-:;,."));
     }
     
     public override string to_string() {
diff --git a/src/calendar/calendar.vala b/src/calendar/calendar.vala
index 70866ab..96fa49a 100644
--- a/src/calendar/calendar.vala
+++ b/src/calendar/calendar.vala
@@ -59,6 +59,10 @@ private unowned string FMT_PRETTY_DATE_NO_DOW;
 private unowned string FMT_PRETTY_DATE_ABBREV_NO_DOW;
 private unowned string FMT_PRETTY_DATE_NO_DOW_NO_YEAR;
 private unowned string FMT_PRETTY_DATE_ABBREV_NO_DOW_NO_YEAR;
+private unowned string FMT_PRETTY_DATE_COMPACT;
+private unowned string FMT_PRETTY_DATE_COMPACT_NO_YEAR;
+private unowned string FMT_PRETTY_DATE_COMPACT_NO_DOW;
+private unowned string FMT_PRETTY_DATE_COMPACT_NO_DOW_NO_YEAR;
 private unowned string FMT_AM;
 private unowned string FMT_BRIEF_AM;
 private unowned string FMT_PM;
@@ -106,6 +110,7 @@ public void init() throws Error {
     FMT_DAY_OF_WEEK_FULL = "%A";
     FMT_DAY_OF_WEEK_ABBREV = "%a";
     FMT_FULL_DATE = "%x";
+    FMT_PRETTY_DATE_COMPACT_NO_DOW = "%x";
     
     // The month and year according to locale preferences, i.e. "March 2014"
     // See http://www.cplusplus.com/reference/ctime/strftime/ for format reference
@@ -150,6 +155,22 @@ public void init() throws Error {
     // See http://www.cplusplus.com/reference/ctime/strftime/ for format reference
     FMT_PRETTY_DATE_ABBREV_NO_DOW_NO_YEAR = _("%b %e");
     
+    // A "pretty" date compacted according to locale preferences, i.e. "Mon 3/10/2014"
+    // Leading zeroes will be stripped.
+    // See http://www.cplusplus.com/reference/ctime/strftime/ for format reference
+    FMT_PRETTY_DATE_COMPACT = _("%a %x");
+    
+    // A "pretty" date abbreviated and no year according to locale preferences, i.e. "Mon 3/10"
+    // Leading zeroes will be stripped.
+    // See http://www.cplusplus.com/reference/ctime/strftime/ for format reference
+    FMT_PRETTY_DATE_COMPACT_NO_YEAR = _("%a %m/%d");
+    
+    // A "pretty" date abbreviated with no day of week or year according to locale preferences,
+    // i.e. "3/10"
+    // Leading zeroes will be stripped.
+    // See http://www.cplusplus.com/reference/ctime/strftime/ for format reference
+    FMT_PRETTY_DATE_COMPACT_NO_DOW_NO_YEAR = _("%m/%d");
+    
     // Ante meridiem
     // (Please translate even if 24-hour clock used in your locale; this allows for GNOME time
     // format user settings to be honored)
diff --git a/src/tests/tests-string.vala b/src/tests/tests-string.vala
new file mode 100644
index 0000000..436cf81
--- /dev/null
+++ b/src/tests/tests-string.vala
@@ -0,0 +1,44 @@
+/* Copyright 2014 Yorba Foundation
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+namespace California.Tests {
+
+private class String : UnitTest.Harness {
+    public String() {
+        add_case("strip-zeroes-space", strip_zeroes_space);
+        add_case("strip-zeroes-slash", strip_zeroes_slash);
+        add_case("strip-zeroes-multiple", strip_zeroes_multiple);
+    }
+    
+    protected override void setup() throws Error {
+        Util.init();
+    }
+    
+    protected override void teardown() {
+        Util.terminate();
+    }
+    
+    private bool strip_zeroes_space() throws Error {
+        string result = California.String.remove_leading_chars("01 2 03 4", '0');
+        
+        return result == "1 2 3 4";
+    }
+    
+    private bool strip_zeroes_slash() throws Error {
+        string result = California.String.remove_leading_chars("01/2/03/4", '0', " /");
+        
+        return result == "1/2/3/4";
+    }
+    
+    private bool strip_zeroes_multiple() throws Error {
+        string result = California.String.remove_leading_chars("001/2/03/4", '0', " /");
+        
+        return result == "1/2/3/4";
+    }
+}
+
+}
+
diff --git a/src/tests/tests.vala b/src/tests/tests.vala
index 1d484b0..c85530b 100644
--- a/src/tests/tests.vala
+++ b/src/tests/tests.vala
@@ -7,6 +7,7 @@
 namespace California.Tests {
 
 public int run(string[] args) {
+    UnitTest.Harness.register(new String());
     UnitTest.Harness.register(new QuickAdd());
     UnitTest.Harness.register(new CalendarDate());
     UnitTest.Harness.register(new CalendarMonthSpan());
diff --git a/src/util/util-string.vala b/src/util/util-string.vala
index b9561af..05b9c33 100644
--- a/src/util/util-string.vala
+++ b/src/util/util-string.vala
@@ -69,5 +69,41 @@ public bool is_numeric(string? str) {
     return true;
 }
 
+/**
+ * Removes leading characters from throughout the string.
+ *
+ * Both the leading character and what constitutes tokens can be specified.
+ *
+ * Results are undefined if the leading character is also found in the delimiter string.
+ */
+public string? remove_leading_chars(string? str, unichar ch, string delims = " ") {
+    if (is_empty(str))
+        return str;
+    
+    StringBuilder builder = new StringBuilder();
+    unichar current_ch;
+    int index = 0;
+    bool leading = true;
+    while (str.get_next_char(ref index, out current_ch)) {
+        // if character is a delimiter, reset state, append, and move on
+        if (delims.index_of_char(current_ch) >= 0) {
+            leading = true;
+            builder.append_unichar(current_ch);
+            
+            continue;
+        }
+        
+        // if looking for leading characters and this matches, drop on the floor
+        if (leading && current_ch == ch)
+            continue;
+        
+        // done looking for leading characters until the next delimiter
+        leading = false;
+        builder.append_unichar(current_ch);
+    }
+    
+    return builder.str;
+}
+
 }
 
diff --git a/src/view/week/week-grid.vala b/src/view/week/week-grid.vala
index f38ea4b..cdc4cd7 100644
--- a/src/view/week/week-grid.vala
+++ b/src/view/week/week-grid.vala
@@ -25,6 +25,9 @@ namespace California.View.Week {
 internal class Grid : Gtk.Box {
     public const string PROP_WEEK = "week";
     
+    private const Calendar.Date.PrettyFlag DATE_LABEL_FLAGS =
+        Calendar.Date.PrettyFlag.COMPACT | Calendar.Date.PrettyFlag.NO_TODAY;
+    
     public weak Controller owner { get; private set; }
     
     /**
@@ -98,8 +101,7 @@ internal class Grid : Gtk.Box {
         // to account for spacer/HourRunner
         int col = 1;
         foreach (Calendar.Date date in week) {
-            Gtk.Label date_label = new Gtk.Label("%s %d/%d".printf(date.day_of_week.abbrev_name,
-                date.month_of_year().month.value, date.day_of_month.value));
+            Gtk.Label date_label = new Gtk.Label(date.to_pretty_string(DATE_LABEL_FLAGS));
             // draw a line along the bottom of the label
             date_label.draw.connect(on_draw_bottom_line);
             top_grid.attach(date_label, col, 0, 1, 1);


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