Debian Codegraph @

closure-library/closure/goog/date/relativewithplurals.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Code to make goog.date.relative plurals-aware.
17 */
18
19 goog.provide('goog.date.relativeWithPlurals');
20
21 goog.require('goog.date.relative');
22 goog.require('goog.date.relative.Unit');
23 goog.require('goog.i18n.MessageFormat');
24
25
26 /**
27 * Gets a localized relative date string for a given delta and unit.
28 * @param {number} delta Number of minutes/hours/days.
29 * @param {boolean} future Whether the delta is in the future.
30 * @param {goog.date.relative.Unit} unit The units the delta is in.
31 * @return {string} The message.
32 * @private
33 */
34 goog.date.relativeWithPlurals.formatTimeDelta_ = function(delta, future, unit) {
35 if (!future && unit == goog.date.relative.Unit.MINUTES) {
36 /**
37 * @desc Relative date indicating how many minutes ago something happened.
38 */
39 var MSG_MINUTES_AGO_ICU = goog.getMsg(
40 '{NUM, plural, ' +
41 '=0 {# minutes ago}' +
42 '=1 {# minute ago}' +
43 'other {# minutes ago}}');
44
45 return new goog.i18n.MessageFormat(MSG_MINUTES_AGO_ICU).format({
46 'NUM': delta
47 });
48
49 } else if (future && unit == goog.date.relative.Unit.MINUTES) {
50 /**
51 * @desc Relative date indicating in how many minutes something happens.
52 */
53 var MSG_IN_MINUTES_ICU = goog.getMsg(
54 '{NUM, plural, ' +
55 '=0 {in # minutes}' +
56 '=1 {in # minute}' +
57 'other {in # minutes}}');
58
59 return new goog.i18n.MessageFormat(MSG_IN_MINUTES_ICU).format({
60 'NUM': delta
61 });
62
63 } else if (!future && unit == goog.date.relative.Unit.HOURS) {
64 /**
65 * @desc Relative date indicating how many hours ago something happened.
66 */
67 var MSG_HOURS_AGO_ICU = goog.getMsg(
68 '{NUM, plural, ' +
69 '=0 {# hours ago}' +
70 '=1 {# hour ago}' +
71 'other {# hours ago}}');
72
73 return new goog.i18n.MessageFormat(MSG_HOURS_AGO_ICU).format({
74 'NUM': delta
75 });
76
77 } else if (future && unit == goog.date.relative.Unit.HOURS) {
78 /**
79 * @desc Relative date indicating in how many hours something happens.
80 */
81 var MSG_IN_HOURS_ICU = goog.getMsg(
82 '{NUM, plural, ' +
83 '=0 {in # hours}' +
84 '=1 {in # hour}' +
85 'other {in # hours}}');
86
87 return new goog.i18n.MessageFormat(MSG_IN_HOURS_ICU).format({'NUM': delta});
88
89 } else if (!future && unit == goog.date.relative.Unit.DAYS) {
90 /**
91 * @desc Relative date indicating how many days ago something happened.
92 */
93 var MSG_DAYS_AGO_ICU = goog.getMsg(
94 '{NUM, plural, ' +
95 '=0 {# days ago}' +
96 '=1 {# day ago}' +
97 'other {# days ago}}');
98
99 return new goog.i18n.MessageFormat(MSG_DAYS_AGO_ICU).format({'NUM': delta});
100
101 } else if (future && unit == goog.date.relative.Unit.DAYS) {
102 /**
103 * @desc Relative date indicating in how many days something happens.
104 */
105 var MSG_IN_DAYS_ICU = goog.getMsg(
106 '{NUM, plural, ' +
107 '=0 {in # days}' +
108 '=1 {in # day}' +
109 'other {in # days}}');
110
111 return new goog.i18n.MessageFormat(MSG_IN_DAYS_ICU).format({'NUM': delta});
112
113 } else {
114 return '';
115 }
116 };
117
118 goog.date.relative.setTimeDeltaFormatter(
119 goog.date.relativeWithPlurals.formatTimeDelta_);