|
| 1 | +/* |
| 2 | + * Copyright 2013 Martin Kouba |
| 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.i18n; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.ListResourceBundle; |
| 23 | +import java.util.ResourceBundle; |
| 24 | + |
| 25 | +import org.ocpsoft.prettytime.Duration; |
| 26 | +import org.ocpsoft.prettytime.TimeFormat; |
| 27 | +import org.ocpsoft.prettytime.TimeUnit; |
| 28 | +import org.ocpsoft.prettytime.format.SimpleTimeFormat; |
| 29 | +import org.ocpsoft.prettytime.impl.TimeFormatProvider; |
| 30 | +import org.ocpsoft.prettytime.units.Day; |
| 31 | +import org.ocpsoft.prettytime.units.Hour; |
| 32 | +import org.ocpsoft.prettytime.units.Minute; |
| 33 | +import org.ocpsoft.prettytime.units.Month; |
| 34 | +import org.ocpsoft.prettytime.units.Week; |
| 35 | +import org.ocpsoft.prettytime.units.Year; |
| 36 | + |
| 37 | +/** |
| 38 | + * |
| 39 | + * @author Martin Kouba |
| 40 | + */ |
| 41 | +public class Resources_cs extends ListResourceBundle implements TimeFormatProvider |
| 42 | +{ |
| 43 | + private static final Object[][] OBJECTS = new Object[][] { |
| 44 | + |
| 45 | + { "CenturyPattern", "%n %u" }, |
| 46 | + { "CenturyFuturePrefix", "za " }, |
| 47 | + { "CenturyFutureSuffix", "" }, |
| 48 | + { "CenturyPastPrefix", "před " }, |
| 49 | + { "CenturyPastSuffix", "" }, |
| 50 | + { "CenturySingularName", "století" }, |
| 51 | + { "CenturyPluralName", "století" }, |
| 52 | + { "CenturyPastSingularName", "stoletím" }, |
| 53 | + { "CenturyPastPluralName", "stoletími" }, |
| 54 | + { "CenturyFutureSingularName", "století" }, |
| 55 | + { "CenturyFuturePluralName", "století" }, |
| 56 | + |
| 57 | + { "DayPattern", "%n %u" }, |
| 58 | + { "DayFuturePrefix", "za " }, |
| 59 | + { "DayFutureSuffix", "" }, |
| 60 | + { "DayPastPrefix", "před " }, |
| 61 | + { "DayPastSuffix", "" }, |
| 62 | + { "DaySingularName", "den" }, |
| 63 | + { "DayPluralName", "dny" }, |
| 64 | + |
| 65 | + { "DecadePattern", "%n %u" }, |
| 66 | + { "DecadeFuturePrefix", "za " }, |
| 67 | + { "DecadeFutureSuffix", "" }, |
| 68 | + { "DecadePastPrefix", "před " }, |
| 69 | + { "DecadePastSuffix", "" }, |
| 70 | + { "DecadeSingularName", "desetiletí" }, |
| 71 | + { "DecadePluralName", "desetiletí" }, |
| 72 | + { "DecadePastSingularName", "desetiletím" }, |
| 73 | + { "DecadePastPluralName", "desetiletími" }, |
| 74 | + { "DecadeFutureSingularName", "desetiletí" }, |
| 75 | + { "DecadeFuturePluralName", "desetiletí" }, |
| 76 | + |
| 77 | + { "HourPattern", "%n %u" }, |
| 78 | + { "HourFuturePrefix", "za " }, |
| 79 | + { "HourFutureSuffix", "" }, |
| 80 | + { "HourPastPrefix", "před" }, |
| 81 | + { "HourPastSuffix", "" }, |
| 82 | + { "HourSingularName", "hodina" }, |
| 83 | + { "HourPluralName", "hodiny" }, |
| 84 | + |
| 85 | + { "JustNowPattern", "%u" }, |
| 86 | + { "JustNowFuturePrefix", "" }, |
| 87 | + { "JustNowFutureSuffix", "za chvíli" }, |
| 88 | + { "JustNowPastPrefix", "před chvílí" }, |
| 89 | + { "JustNowPastSuffix", "" }, |
| 90 | + { "JustNowSingularName", "" }, |
| 91 | + { "JustNowPluralName", "" }, |
| 92 | + |
| 93 | + { "MillenniumPattern", "%n %u" }, |
| 94 | + { "MillenniumFuturePrefix", "za " }, |
| 95 | + { "MillenniumFutureSuffix", "" }, |
| 96 | + { "MillenniumPastPrefix", "před " }, |
| 97 | + { "MillenniumPastSuffix", "" }, |
| 98 | + { "MillenniumSingularName", "tisíciletí" }, |
| 99 | + { "MillenniumPluralName", "tisíciletí" }, |
| 100 | + |
| 101 | + { "MillisecondPattern", "%n %u" }, |
| 102 | + { "MillisecondFuturePrefix", "za " }, |
| 103 | + { "MillisecondFutureSuffix", "" }, |
| 104 | + { "MillisecondPastPrefix", "před " }, |
| 105 | + { "MillisecondPastSuffix", "" }, |
| 106 | + { "MillisecondSingularName", "milisekunda" }, |
| 107 | + { "MillisecondPluralName", "milisekundy" }, |
| 108 | + { "MillisecondPastSingularName", "milisekundou" }, |
| 109 | + { "MillisecondPastPluralName", "milisekundami" }, |
| 110 | + { "MillisecondFutureSingularName", "milisekundu" }, |
| 111 | + { "MillisecondFuturePluralName", "milisekund" }, |
| 112 | + |
| 113 | + { "MinutePattern", "%n %u" }, |
| 114 | + { "MinuteFuturePrefix", "za " }, |
| 115 | + { "MinuteFutureSuffix", "" }, |
| 116 | + { "MinutePastPrefix", "před " }, |
| 117 | + { "MinutePastSuffix", "" }, |
| 118 | + { "MinuteSingularName", "minuta" }, |
| 119 | + { "MinutePluralName", "minuty" }, |
| 120 | + |
| 121 | + { "MonthPattern", "%n %u" }, |
| 122 | + { "MonthFuturePrefix", "za " }, |
| 123 | + { "MonthFutureSuffix", "" }, |
| 124 | + { "MonthPastPrefix", "před " }, |
| 125 | + { "MonthPastSuffix", "" }, |
| 126 | + { "MonthSingularName", "měsíc" }, |
| 127 | + { "MonthPluralName", "měsíce" }, |
| 128 | + |
| 129 | + { "SecondPattern", "%n %u" }, |
| 130 | + { "SecondFuturePrefix", "za " }, |
| 131 | + { "SecondFutureSuffix", "" }, |
| 132 | + { "SecondPastPrefix", "před " }, |
| 133 | + { "SecondPastSuffix", "" }, |
| 134 | + { "SecondSingularName", "sekunda" }, |
| 135 | + { "SecondPluralName", "sekundy" }, |
| 136 | + |
| 137 | + { "WeekPattern", "%n %u" }, |
| 138 | + { "WeekFuturePrefix", "za " }, |
| 139 | + { "WeekFutureSuffix", "" }, |
| 140 | + { "WeekPastPrefix", "před " }, |
| 141 | + { "WeekPastSuffix", "" }, |
| 142 | + { "WeekSingularName", "týden" }, |
| 143 | + { "WeekPluralName", "týdny" }, |
| 144 | + |
| 145 | + { "YearPattern", "%n %u" }, |
| 146 | + { "YearFuturePrefix", "za " }, |
| 147 | + { "YearFutureSuffix", "" }, |
| 148 | + { "YearPastPrefix", "před " }, |
| 149 | + { "YearPastSuffix", "" }, |
| 150 | + { "YearSingularName", "rok" }, |
| 151 | + { "YearPluralName", "roky" }, |
| 152 | + |
| 153 | + { "AbstractTimeUnitPattern", "" }, |
| 154 | + { "AbstractTimeUnitFuturePrefix", "" }, |
| 155 | + { "AbstractTimeUnitFutureSuffix", "" }, |
| 156 | + { "AbstractTimeUnitPastPrefix", "" }, |
| 157 | + { "AbstractTimeUnitPastSuffix", "" }, |
| 158 | + { "AbstractTimeUnitSingularName", "" }, |
| 159 | + { "AbstractTimeUnitPluralName", "" } }; |
| 160 | + |
| 161 | + @Override |
| 162 | + public Object[][] getContents() |
| 163 | + { |
| 164 | + return OBJECTS; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public TimeFormat getFormatFor(TimeUnit t) { |
| 169 | + if(t instanceof Minute) { |
| 170 | + return new CsTimeFormatBuilder("Minute") |
| 171 | + .addFutureName("minutu", 1) |
| 172 | + .addFutureName("minuty", 4) |
| 173 | + .addFutureName("minut", Long.MAX_VALUE) |
| 174 | + .addPastName("minutou", 1) |
| 175 | + .addPastName("minutami", Long.MAX_VALUE) |
| 176 | + .build(this); |
| 177 | + } else if(t instanceof Hour) { |
| 178 | + return new CsTimeFormatBuilder("Hour") |
| 179 | + .addFutureName("hodinu", 1) |
| 180 | + .addFutureName("hodiny", 4) |
| 181 | + .addFutureName("hodin", Long.MAX_VALUE) |
| 182 | + .addPastName("hodinou", 1) |
| 183 | + .addPastName("hodinami", Long.MAX_VALUE) |
| 184 | + .build(this); |
| 185 | + } else if(t instanceof Day) { |
| 186 | + return new CsTimeFormatBuilder("Day") |
| 187 | + .addFutureName("den", 1) |
| 188 | + .addFutureName("dny", 4) |
| 189 | + .addFutureName("dní", Long.MAX_VALUE) |
| 190 | + .addPastName("dnem", 1) |
| 191 | + .addPastName("dny", Long.MAX_VALUE) |
| 192 | + .build(this); |
| 193 | + } else if(t instanceof Week) { |
| 194 | + return new CsTimeFormatBuilder("Week") |
| 195 | + .addFutureName("týden", 1) |
| 196 | + .addFutureName("týdny", 4) |
| 197 | + .addFutureName("týdnů", Long.MAX_VALUE) |
| 198 | + .addPastName("týdnem", 1) |
| 199 | + .addPastName("týdny", Long.MAX_VALUE) |
| 200 | + .build(this); |
| 201 | + } else if(t instanceof Month) { |
| 202 | + return new CsTimeFormatBuilder("Month") |
| 203 | + .addFutureName("měsíc", 1) |
| 204 | + .addFutureName("měsíce", 4) |
| 205 | + .addFutureName("měsíců", Long.MAX_VALUE) |
| 206 | + .addPastName("měsícem", 1) |
| 207 | + .addPastName("měsíci", Long.MAX_VALUE) |
| 208 | + .build(this); |
| 209 | + } else if(t instanceof Year) { |
| 210 | + return new CsTimeFormatBuilder("Year") |
| 211 | + .addFutureName("rok", 1) |
| 212 | + .addFutureName("roky", 4) |
| 213 | + .addFutureName("let", Long.MAX_VALUE) |
| 214 | + .addPastName("rokem", 1) |
| 215 | + .addPastName("roky", Long.MAX_VALUE) |
| 216 | + .build(this); |
| 217 | + } |
| 218 | + // Don't override format for other time units |
| 219 | + return null; |
| 220 | + } |
| 221 | + |
| 222 | + private static class CsTimeFormatBuilder { |
| 223 | + |
| 224 | + private List<CsName> names = new ArrayList<Resources_cs.CsName>(); |
| 225 | + |
| 226 | + private String resourceKeyPrefix; |
| 227 | + |
| 228 | + CsTimeFormatBuilder(String resourceKeyPrefix) { |
| 229 | + this.resourceKeyPrefix = resourceKeyPrefix; |
| 230 | + } |
| 231 | + |
| 232 | + CsTimeFormatBuilder addFutureName(String name, long limit) { |
| 233 | + return addName(true, name, limit); |
| 234 | + } |
| 235 | + |
| 236 | + CsTimeFormatBuilder addPastName(String name, long limit) { |
| 237 | + return addName(false, name, limit); |
| 238 | + } |
| 239 | + |
| 240 | + private CsTimeFormatBuilder addName(boolean isFuture, String name, long limit) { |
| 241 | + if(name == null) { |
| 242 | + throw new IllegalArgumentException(); |
| 243 | + } |
| 244 | + names.add(new CsName(isFuture, name, limit)); |
| 245 | + return this; |
| 246 | + } |
| 247 | + |
| 248 | + CsTimeFormat build(final ResourceBundle bundle) { |
| 249 | + return new CsTimeFormat(resourceKeyPrefix, bundle, names); |
| 250 | + } |
| 251 | + |
| 252 | + } |
| 253 | + |
| 254 | + private static class CsTimeFormat extends SimpleTimeFormat implements TimeFormat { |
| 255 | + |
| 256 | + private final List<CsName> futureNames = new ArrayList<Resources_cs.CsName>(); |
| 257 | + |
| 258 | + private final List<CsName> pastNames = new ArrayList<Resources_cs.CsName>(); |
| 259 | + |
| 260 | + public CsTimeFormat(String resourceKeyPrefix, ResourceBundle bundle, Collection<CsName> names) { |
| 261 | + setPattern(bundle.getString(resourceKeyPrefix + "Pattern")); |
| 262 | + setFuturePrefix(bundle.getString(resourceKeyPrefix + "FuturePrefix")); |
| 263 | + setFutureSuffix(bundle.getString(resourceKeyPrefix + "FutureSuffix")); |
| 264 | + setPastPrefix(bundle.getString(resourceKeyPrefix + "PastPrefix")); |
| 265 | + setPastSuffix(bundle.getString(resourceKeyPrefix + "PastSuffix")); |
| 266 | + setSingularName(bundle.getString(resourceKeyPrefix + "SingularName")); |
| 267 | + setPluralName(bundle.getString(resourceKeyPrefix + "PluralName")); |
| 268 | + |
| 269 | + try { |
| 270 | + setFuturePluralName(bundle.getString(resourceKeyPrefix + "FuturePluralName")); |
| 271 | + } |
| 272 | + catch (Exception e) {} |
| 273 | + try { |
| 274 | + setFutureSingularName((bundle.getString(resourceKeyPrefix + "FutureSingularName"))); |
| 275 | + } |
| 276 | + catch (Exception e) {} |
| 277 | + try { |
| 278 | + setPastPluralName((bundle.getString(resourceKeyPrefix + "PastPluralName"))); |
| 279 | + } |
| 280 | + catch (Exception e) {} |
| 281 | + try { |
| 282 | + setPastSingularName((bundle.getString(resourceKeyPrefix + "PastSingularName"))); |
| 283 | + } |
| 284 | + catch (Exception e) {} |
| 285 | + |
| 286 | + for (CsName name : names) { |
| 287 | + if(name.isFuture()) { |
| 288 | + futureNames.add(name); |
| 289 | + } else { |
| 290 | + pastNames.add(name); |
| 291 | + } |
| 292 | + } |
| 293 | + Collections.sort(futureNames); |
| 294 | + Collections.sort(pastNames); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + protected String getGramaticallyCorrectName(Duration d, boolean round) { |
| 299 | + long quantity = Math.abs(getQuantity(d, round)); |
| 300 | + if(d.isInFuture()) { |
| 301 | + return getGramaticallyCorrectName(quantity, futureNames); |
| 302 | + } |
| 303 | + return getGramaticallyCorrectName(quantity, pastNames); |
| 304 | + } |
| 305 | + |
| 306 | + private String getGramaticallyCorrectName(long quantity, List<CsName> names) { |
| 307 | + for (CsName name : names) { |
| 308 | + if(name.getThreshold() >= quantity) { |
| 309 | + return name.get(); |
| 310 | + } |
| 311 | + } |
| 312 | + throw new IllegalStateException("Invalid resource bundle configuration"); |
| 313 | + } |
| 314 | + |
| 315 | + } |
| 316 | + |
| 317 | + private static class CsName implements Comparable<CsName>{ |
| 318 | + |
| 319 | + private final boolean isFuture; |
| 320 | + |
| 321 | + private final String value; |
| 322 | + |
| 323 | + private final Long threshold; |
| 324 | + |
| 325 | + public CsName(boolean isFuture, String value, Long threshold) { |
| 326 | + this.isFuture = isFuture; |
| 327 | + this.value = value; |
| 328 | + this.threshold = threshold; |
| 329 | + } |
| 330 | + |
| 331 | + public boolean isFuture() { |
| 332 | + return isFuture; |
| 333 | + } |
| 334 | + |
| 335 | + public String get() { |
| 336 | + return value; |
| 337 | + } |
| 338 | + |
| 339 | + public long getThreshold() { |
| 340 | + return threshold; |
| 341 | + } |
| 342 | + |
| 343 | + @Override |
| 344 | + public int compareTo(CsName o) { |
| 345 | + return threshold.compareTo(o.getThreshold()); |
| 346 | + } |
| 347 | + |
| 348 | + } |
| 349 | + |
| 350 | +} |
0 commit comments