Skip to content

Commit f2ded3e

Browse files
committed
Merge pull request ocpsoft#41 from 452/master
added Ukrainian language from Tumin Alexander implementation
2 parents 106e013 + 4b5f49b commit f2ded3e

4 files changed

Lines changed: 438 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ public String decorateUnrounded(Duration duration, String time) {
145145
} else if (t instanceof Hour) {
146146
return new TimeFormatAided("час", "часа", "часов");
147147
} else if (t instanceof Millennium) {
148-
return new TimeFormatAided("тысячелетие", "тысячелетия", "тысячилетий");
148+
return new TimeFormatAided("тысячелетие", "тысячелетия", "тысячелетий");
149149
} else if (t instanceof Millisecond) {
150-
return new TimeFormatAided("миллисекунду", "миллисекунды", "миллесекунд");
150+
return new TimeFormatAided("миллисекунду", "миллисекунды", "миллисекунд");
151151
} else if (t instanceof Minute) {
152152
return new TimeFormatAided("минуту", "минуты", "минут");
153153
} else if (t instanceof Month) {
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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.i18n;
17+
18+
import org.ocpsoft.prettytime.Duration;
19+
import org.ocpsoft.prettytime.TimeFormat;
20+
import org.ocpsoft.prettytime.TimeUnit;
21+
import org.ocpsoft.prettytime.impl.TimeFormatProvider;
22+
import org.ocpsoft.prettytime.units.*;
23+
24+
import java.util.ListResourceBundle;
25+
26+
/**
27+
* Created with IntelliJ IDEA.
28+
* User: Tumin Alexander
29+
* Date: 2012-12-13
30+
* Time: 03:33
31+
*
32+
* reedit to Ukrainian with Eclipse).
33+
* User: Ihor Lavrynuk
34+
* Date: 2013-01-06
35+
* Time: 15:04
36+
*
37+
*/
38+
public class Resources_ua extends ListResourceBundle implements TimeFormatProvider
39+
{
40+
private static final Object[][] OBJECTS = new Object[0][0];
41+
42+
private static final int tolerance = 50;
43+
44+
// see http://translate.sourceforge.net/wiki/l10n/pluralforms
45+
private static final int slavicPluralForms = 3;
46+
47+
private static class TimeFormatAided implements TimeFormat {
48+
private final String[] pluarls;
49+
50+
public TimeFormatAided(String ... plurals) {
51+
if (plurals.length != slavicPluralForms) {
52+
throw new IllegalArgumentException("Wrong plural forms number for slavic language!");
53+
}
54+
this.pluarls = plurals;
55+
}
56+
57+
@Override
58+
public String format(Duration duration) {
59+
long quantity = duration.getQuantityRounded(tolerance);
60+
StringBuilder result = new StringBuilder();
61+
result.append(quantity);
62+
return result.toString();
63+
}
64+
65+
@Override
66+
public String formatUnrounded(Duration duration) {
67+
long quantity = duration.getQuantity();
68+
StringBuilder result = new StringBuilder();
69+
result.append(quantity);
70+
return result.toString();
71+
}
72+
73+
@Override
74+
public String decorate(Duration duration, String time) {
75+
return performDecoration(
76+
duration.isInPast(),
77+
duration.isInFuture(),
78+
duration.getQuantityRounded(tolerance),
79+
time
80+
);
81+
}
82+
83+
@Override
84+
public String decorateUnrounded(Duration duration, String time) {
85+
return performDecoration(
86+
duration.isInPast(),
87+
duration.isInFuture(),
88+
duration.getQuantity(),
89+
time
90+
);
91+
}
92+
93+
private String performDecoration(boolean past, boolean future, long n, String time) {
94+
// a bit cryptic, yet well-tested
95+
// consider http://translate.sourceforge.net/wiki/l10n/pluralforms
96+
int pluralIdx = (n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
97+
if (pluralIdx > slavicPluralForms) {
98+
// impossible happening
99+
throw new IllegalStateException("Wrong plural index was calculated somehow for slavic language");
100+
}
101+
102+
StringBuilder result = new StringBuilder();
103+
104+
if (future) {
105+
result.append("через ");
106+
}
107+
108+
result.append(time);
109+
result.append(' ');
110+
result.append(pluarls[pluralIdx]);
111+
112+
if (past) {
113+
result.append(" тому");
114+
}
115+
116+
return result.toString();
117+
}
118+
}
119+
120+
@Override
121+
public Object[][] getContents()
122+
{
123+
return OBJECTS;
124+
}
125+
126+
@Override
127+
public TimeFormat getFormatFor(TimeUnit t) {
128+
if (t instanceof JustNow) {
129+
return new TimeFormat() {
130+
@Override
131+
public String format(Duration duration) {
132+
return performFormat(duration);
133+
}
134+
135+
@Override
136+
public String formatUnrounded(Duration duration) {
137+
return performFormat(duration);
138+
}
139+
140+
private String performFormat(Duration duration) {
141+
if (duration.isInFuture()) {
142+
return "зараз";
143+
}
144+
if (duration.isInPast()) {
145+
return "тільки що";
146+
}
147+
return null;
148+
}
149+
150+
@Override
151+
public String decorate(Duration duration, String time) {
152+
return time;
153+
}
154+
155+
@Override
156+
public String decorateUnrounded(Duration duration, String time) {
157+
return time;
158+
}
159+
};
160+
} else if (t instanceof Century) {
161+
return new TimeFormatAided("століття", "століття", "столітть");
162+
} else if (t instanceof Day) {
163+
return new TimeFormatAided("день", "дні", "днів");
164+
} else if (t instanceof Decade) {
165+
return new TimeFormatAided("десятиліття", "десятиліття", "десятиліть");
166+
} else if (t instanceof Hour) {
167+
return new TimeFormatAided("годину", "години", "годин");
168+
} else if (t instanceof Millennium) {
169+
return new TimeFormatAided("тисячоліття", "тисячоліття", "тисячоліть");
170+
} else if (t instanceof Millisecond) {
171+
return new TimeFormatAided("мілісекунду", "мілісекунди", "мілісекунд");
172+
} else if (t instanceof Minute) {
173+
return new TimeFormatAided("хвилину", "хвилини", "хвилин");
174+
} else if (t instanceof Month) {
175+
return new TimeFormatAided("місяць", "місяці", "місяців");
176+
} else if (t instanceof Second) {
177+
return new TimeFormatAided("секунду", "секунди", "секунд");
178+
} else if (t instanceof Week) {
179+
return new TimeFormatAided("тиждень", "тижні", "тижнів");
180+
} else if (t instanceof Year) {
181+
return new TimeFormatAided("рік", "роки", "років");
182+
}
183+
return null; // error
184+
}
185+
}

‎core/src/test/java/org/ocpsoft/prettytime/PrettyTimeI18n_RU_Test.java‎

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

3-
import org.junit.After;
4-
import org.junit.Before;
5-
import org.junit.Test;
6-
import org.ocpsoft.prettytime.impl.ResourcesTimeFormat;
7-
import org.ocpsoft.prettytime.impl.ResourcesTimeUnit;
8-
import org.ocpsoft.prettytime.units.*;
3+
import static org.junit.Assert.assertEquals;
94

105
import java.text.SimpleDateFormat;
116
import java.util.Date;
127
import java.util.Locale;
138

14-
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.fail;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
1612

1713
/**
1814
* Created with IntelliJ IDEA.

0 commit comments

Comments
 (0)