|
15 | 15 | */ |
16 | 16 | package org.ocpsoft.prettytime.i18n; |
17 | 17 |
|
| 18 | +import org.ocpsoft.prettytime.Duration; |
| 19 | +import org.ocpsoft.prettytime.TimeFormat; |
| 20 | +import org.ocpsoft.prettytime.TimeUnit; |
| 21 | +import org.ocpsoft.prettytime.format.SimpleTimeFormat; |
| 22 | +import org.ocpsoft.prettytime.impl.TimeFormatProvider; |
| 23 | +import org.ocpsoft.prettytime.units.Day; |
| 24 | +import org.ocpsoft.prettytime.units.Hour; |
| 25 | +import org.ocpsoft.prettytime.units.Millennium; |
| 26 | +import org.ocpsoft.prettytime.units.Minute; |
| 27 | +import org.ocpsoft.prettytime.units.Week; |
| 28 | +import org.ocpsoft.prettytime.units.Month; |
| 29 | +import org.ocpsoft.prettytime.units.Year; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
18 | 35 | import java.util.ListResourceBundle; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.ResourceBundle; |
19 | 38 |
|
20 | | -public class Resources_sl extends ListResourceBundle |
| 39 | +public class Resources_sl extends ListResourceBundle implements TimeFormatProvider |
21 | 40 | { |
22 | 41 |
|
23 | 42 | private static final Object[][] OBJECTS = new Object[][] { |
@@ -118,4 +137,205 @@ protected Object[][] getContents() |
118 | 137 | { |
119 | 138 | return OBJECTS; |
120 | 139 | } |
| 140 | + |
| 141 | + @Override |
| 142 | + public TimeFormat getFormatFor(final TimeUnit t) |
| 143 | + { |
| 144 | + if (t instanceof Minute) { |
| 145 | + return new SlTimeFormatBuilder("Minute") |
| 146 | + .addNames("minuto", 1) |
| 147 | + .addNames("minuti", 2) |
| 148 | + .addNames("minute", 4) |
| 149 | + .addNames("minut", Long.MAX_VALUE) |
| 150 | + .build(this); |
| 151 | + } |
| 152 | + else if (t instanceof Hour) { |
| 153 | + return new SlTimeFormatBuilder("Hour") |
| 154 | + .addNames("uro", 1) |
| 155 | + .addNames("uri", 2) |
| 156 | + .addNames("ure", 4) |
| 157 | + .addNames("ur", Long.MAX_VALUE) |
| 158 | + .build(this); |
| 159 | + } |
| 160 | + else if (t instanceof Day) { |
| 161 | + return new SlTimeFormatBuilder("Day") |
| 162 | + .addNames("dan", 1) |
| 163 | + .addNames("dni", Long.MAX_VALUE) |
| 164 | + .build(this); |
| 165 | + } |
| 166 | + else if (t instanceof Week) { |
| 167 | + return new SlTimeFormatBuilder("Week") |
| 168 | + .addNames("teden", 1) |
| 169 | + .addNames("tedna", 2) |
| 170 | + .addNames("tedne", 4) |
| 171 | + .addNames("tednov", Long.MAX_VALUE) |
| 172 | + .build(this); |
| 173 | + } |
| 174 | + else if (t instanceof Month) { |
| 175 | + return new SlTimeFormatBuilder("Month") |
| 176 | + .addNames("mesec", 1) |
| 177 | + .addNames("meseca", 2) |
| 178 | + .addNames("mesece", 4) |
| 179 | + .addNames("mesecev", Long.MAX_VALUE) |
| 180 | + .build(this); |
| 181 | + } |
| 182 | + else if (t instanceof Year) { |
| 183 | + return new SlTimeFormatBuilder("Year") |
| 184 | + .addNames("leto", 1) |
| 185 | + .addNames("leti", 2) |
| 186 | + .addNames("leta", 4) |
| 187 | + .addNames("let", Long.MAX_VALUE) |
| 188 | + .build(this); |
| 189 | + } |
| 190 | + else if (t instanceof Millennium) { |
| 191 | + return new SlTimeFormatBuilder("Millennium") |
| 192 | + .addNames("tisočletje", 1) |
| 193 | + .addNames("tisočletji", 2) |
| 194 | + .addNames("tisočletja", 4) |
| 195 | + .addNames("tisočletij", Long.MAX_VALUE) |
| 196 | + .build(this); |
| 197 | + } |
| 198 | + // Don't override format for other time units |
| 199 | + return null; |
| 200 | + } |
| 201 | + |
| 202 | + private static class SlName implements Comparable<SlName> |
| 203 | + { |
| 204 | + |
| 205 | + private final boolean isFuture; |
| 206 | + |
| 207 | + private final Long threshold; |
| 208 | + |
| 209 | + private final String value; |
| 210 | + |
| 211 | + public SlName(final boolean isFuture, final String value, final Long threshold) |
| 212 | + { |
| 213 | + this.isFuture = isFuture; |
| 214 | + this.value = value; |
| 215 | + this.threshold = threshold; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public int compareTo(final SlName o) |
| 220 | + { |
| 221 | + return threshold.compareTo(o.getThreshold()); |
| 222 | + } |
| 223 | + |
| 224 | + public String get() |
| 225 | + { |
| 226 | + return value; |
| 227 | + } |
| 228 | + |
| 229 | + public long getThreshold() |
| 230 | + { |
| 231 | + return threshold; |
| 232 | + } |
| 233 | + |
| 234 | + public boolean isFuture() |
| 235 | + { |
| 236 | + return isFuture; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + private static class SlTimeFormat extends SimpleTimeFormat |
| 241 | + { |
| 242 | + |
| 243 | + private final List<SlName> futureNames = new ArrayList<Resources_sl.SlName>(); |
| 244 | + |
| 245 | + private final List<SlName> pastNames = new ArrayList<Resources_sl.SlName>(); |
| 246 | + |
| 247 | + public SlTimeFormat(final String resourceKeyPrefix, final ResourceBundle bundle, final Collection<SlName> names) |
| 248 | + { |
| 249 | + setPattern(bundle.getString(resourceKeyPrefix + "Pattern")); |
| 250 | + setFuturePrefix(bundle.getString(resourceKeyPrefix + "FuturePrefix")); |
| 251 | + setFutureSuffix(bundle.getString(resourceKeyPrefix + "FutureSuffix")); |
| 252 | + setPastPrefix(bundle.getString(resourceKeyPrefix + "PastPrefix")); |
| 253 | + setPastSuffix(bundle.getString(resourceKeyPrefix + "PastSuffix")); |
| 254 | + setSingularName(bundle.getString(resourceKeyPrefix + "SingularName")); |
| 255 | + setPluralName(bundle.getString(resourceKeyPrefix + "PluralName")); |
| 256 | + |
| 257 | + try { |
| 258 | + setFuturePluralName(bundle.getString(resourceKeyPrefix + "FuturePluralName")); |
| 259 | + } |
| 260 | + catch (final Exception e) { |
| 261 | + } |
| 262 | + try { |
| 263 | + setFutureSingularName(bundle.getString(resourceKeyPrefix + "FutureSingularName")); |
| 264 | + } |
| 265 | + catch (final Exception e) { |
| 266 | + } |
| 267 | + try { |
| 268 | + setPastPluralName(bundle.getString(resourceKeyPrefix + "PastPluralName")); |
| 269 | + } |
| 270 | + catch (final Exception e) { |
| 271 | + } |
| 272 | + try { |
| 273 | + setPastSingularName(bundle.getString(resourceKeyPrefix + "PastSingularName")); |
| 274 | + } |
| 275 | + catch (final Exception e) { |
| 276 | + } |
| 277 | + |
| 278 | + for (final SlName name : names) { |
| 279 | + if (name.isFuture()) { |
| 280 | + futureNames.add(name); |
| 281 | + } |
| 282 | + else { |
| 283 | + pastNames.add(name); |
| 284 | + } |
| 285 | + } |
| 286 | + Collections.sort(futureNames); |
| 287 | + Collections.sort(pastNames); |
| 288 | + } |
| 289 | + |
| 290 | + private String getGramaticallyCorrectName(final long quantity, final List<SlName> names) |
| 291 | + { |
| 292 | + for (final SlName name : names) { |
| 293 | + if (name.getThreshold() >= quantity) { |
| 294 | + return name.get(); |
| 295 | + } |
| 296 | + } |
| 297 | + throw new IllegalStateException("Invalid resource bundle configuration"); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + protected String getGramaticallyCorrectName(final Duration d, final boolean round) |
| 302 | + { |
| 303 | + final long quantity = Math.abs(getQuantity(d, round)); |
| 304 | + if (d.isInFuture()) { |
| 305 | + return getGramaticallyCorrectName(quantity, futureNames); |
| 306 | + } |
| 307 | + return getGramaticallyCorrectName(quantity, pastNames); |
| 308 | + } |
| 309 | + |
| 310 | + } |
| 311 | + |
| 312 | + private static class SlTimeFormatBuilder |
| 313 | + { |
| 314 | + |
| 315 | + private final List<SlName> names = new ArrayList<Resources_sl.SlName>(); |
| 316 | + |
| 317 | + private final String resourceKeyPrefix; |
| 318 | + |
| 319 | + SlTimeFormatBuilder(final String resourceKeyPrefix) |
| 320 | + { |
| 321 | + this.resourceKeyPrefix = resourceKeyPrefix; |
| 322 | + } |
| 323 | + |
| 324 | + private SlTimeFormatBuilder addName(final boolean isFuture, final String name, final long limit) |
| 325 | + { |
| 326 | + names.add(new SlName(isFuture, Objects.requireNonNull(name), limit)); |
| 327 | + return this; |
| 328 | + } |
| 329 | + |
| 330 | + SlTimeFormatBuilder addNames(final String name, final long limit) |
| 331 | + { |
| 332 | + return addName(true, name, limit).addName(false, name, limit); |
| 333 | + } |
| 334 | + |
| 335 | + SlTimeFormat build(final ResourceBundle bundle) |
| 336 | + { |
| 337 | + return new SlTimeFormat(resourceKeyPrefix, bundle, names); |
| 338 | + } |
| 339 | + |
| 340 | + } |
121 | 341 | } |
0 commit comments