Skip to content

Commit bcc9aed

Browse files
committed
Added (Past|Future)(Singular|Plural)Name overrides to resource bundle and SimpleTimeFormat
1 parent 153a9aa commit bcc9aed

38 files changed

Lines changed: 787 additions & 540 deletions

‎core/src/main/java/org/ocpsoft/prettytime/format/SimpleTimeFormat.java‎

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,10 +19,9 @@
1919
import org.ocpsoft.prettytime.TimeFormat;
2020
import org.ocpsoft.prettytime.TimeUnit;
2121

22-
2322
/**
2423
* Represents a simple method of formatting a specific {@link Duration} of time
25-
*
24+
*
2625
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2726
*/
2827
public class SimpleTimeFormat implements TimeFormat
@@ -32,8 +31,12 @@ public class SimpleTimeFormat implements TimeFormat
3231
public static final String QUANTITY = "%n";
3332
public static final String UNIT = "%u";
3433

35-
private String name = "";
34+
private String singularName = "";
3635
private String pluralName = "";
36+
private String futureSingularName = "";
37+
private String futurePluralName = "";
38+
private String pastSingularName = "";
39+
private String pastPluralName = "";
3740

3841
private String pattern = "";
3942
private String futurePrefix = "";
@@ -93,8 +96,14 @@ private String applyPattern(final String sign, final String unit, final long qua
9396
return result;
9497
}
9598

96-
protected String getPattern(final long quantity) {
97-
return getPattern();
99+
protected String getPattern(final long quantity)
100+
{
101+
return pattern;
102+
}
103+
104+
public String getPattern()
105+
{
106+
return pattern;
98107
}
99108

100109
protected long getQuantity(Duration duration, boolean round)
@@ -104,10 +113,10 @@ protected long getQuantity(Duration duration, boolean round)
104113

105114
protected String getGramaticallyCorrectName(final Duration d, boolean round)
106115
{
107-
String result = getName();
116+
String result = getSingularName(d);
108117
if ((Math.abs(getQuantity(d, round)) == 0) || (Math.abs(getQuantity(d, round)) > 1))
109118
{
110-
result = getPluralName();
119+
result = getPluralName(d);
111120
}
112121
return result;
113122
}
@@ -121,6 +130,26 @@ private String getSign(final Duration d)
121130
return "";
122131
}
123132

133+
private String getSingularName(Duration duration)
134+
{
135+
if (duration.isInFuture() && futureSingularName != null && !futureSingularName.isEmpty())
136+
return futureSingularName;
137+
else if (duration.isInPast() && pastSingularName != null && !pastSingularName.isEmpty())
138+
return pastSingularName;
139+
else
140+
return singularName;
141+
}
142+
143+
private String getPluralName(Duration duration)
144+
{
145+
if (duration.isInFuture() && futurePluralName != null && !futurePluralName.isEmpty())
146+
return futurePluralName;
147+
else if (duration.isInPast() && pastPluralName != null && !pastPluralName.isEmpty())
148+
return pastPluralName;
149+
else
150+
return pluralName;
151+
}
152+
124153
/*
125154
* Builder Setters
126155
*/
@@ -156,7 +185,7 @@ public SimpleTimeFormat setPastSuffix(final String pastSuffix)
156185

157186
/**
158187
* The percentage of the current {@link TimeUnit}.getMillisPerUnit() for which the quantity may be rounded up by one.
159-
*
188+
*
160189
* @param roundingTolerance
161190
* @return
162191
*/
@@ -166,38 +195,40 @@ public SimpleTimeFormat setRoundingTolerance(final int roundingTolerance)
166195
return this;
167196
}
168197

169-
/*
170-
* Normal getters
171-
*/
172-
173-
public String getPattern()
198+
public SimpleTimeFormat setSingularName(String name)
174199
{
175-
return pattern;
200+
this.singularName = name;
201+
return this;
176202
}
177203

178-
public String getFuturePrefix()
204+
public SimpleTimeFormat setPluralName(String pluralName)
179205
{
180-
return futurePrefix;
206+
this.pluralName = pluralName;
207+
return this;
181208
}
182209

183-
public String getFutureSuffix()
210+
public SimpleTimeFormat setFutureSingularName(String futureSingularName)
184211
{
185-
return futureSuffix;
212+
this.futureSingularName = futureSingularName;
213+
return this;
186214
}
187215

188-
public String getPastPrefix()
216+
public SimpleTimeFormat setFuturePluralName(String futurePluralName)
189217
{
190-
return pastPrefix;
218+
this.futurePluralName = futurePluralName;
219+
return this;
191220
}
192221

193-
public String getPastSuffix()
222+
public SimpleTimeFormat setPastSingularName(String pastSingularName)
194223
{
195-
return pastSuffix;
224+
this.pastSingularName = pastSingularName;
225+
return this;
196226
}
197227

198-
public int getRoundingTolerance()
228+
public SimpleTimeFormat setPastPluralName(String pastPluralName)
199229
{
200-
return roundingTolerance;
230+
this.pastPluralName = pastPluralName;
231+
return this;
201232
}
202233

203234
@Override
@@ -207,27 +238,4 @@ public String toString()
207238
+ futureSuffix + ", pastPrefix=" + pastPrefix + ", pastSuffix=" + pastSuffix + ", roundingTolerance="
208239
+ roundingTolerance + "]";
209240
}
210-
211-
public String getName()
212-
{
213-
return name;
214-
}
215-
216-
public SimpleTimeFormat setName(String name)
217-
{
218-
this.name = name;
219-
return this;
220-
}
221-
222-
public String getPluralName()
223-
{
224-
return pluralName;
225-
}
226-
227-
public SimpleTimeFormat setPluralName(String pluralName)
228-
{
229-
this.pluralName = pluralName;
230-
return this;
231-
}
232-
233241
}

‎core/src/main/java/org/ocpsoft/prettytime/i18n/Resources.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,91 @@ public class Resources extends ListResourceBundle
2525
{ "CenturyFutureSuffix", " from now" },
2626
{ "CenturyPastPrefix", "" },
2727
{ "CenturyPastSuffix", " ago" },
28-
{ "CenturyName", "century" },
28+
{ "CenturySingularName", "century" },
2929
{ "CenturyPluralName", "centuries" },
3030
{ "DayPattern", "%n %u" },
3131
{ "DayFuturePrefix", "" },
3232
{ "DayFutureSuffix", " from now" },
3333
{ "DayPastPrefix", "" },
3434
{ "DayPastSuffix", " ago" },
35-
{ "DayName", "day" },
35+
{ "DaySingularName", "day" },
3636
{ "DayPluralName", "days" },
3737
{ "DecadePattern", "%n %u" },
3838
{ "DecadeFuturePrefix", "" },
3939
{ "DecadeFutureSuffix", " from now" },
4040
{ "DecadePastPrefix", "" },
4141
{ "DecadePastSuffix", " ago" },
42-
{ "DecadeName", "decade" },
42+
{ "DecadeSingularName", "decade" },
4343
{ "DecadePluralName", "decades" },
4444
{ "HourPattern", "%n %u" },
4545
{ "HourFuturePrefix", "" },
4646
{ "HourFutureSuffix", " from now" },
4747
{ "HourPastPrefix", "" },
4848
{ "HourPastSuffix", " ago" },
49-
{ "HourName", "hour" },
49+
{ "HourSingularName", "hour" },
5050
{ "HourPluralName", "hours" },
5151
{ "JustNowPattern", "%u" },
5252
{ "JustNowFuturePrefix", "" },
5353
{ "JustNowFutureSuffix", "moments from now" },
5454
{ "JustNowPastPrefix", "moments ago" },
5555
{ "JustNowPastSuffix", "" },
56-
{ "JustNowName", "" },
56+
{ "JustNowSingularName", "" },
5757
{ "JustNowPluralName", "" },
5858
{ "MillenniumPattern", "%n %u" },
5959
{ "MillenniumFuturePrefix", "" },
6060
{ "MillenniumFutureSuffix", " from now" },
6161
{ "MillenniumPastPrefix", "" },
6262
{ "MillenniumPastSuffix", " ago" },
63-
{ "MillenniumName", "millennium" },
63+
{ "MillenniumSingularName", "millennium" },
6464
{ "MillenniumPluralName", "millennia" },
6565
{ "MillisecondPattern", "%n %u" },
6666
{ "MillisecondFuturePrefix", "" },
6767
{ "MillisecondFutureSuffix", " from now" },
6868
{ "MillisecondPastPrefix", "" },
6969
{ "MillisecondPastSuffix", " ago" },
70-
{ "MillisecondName", "millisecond" },
70+
{ "MillisecondSingularName", "millisecond" },
7171
{ "MillisecondPluralName", "milliseconds" },
7272
{ "MinutePattern", "%n %u" },
7373
{ "MinuteFuturePrefix", "" },
7474
{ "MinuteFutureSuffix", " from now" },
7575
{ "MinutePastPrefix", "" },
7676
{ "MinutePastSuffix", " ago" },
77-
{ "MinuteName", "minute" },
77+
{ "MinuteSingularName", "minute" },
7878
{ "MinutePluralName", "minutes" },
7979
{ "MonthPattern", "%n %u" },
8080
{ "MonthFuturePrefix", "" },
8181
{ "MonthFutureSuffix", " from now" },
8282
{ "MonthPastPrefix", "" },
8383
{ "MonthPastSuffix", " ago" },
84-
{ "MonthName", "month" },
84+
{ "MonthSingularName", "month" },
8585
{ "MonthPluralName", "months" },
8686
{ "SecondPattern", "%n %u" },
8787
{ "SecondFuturePrefix", "" },
8888
{ "SecondFutureSuffix", " from now" },
8989
{ "SecondPastPrefix", "" },
9090
{ "SecondPastSuffix", " ago" },
91-
{ "SecondName", "second" },
91+
{ "SecondSingularName", "second" },
9292
{ "SecondPluralName", "seconds" },
9393
{ "WeekPattern", "%n %u" },
9494
{ "WeekFuturePrefix", "" },
9595
{ "WeekFutureSuffix", " from now" },
9696
{ "WeekPastPrefix", "" },
9797
{ "WeekPastSuffix", " ago" },
98-
{ "WeekName", "week" },
98+
{ "WeekSingularName", "week" },
9999
{ "WeekPluralName", "weeks" },
100100
{ "YearPattern", "%n %u" },
101101
{ "YearFuturePrefix", "" },
102102
{ "YearFutureSuffix", " from now" },
103103
{ "YearPastPrefix", "" },
104104
{ "YearPastSuffix", " ago" },
105-
{ "YearName", "year" },
105+
{ "YearSingularName", "year" },
106106
{ "YearPluralName", "years" },
107107
{ "AbstractTimeUnitPattern", "" },
108108
{ "AbstractTimeUnitFuturePrefix", "" },
109109
{ "AbstractTimeUnitFutureSuffix", "" },
110110
{ "AbstractTimeUnitPastPrefix", "" },
111111
{ "AbstractTimeUnitPastSuffix", "" },
112-
{ "AbstractTimeUnitName", "" },
112+
{ "AbstractTimeUnitSingularName", "" },
113113
{ "AbstractTimeUnitPluralName", "" } };
114114

115115
@Override

0 commit comments

Comments
 (0)