Skip to content

Commit a046c83

Browse files
New optional override to the resource bundle path. Allows the user to pass in their own path to the resource bundle to make overriding easy.
1 parent 5b94c77 commit a046c83

4 files changed

Lines changed: 242 additions & 5 deletions

File tree

‎core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class PrettyTime
6767
private volatile Locale locale = Locale.getDefault();
6868
private volatile Map<TimeUnit, TimeFormat> units = new LinkedHashMap<TimeUnit, TimeFormat>();
6969
private volatile List<TimeUnit> cachedUnits;
70+
private String overrideResourceBundle;
7071

7172
/**
7273
* Create a new {@link PrettyTime} instance that will always use the current value of
@@ -78,6 +79,18 @@ public PrettyTime()
7879
initTimeUnits();
7980
}
8081

82+
/**
83+
* Create a new {@link PrettyTime} instance that will always use the current value of
84+
* {@link System#currentTimeMillis()} to represent the reference point for {@link Date} comparison, and will use
85+
* {@link Locale#getDefault()} as the selected {@link Locale} for language and dialect formatting.
86+
* Will use {@link String} as an optional override to the default resource bundles.
87+
*/
88+
public PrettyTime(String overrideResourceBundle)
89+
{
90+
this.overrideResourceBundle = overrideResourceBundle;
91+
initTimeUnits();
92+
}
93+
8194
/**
8295
* Create a new {@link PrettyTime} instance that will use the given {@link Date} timestamp to represent the reference
8396
* point for {@link Date} comparison, and will use {@link Locale#getDefault()} as the selected {@link Locale} for
@@ -94,6 +107,23 @@ public PrettyTime(final Date reference)
94107
setReference(reference);
95108
}
96109

110+
/**
111+
* Create a new {@link PrettyTime} instance that will use the given {@link Date} timestamp to represent the reference
112+
* point for {@link Date} comparison, and will use {@link Locale#getDefault()} as the selected {@link Locale} for
113+
* language and dialect formatting. If the given {@link Date} is <code>null</code>, this instance will always use the
114+
* current value of {@link System#currentTimeMillis()} to represent the reference point for {@link Date} comparison.
115+
* Will use {@link String} as an optional override to the default resource bundles.
116+
* <p>
117+
*
118+
* <p>
119+
* See {@code PrettyTime#setReference(Date timestamp)}.
120+
*/
121+
public PrettyTime(final Date reference, String overrideResourceBundle)
122+
{
123+
this(overrideResourceBundle);
124+
setReference(reference);
125+
}
126+
97127
/**
98128
* Construct a new {@link PrettyTime} instance that will always use the current value of
99129
* {@link System#currentTimeMillis()} to represent the reference point for {@link Date} comparison. This instance
@@ -106,6 +136,20 @@ public PrettyTime(final Locale locale)
106136
initTimeUnits();
107137
}
108138

139+
/**
140+
* Construct a new {@link PrettyTime} instance that will always use the current value of
141+
* {@link System#currentTimeMillis()} to represent the reference point for {@link Date} comparison. This instance
142+
* will use the given {@link Locale} instead of the system default. If the provided {@link Locale} is
143+
* <code>null</code>, {@link Locale#getDefault()} will be used.
144+
* Will use {@link String} as an optional override to the default resource bundles.
145+
*/
146+
public PrettyTime(final Locale locale, String overrideResourceBundle)
147+
{
148+
this.overrideResourceBundle = overrideResourceBundle;
149+
setLocale(locale);
150+
initTimeUnits();
151+
}
152+
109153
/**
110154
* Construct a new {@link PrettyTime} instance that will use the given {@link Date} timestamp to represent the
111155
* reference point for {@link Date} comparison, and will use the given {@link Locale} instead of the system default.
@@ -122,6 +166,23 @@ public PrettyTime(final Date reference, final Locale locale)
122166
setReference(reference);
123167
}
124168

169+
/**
170+
* Construct a new {@link PrettyTime} instance that will use the given {@link Date} timestamp to represent the
171+
* reference point for {@link Date} comparison, and will use the given {@link Locale} instead of the system default.
172+
* <p>
173+
* If the provided {@link Locale} is <code>null</code>, {@link Locale#getDefault()} will be used instead.<br>
174+
* If the given {@link Date} is <code>null</code>, this instance will always use current value of
175+
* {@link System#currentTimeMillis()} will be used to represent the reference point for {@link Date} comparison.
176+
* Will use {@link String} as an optional override to the default resource bundles.
177+
* <p>
178+
* See {@code PrettyTime#setReference(Date timestamp)}.
179+
*/
180+
public PrettyTime(final Date reference, final Locale locale, String overrideResourceBundle)
181+
{
182+
this(locale, overrideResourceBundle);
183+
setReference(reference);
184+
}
185+
125186
/**
126187
* Calculate the approximate {@link Duration} between the reference {@link Date} and given {@link Date}. If the given
127188
* {@link Date} is <code>null</code>, the current value of {@link System#currentTimeMillis()} will be used instead.
@@ -711,7 +772,7 @@ private void initTimeUnits()
711772

712773
private void addUnit(ResourcesTimeUnit unit)
713774
{
714-
registerUnit(unit, new ResourcesTimeFormat(unit));
775+
registerUnit(unit, new ResourcesTimeFormat(unit, overrideResourceBundle));
715776
}
716777

717778
private Duration calculateDuration(final long difference)

‎core/src/main/java/org/ocpsoft/prettytime/impl/ResourcesTimeFormat.java‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.ocpsoft.prettytime.impl;
22

3-
import java.util.Locale;
4-
import java.util.ResourceBundle;
5-
63
import org.ocpsoft.prettytime.Duration;
74
import org.ocpsoft.prettytime.LocaleAware;
85
import org.ocpsoft.prettytime.TimeFormat;
96
import org.ocpsoft.prettytime.format.SimpleTimeFormat;
107

8+
import java.util.Locale;
9+
import java.util.ResourceBundle;
10+
1111
/**
1212
* Represents a simple method of formatting a specific {@link Duration} of time
1313
*
@@ -18,16 +18,35 @@ public class ResourcesTimeFormat extends SimpleTimeFormat implements TimeFormat,
1818
private ResourceBundle bundle;
1919
private final ResourcesTimeUnit unit;
2020
private TimeFormat override;
21+
private String overrideResourceBundle; // If used this bundle will override the included bundle
2122

2223
public ResourcesTimeFormat(ResourcesTimeUnit unit)
2324
{
2425
this.unit = unit;
2526
}
2627

28+
public ResourcesTimeFormat(ResourcesTimeUnit unit, String overrideResourceBundle)
29+
{
30+
this.unit = unit;
31+
this.overrideResourceBundle = overrideResourceBundle;
32+
}
33+
2734
@Override
2835
public ResourcesTimeFormat setLocale(Locale locale)
2936
{
30-
bundle = ResourceBundle.getBundle(unit.getResourceBundleName(), locale);
37+
if (overrideResourceBundle != null) {
38+
try {
39+
// Attempt to load the bundle that the user passed in, maybe it exists, maybe not
40+
bundle = ResourceBundle.getBundle(overrideResourceBundle, locale);
41+
} catch (Exception e) {
42+
// Throw away if the bundle doesn't contain this local
43+
}
44+
}
45+
46+
// If the bundle doesn't exist then load the default included one
47+
if (bundle == null) {
48+
bundle = ResourceBundle.getBundle(unit.getResourceBundleName(), locale);
49+
}
3150

3251
if (bundle instanceof TimeFormatProvider)
3352
{
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.prettytime;
17+
18+
import org.junit.After;
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import java.util.Date;
24+
import java.util.Locale;
25+
26+
public class PrettyTimeOverrideBundleTest
27+
{
28+
private Locale locale;
29+
30+
@Before
31+
public void setUp() throws Exception
32+
{
33+
locale = Locale.getDefault();
34+
Locale.setDefault(Locale.ROOT);
35+
}
36+
37+
@Test
38+
public void testHoursAgo() throws Exception
39+
{
40+
PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 3), Locale.ENGLISH,
41+
"org.ocpsoft.prettytime.i18n.override.Resources");
42+
Assert.assertEquals("3 hours ago override", t.format(new Date(0)));
43+
}
44+
45+
@After
46+
public void tearDown() throws Exception
47+
{
48+
Locale.setDefault(locale);
49+
}
50+
51+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.ocpsoft.prettytime.i18n.override;
2+
3+
import java.util.ListResourceBundle;
4+
5+
public class Resources_en extends ListResourceBundle
6+
{
7+
private static final Object[][] OBJECTS = new Object[][] {
8+
{ "CenturyPattern", "%n %u" },
9+
{ "CenturyFuturePrefix", "" },
10+
{ "CenturyFutureSuffix", " from now" },
11+
{ "CenturyPastPrefix", "" },
12+
{ "CenturyPastSuffix", " ago" },
13+
{ "CenturySingularName", "century" },
14+
{ "CenturyPluralName", "centuries" },
15+
{ "DayPattern", "%n %u" },
16+
{ "DayFuturePrefix", "" },
17+
{ "DayFutureSuffix", " from now" },
18+
{ "DayPastPrefix", "" },
19+
{ "DayPastSuffix", " ago" },
20+
{ "DaySingularName", "day" },
21+
{ "DayPluralName", "days" },
22+
{ "DecadePattern", "%n %u" },
23+
{ "DecadeFuturePrefix", "" },
24+
{ "DecadeFutureSuffix", " from now" },
25+
{ "DecadePastPrefix", "" },
26+
{ "DecadePastSuffix", " ago" },
27+
{ "DecadeSingularName", "decade" },
28+
{ "DecadePluralName", "decades" },
29+
{ "HourPattern", "%n %u" },
30+
{ "HourFuturePrefix", "" },
31+
{ "HourFutureSuffix", " from now" },
32+
{ "HourPastPrefix", "" },
33+
{ "HourPastSuffix", " ago override" },
34+
{ "HourSingularName", "hour" },
35+
{ "HourPluralName", "hours" },
36+
{ "JustNowPattern", "%u" },
37+
{ "JustNowFuturePrefix", "" },
38+
{ "JustNowFutureSuffix", "moments from now" },
39+
{ "JustNowPastPrefix", "moments ago" },
40+
{ "JustNowPastSuffix", "" },
41+
{ "JustNowSingularName", "" },
42+
{ "JustNowPluralName", "" },
43+
{ "MillenniumPattern", "%n %u" },
44+
{ "MillenniumFuturePrefix", "" },
45+
{ "MillenniumFutureSuffix", " from now" },
46+
{ "MillenniumPastPrefix", "" },
47+
{ "MillenniumPastSuffix", " ago" },
48+
{ "MillenniumSingularName", "millennium" },
49+
{ "MillenniumPluralName", "millennia" },
50+
{ "MillisecondPattern", "%n %u" },
51+
{ "MillisecondFuturePrefix", "" },
52+
{ "MillisecondFutureSuffix", " from now" },
53+
{ "MillisecondPastPrefix", "" },
54+
{ "MillisecondPastSuffix", " ago" },
55+
{ "MillisecondSingularName", "millisecond" },
56+
{ "MillisecondPluralName", "milliseconds" },
57+
{ "MinutePattern", "%n %u" },
58+
{ "MinuteFuturePrefix", "" },
59+
{ "MinuteFutureSuffix", " from now" },
60+
{ "MinutePastPrefix", "" },
61+
{ "MinutePastSuffix", " ago" },
62+
{ "MinuteSingularName", "minute" },
63+
{ "MinutePluralName", "minutes" },
64+
{ "MonthPattern", "%n %u" },
65+
{ "MonthFuturePrefix", "" },
66+
{ "MonthFutureSuffix", " from now" },
67+
{ "MonthPastPrefix", "" },
68+
{ "MonthPastSuffix", " ago" },
69+
{ "MonthSingularName", "month" },
70+
{ "MonthPluralName", "months" },
71+
{ "SecondPattern", "%n %u" },
72+
{ "SecondFuturePrefix", "" },
73+
{ "SecondFutureSuffix", " from now" },
74+
{ "SecondPastPrefix", "" },
75+
{ "SecondPastSuffix", " ago" },
76+
{ "SecondSingularName", "second" },
77+
{ "SecondPluralName", "seconds" },
78+
{ "WeekPattern", "%n %u" },
79+
{ "WeekFuturePrefix", "" },
80+
{ "WeekFutureSuffix", " from now" },
81+
{ "WeekPastPrefix", "" },
82+
{ "WeekPastSuffix", " ago" },
83+
{ "WeekSingularName", "week" },
84+
{ "WeekPluralName", "weeks" },
85+
{ "YearPattern", "%n %u" },
86+
{ "YearFuturePrefix", "" },
87+
{ "YearFutureSuffix", " from now" },
88+
{ "YearPastPrefix", "" },
89+
{ "YearPastSuffix", " ago" },
90+
{ "YearSingularName", "year" },
91+
{ "YearPluralName", "years" },
92+
{ "AbstractTimeUnitPattern", "" },
93+
{ "AbstractTimeUnitFuturePrefix", "" },
94+
{ "AbstractTimeUnitFutureSuffix", "" },
95+
{ "AbstractTimeUnitPastPrefix", "" },
96+
{ "AbstractTimeUnitPastSuffix", "" },
97+
{ "AbstractTimeUnitSingularName", "" },
98+
{ "AbstractTimeUnitPluralName", "" } };
99+
100+
@Override
101+
public Object[][] getContents()
102+
{
103+
return OBJECTS;
104+
}
105+
106+
}

0 commit comments

Comments
 (0)