[gnome-break-timer] Use ngettext for time values nested inside strings



commit 43cf8e7cf84834ba3dba84ef9b67faf5456d55ba
Author: Dylan McCall <dylanmccall ubuntu com>
Date:   Tue Sep 17 20:59:04 2013 -0700

    Use ngettext for time values nested inside strings

 common/NaturalTime.vala               |   52 +++++++++++++++++---------------
 helper/microbreak/MicroBreakView.vala |   16 +++++++--
 helper/restbreak/RestBreakView.vala   |   32 +++++++++++++++-----
 settings/BreakType.vala               |    8 +++++
 settings/MicroBreakType.vala          |   25 +++++++++-------
 settings/RestBreakType.vala           |   26 +++++++++-------
 6 files changed, 100 insertions(+), 59 deletions(-)
---
diff --git a/common/NaturalTime.vala b/common/NaturalTime.vala
index 1332196..66f432f 100644
--- a/common/NaturalTime.vala
+++ b/common/NaturalTime.vala
@@ -27,9 +27,9 @@ public class NaturalTime : Object {
                        this.format_time = format_time;
                }
                
-               public string format_seconds (int seconds) {
-                       int time = seconds / this.seconds;
-                       return this.format_time(time);
+               public string format_seconds (int seconds, out int output_value) {
+                       output_value = seconds / this.seconds;
+                       return this.format_time(output_value);
                }
        }
        
@@ -66,9 +66,10 @@ public class NaturalTime : Object {
         * So, an input of 60 will return "1 minute", but 61 will return
         * "61 seconds".
         * @param seconds time in seconds.
+        * @param output_value set to the numerical value of the output.
         * @return a string with a natural and accurate representation of the time.
         */
-       public string get_label_for_seconds (int seconds) {
+       public string get_label_for_seconds (int seconds, out int output_value = null) {
                TimeUnit label_unit = units[0];
                foreach (TimeUnit unit in units) {
                        if (seconds % unit.seconds == 0) {
@@ -77,7 +78,7 @@ public class NaturalTime : Object {
                                if (seconds == 0) break;
                        }
                }
-               return label_unit.format_seconds (seconds);
+               return label_unit.format_seconds (seconds, out output_value);
        }
        
        /**
@@ -87,29 +88,17 @@ public class NaturalTime : Object {
         * So, an input of 60 will return "1 minute", and 61 will return the
         * same.
         * @param seconds time in seconds.
+        * @param output_value set to the numerical value of the output.
         * @return a string with a natural and accurate representation of the time.
         */
-       public string get_simplest_label_for_seconds (int seconds) {
+       public string get_simplest_label_for_seconds (int seconds, out int output_value = null) {
                TimeUnit label_unit = units[0];
                foreach (TimeUnit unit in units) {
                        if (seconds >= unit.seconds) {
                                label_unit = unit;
                        }
                }
-               return label_unit.format_seconds (seconds);
-       }
-       
-       private int soften_seconds_for_countdown (int seconds) {
-               int interval = 1;
-               if (seconds <= 10) {
-                       interval = 1;
-               } else if (seconds <= 60) {
-                       interval = 10;
-               } else {
-                       interval = 60;
-               }
-               int time_softened = ( (seconds-1) / interval) + 1;
-               return time_softened * interval;
+               return label_unit.format_seconds (seconds, out output_value);
        }
        
        /**
@@ -118,11 +107,12 @@ public class NaturalTime : Object {
         * function softens the time by a gradually smaller interval as seconds
         * reaches 0.
         * @param seconds number of seconds remaining in the countdown.
+        * @param output_value set to the numerical value of the output.
         * @return a string representing the time remaining.
         */
-       public string get_countdown_for_seconds (int seconds) {
+       public string get_countdown_for_seconds (int seconds, out int output_value = null) {
                int seconds_softened = soften_seconds_for_countdown (seconds);
-               return get_simplest_label_for_seconds (seconds_softened);
+               return get_simplest_label_for_seconds (seconds_softened, out output_value);
        }
        
        /**
@@ -134,11 +124,25 @@ public class NaturalTime : Object {
         * is shown instead, without being softened.
         * @param seconds number of seconds remaining in the countdown.
         * @param start countdown start time, in seconds, which will be shown exactly.
+        * @param output_value set to the numerical value of the output.
         * @return a string representing the time remaining.
         */
-       public string get_countdown_for_seconds_with_start (int seconds, int start) {
+       public string get_countdown_for_seconds_with_start (int seconds, int start, out int output_value = 
null) {
                int seconds_softened = soften_seconds_for_countdown (seconds);
                if (seconds_softened > start) seconds_softened = start;
-               return get_simplest_label_for_seconds (seconds_softened);
+               return get_simplest_label_for_seconds (seconds_softened, out output_value);
+       }
+
+       private int soften_seconds_for_countdown (int seconds) {
+               int interval = 1;
+               if (seconds <= 10) {
+                       interval = 1;
+               } else if (seconds <= 60) {
+                       interval = 10;
+               } else {
+                       interval = 60;
+               }
+               int time_softened = ( (seconds-1) / interval) + 1;
+               return time_softened * interval;
        }
 }
\ No newline at end of file
diff --git a/helper/microbreak/MicroBreakView.vala b/helper/microbreak/MicroBreakView.vala
index 23d505c..d7ff879 100644
--- a/helper/microbreak/MicroBreakView.vala
+++ b/helper/microbreak/MicroBreakView.vala
@@ -54,13 +54,21 @@ public class MicroBreakView : TimerBreakView {
        }
 
        private void show_overdue_notification () {
+               int delay_value;
                int time_since_start = this.micro_break.get_seconds_since_start ();
-               string delay_string = NaturalTime.instance.get_simplest_label_for_seconds (
-                       time_since_start);
+               string delay_text = NaturalTime.instance.get_simplest_label_for_seconds (
+                       time_since_start, out delay_value);
+
+               string body_text = ngettext (
+                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
+                       "You were due to take a micro break %s ago",
+                       "You were due to take a micro break %s ago",
+                       delay_value
+               ).printf (delay_text);
+
                var notification = this.build_common_notification (
                        _("Overdue micro break"),
-                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
-                       _("You were due to take a micro break %s ago").printf (delay_string),
+                       body_text,
                        "alarm-symbolic"
                );
                notification.set_urgency (Notify.Urgency.NORMAL);
diff --git a/helper/restbreak/RestBreakView.vala b/helper/restbreak/RestBreakView.vala
index dd79b5b..df96387 100644
--- a/helper/restbreak/RestBreakView.vala
+++ b/helper/restbreak/RestBreakView.vala
@@ -75,14 +75,22 @@ public class RestBreakView : TimerBreakView {
        }
 
        private void show_interrupted_notification () {
+               int countdown_value;
                int time_remaining = this.rest_break.get_time_remaining ();
                int start_time = this.rest_break.get_current_duration ();
-               string countdown = NaturalTime.instance.get_countdown_for_seconds_with_start (
-                       time_remaining, start_time);
+               string countdown_text = NaturalTime.instance.get_countdown_for_seconds_with_start (
+                       time_remaining, start_time, out countdown_value);
+
+               string body_text = ngettext (
+                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
+                       "There is %s remaining in your break",
+                       "There are %s remaining in your break",
+                       countdown_value
+               ).printf (countdown_text);
+
                var notification = this.build_common_notification (
                        _("Break interrupted"),
-                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
-                       _("%s remaining in your break").printf (countdown),
+                       body_text,
                        "alarm-symbolic"
                );
                notification.set_urgency (Notify.Urgency.NORMAL);
@@ -90,14 +98,22 @@ public class RestBreakView : TimerBreakView {
        }
 
        private void show_overdue_notification () {
+               int delay_value;
                int64 now = Util.get_real_time_seconds ();
                int time_since_start = (int) (now - this.original_start_time);
-               string delay_string = NaturalTime.instance.get_simplest_label_for_seconds (
-                       time_since_start);
+               string delay_text = NaturalTime.instance.get_simplest_label_for_seconds (
+                       time_since_start, out delay_value);
+
+               string body_text = ngettext (
+                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
+                       "You were due to take a break %s ago",
+                       "You were due to take a break %s ago",
+                       delay_value
+               ).printf (delay_text);
+
                var notification = this.build_common_notification (
                        _("Overdue break"),
-                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
-                       _("You were due to take a break %s ago").printf (delay_string),
+                       body_text,
                        "alarm-symbolic"
                );
                notification.set_urgency (Notify.Urgency.NORMAL);
diff --git a/settings/BreakType.vala b/settings/BreakType.vala
index f55988d..d0c14a3 100644
--- a/settings/BreakType.vala
+++ b/settings/BreakType.vala
@@ -54,6 +54,7 @@ public abstract class BreakInfoPanel : Gtk.Grid {
 
        private Gtk.Label heading_label;
        private Gtk.Label description_label;
+       private Gtk.Label detail_label;
 
        public BreakInfoPanel (BreakType break_type, string title) {
                Object ();
@@ -75,6 +76,9 @@ public abstract class BreakInfoPanel : Gtk.Grid {
                this.description_label.set_justify (Gtk.Justification.CENTER);
                this.description_label.set_max_width_chars (60);
 
+               this.detail_label = new Gtk.Label (null);
+               this.add (this.detail_label);
+
                this.show_all ();
        }
 
@@ -85,6 +89,10 @@ public abstract class BreakInfoPanel : Gtk.Grid {
        protected void set_description (string description) {
                this.description_label.set_label (description);
        }
+
+       protected void set_detail (string detail) {
+               this.detail_label.set_label (detail);
+       }
 }
 
 public abstract class BreakStatusPanel : Gtk.Grid {
diff --git a/settings/MicroBreakType.vala b/settings/MicroBreakType.vala
index f0d7ab7..66b2e57 100644
--- a/settings/MicroBreakType.vala
+++ b/settings/MicroBreakType.vala
@@ -38,12 +38,6 @@ public class MicroBreakType : TimerBreakType {
 }
 
 class MicroBreakInfoPanel : BreakInfoPanel {
-       /* %s will be replaced with a string that describes a time interval, such as "2 minutes", "40 
seconds" or "1 hour" */
-       const string ACTIVE_DESCRIPTION_FORMAT = 
-_("Take a break from typing and look away from the screen for %s.
-
-I'll chime when it’s time to use the computer again.");
-
        private TimerBreakStatus? status;
 
        public MicroBreakInfoPanel (MicroBreakType break_type) {
@@ -61,12 +55,21 @@ I'll chime when it’s time to use the computer again.");
        }
 
        private void update_description () {
-               this.set_heading ( _("It’s microbreak time"));
+               if (this.status == null) return;
 
-               if (this.status != null && this.status.is_active) {
-                       string duration_text = NaturalTime.instance.get_label_for_seconds 
(this.status.current_duration);
-                       this.set_description (ACTIVE_DESCRIPTION_FORMAT.printf (duration_text));
-               }
+               int time_remaining_value;
+               string time_remaining_text = NaturalTime.instance.get_countdown_for_seconds_with_start (
+                       this.status.time_remaining, this.status.current_duration, out time_remaining_value);
+               string description_text = ngettext (
+                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
+                       "Take a break from typing and look away from the screen for %s.",
+                       "Take a break from typing and look away from the screen for %s.",
+                       time_remaining_value
+               ).printf (time_remaining_text);
+
+               this.set_heading ( _("It’s microbreak time"));
+               this.set_description (description_text);
+               this.set_detail (_("I'll chime when it’s time to use the computer again."));
        }
 }
 
diff --git a/settings/RestBreakType.vala b/settings/RestBreakType.vala
index 9babc6e..228b267 100644
--- a/settings/RestBreakType.vala
+++ b/settings/RestBreakType.vala
@@ -38,12 +38,6 @@ public class RestBreakType : TimerBreakType {
 }
 
 class RestBreakInfoPanel : BreakInfoPanel {
-       /* %s will be replaced with a string that describes a time interval, such as "2 minutes", "40 
seconds" or "1 hour" */
-       const string ACTIVE_DESCRIPTION_FORMAT =
-_("Take some time away from the computer. Stretch and move around.
-
-Your break has %s remaining. I’ll remind you when it’s over.");
-
        private TimerBreakStatus? status;
 
        public RestBreakInfoPanel (RestBreakType break_type) {
@@ -62,13 +56,21 @@ Your break has %s remaining. I’ll remind you when it’s over.");
        }
 
        private void update_description () {
-               this.set_heading ( _("It’s break time"));
+               if (this.status == null) return;
 
-               if (this.status != null && this.status.is_active) {
-                       string time_remaining_text = 
NaturalTime.instance.get_countdown_for_seconds_with_start (
-                               this.status.time_remaining, this.status.current_duration);
-                       this.set_description (ACTIVE_DESCRIPTION_FORMAT.printf (time_remaining_text));
-               }
+               int time_remaining_value;
+               string time_remaining_text = NaturalTime.instance.get_countdown_for_seconds_with_start (
+                       this.status.time_remaining, this.status.current_duration, out time_remaining_value);
+               string detail_text = ngettext (
+                       /* %s will be replaced with a string that describes a time interval, such as "2 
minutes", "40 seconds" or "1 hour" */
+                       "Your break has %s remaining. I’ll remind you when it’s over.",
+                       "Your break has %s remaining. I’ll remind you when it’s over.",
+                       time_remaining_value
+               ).printf (time_remaining_text);
+
+               this.set_heading ( _("It’s break time"));
+               this.set_description (_("Take some time away from the computer. Stretch and move around."));
+               this.set_detail (detail_text);
        }
 }
 


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