Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Unified Diff: ui/base/l10n/time_format.h

Issue 147443007: Add support for localized time strings with two units, eg. "2 hours 17 minutes" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More cleanup and documentation Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/base/l10n/time_format.h
diff --git a/ui/base/l10n/time_format.h b/ui/base/l10n/time_format.h
index 1306bf00051e623e920cbbb0ad0ed0cb57130287..76fcac6394849476fd0252f0b2b45f5bf32f8415 100644
--- a/ui/base/l10n/time_format.h
+++ b/ui/base/l10n/time_format.h
@@ -15,27 +15,48 @@ class TimeDelta;
}
namespace ui {
+class FormatterContainer;
// Methods to format time values as strings.
class UI_BASE_EXPORT TimeFormat {
public:
- // TimeElapsed, TimeRemaining* and TimeDuration*:
- // These functions return a localized string of approximate time duration.
+ enum { kNType = 3 };
bartfab (slow) 2014/02/18 12:04:04 This is not how we do enums in Chrome: 1. Enum en
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ enum Type {
+ kDuration, // plain duration, eg. in English: "2 minutes"
bartfab (slow) 2014/02/18 12:04:04 Nit: Capitalize the first letter of comments.
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ kRemaining, // remaining time, eg. in English: "2 minutes left"
+ kElapsed // elapsed time, eg. in English: "2 minutes ago"
+ };
- // Returns times in elapsed-format: "3 mins ago", "2 days ago".
- static base::string16 TimeElapsed(const base::TimeDelta& delta);
+ enum { kNLength = 2 };
bartfab (slow) 2014/02/18 12:04:04 See above. This is not how we do enums in Chrome.
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ enum Length {
+ kShort, // short format, eg. in English: sec/min/hour/day
bartfab (slow) 2014/02/18 12:04:04 Nit 1: Capitalize the first letter of comments. Ni
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ kLong // long format, eg. in English: second/minute/hour/day
+ };
- // Returns times in remaining-format: "3 mins left", "2 days left".
- static base::string16 TimeRemaining(const base::TimeDelta& delta);
+ // Return a localized string of approximate time duration, formatted as a
+ // single number, eg. in English "2 hours ago". Currently, all combinations
bartfab (slow) 2014/02/18 12:04:04 Nit: s/eg./e.g.,/
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ // of type and length are implemented except (kElapsed,kLong).
bartfab (slow) 2014/02/18 12:04:04 Nit 1: Add space after comma. Nit 2: Reorder sente
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ static base::string16 Simple(Type type,
+ Length length,
+ const base::TimeDelta& delta);
- // Returns times in remaining-long-format: "3 minutes left", "2 days left".
- static base::string16 TimeRemainingLong(const base::TimeDelta& delta);
-
- // Returns times in short-format: "3 mins", "2 days".
- static base::string16 TimeDurationShort(const base::TimeDelta& delta);
-
- // Return times in long-format: "2 hours", "25 minutes".
- static base::string16 TimeDurationLong(const base::TimeDelta& delta);
+ // Return a localized string of approximate time duration, conditionally
+ // formatted as a single number, eg. in English "2 hours ago", or as two
bartfab (slow) 2014/02/18 12:04:04 Nit: s/eg./e.g.,/
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ // numbers, eg. in English "2 hours and 13 minutes ago". The two-number
bartfab (slow) 2014/02/18 12:04:04 Nit: s/eg./e.g.,/
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ // format is chosen whenever the single-number format would yield a numeric
+ // value less than cutoff. (Applied to the examples above, cutoff=2 would
bartfab (slow) 2014/02/18 12:04:04 Nit 1: Put pipes around symbol names referenced in
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ // yield the first string and cutoff=3 would return the second string.)
bartfab (slow) 2014/02/18 12:04:04 Your example is correct but it took me a lot of th
Thiemo Nagel 2014/02/19 17:08:44 I've tried to improve the comment.
+ // Currently, the only combination of type and length that is implemented is
+ // (kDuration,kLong).
bartfab (slow) 2014/02/18 12:04:04 Nit: Add space after comma.
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ //
+ // Note: This is implemented with two translation strings (IDS_TIME_*_1ST and
+ // IDS_TIME_*_2ND) per [plurality x pair of units] which are contatenated
bartfab (slow) 2014/02/18 12:04:04 Nit: s/contatenated/concatenated/
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ // after having been formatted individually. The separator between first unit
+ // and second unit (presently a blank) is included in IDS_TIME_*_1ST.
bartfab (slow) 2014/02/18 12:04:04 I think "a blank in the case of English" would be
Thiemo Nagel 2014/02/19 17:08:44 Done. (I wanted to express that someday someone m
+ static base::string16 Detailed(Type type,
+ Length length,
+ int cutoff,
+ const base::TimeDelta& delta);
// For displaying a relative time in the past. This method returns either
// "Today", "Yesterday", or an empty string if it's older than that. Returns
@@ -54,8 +75,32 @@ class UI_BASE_EXPORT TimeFormat {
static base::string16 RelativeDate(const base::Time& time,
const base::Time* optional_midnight_today);
+ protected:
+ // Implemented in header file to avoid code generation in production builds.
+ static base::string16 SimpleForTesting(const FormatterContainer& container,
bartfab (slow) 2014/02/18 12:04:04 AFAICT, the only difference between these ForTesti
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ Type type,
+ Length length,
+ const base::TimeDelta& delta) {
+ return DetailedForTesting(container, type, length, 0, delta);
+ }
+ static base::string16 DetailedForTesting(const FormatterContainer& container,
bartfab (slow) 2014/02/18 12:04:04 Nit: Add blank line before this.
Thiemo Nagel 2014/02/19 17:08:44 Done.
+ Type type,
+ Length length,
+ int cutoff,
+ const base::TimeDelta& delta) {
+ return FormatTimeImpl(container, type, length, cutoff, delta);
+ }
+
private:
+ static base::string16 FormatTimeImpl(const FormatterContainer& container,
bartfab (slow) 2014/02/18 12:04:04 Nit: The suffix |Impl| is used in Chrome for class
Thiemo Nagel 2014/02/19 17:08:44 I've removed the method anyways.
+ Type type,
+ Length length,
+ int cutoff,
+ const base::TimeDelta& delta);
+
+#ifndef TIME_FORMAT_UNITTEST
bartfab (slow) 2014/02/18 12:04:04 There is no precedent for this kind of constructio
Thiemo Nagel 2014/02/19 17:08:44 Done.
DISALLOW_IMPLICIT_CONSTRUCTORS(TimeFormat);
+#endif
};
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698