Skip to content

Commit

Permalink
ICU-20205 RelativeDateTimeFormatter pt data fix, improved error handl…
Browse files Browse the repository at this point in the history
…ing and test. (#210)

* ICU-20205 Add locale test for RelativeDateTimeFormatter.

* ICU-20205 Fix error in pt relative date data. Improve error handling in code.

* ICU-20205 Add instantiation test & regen data from ICU4C

* ICU-20205 Added DateFormatSymbols error check per jefgen's comments.

(cherry picked from commit e509105)
  • Loading branch information
aheninger authored and markusicu committed Oct 13, 2018
1 parent 7451a2c commit 4689545
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion icu4c/source/data/locales/pt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ pt{
}
past{
one{"há {0} segunda-feira"}
other{"{{0} segundas-feiras atrás"}
other{"{0} segundas-feiras atrás"}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions icu4c/source/i18n/reldatefmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,14 @@ static void loadWeekdayNames(UnicodeString absoluteUnits[UDAT_STYLE_COUNT]
[UDAT_ABSOLUTE_UNIT_COUNT][UDAT_DIRECTION_COUNT],
const char* localeId,
UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
Locale locale(localeId);
DateFormatSymbols dfSym(locale, status);
if (U_FAILURE(status)) {
return;
}
for (int32_t style = 0; style < UDAT_STYLE_COUNT; ++style) {
DateFormatSymbols::DtWidthType dtfmtWidth = styleToDateFormatSymbolWidth[style];
int32_t count;
Expand All @@ -630,6 +636,9 @@ static UBool loadUnitData(
RelDateTimeFmtDataSink sink(cacheData);

ures_getAllItemsWithFallback(resource, "fields", sink, status);
if (U_FAILURE(status)) {
return false;
}

// Get the weekday names from DateFormatSymbols.
loadWeekdayNames(cacheData.absoluteUnits, localeId, status);
Expand Down
17 changes: 17 additions & 0 deletions icu4c/source/test/intltest/reldatefmttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*
*******************************************************************************
*/
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#include "intltest.h"

Expand Down Expand Up @@ -765,6 +767,7 @@ class RelativeDateTimeFormatterTest : public IntlTest {
void TestBadDisplayContext();
void TestFormat();
void TestFormatNumeric();
void TestLocales();
void RunTest(
const Locale& locale,
const WithQuantityExpected* expectedResults,
Expand Down Expand Up @@ -854,6 +857,7 @@ void RelativeDateTimeFormatterTest::runIndexedTest(
TESTCASE_AUTO(TestSidewaysDataLoading);
TESTCASE_AUTO(TestFormat);
TESTCASE_AUTO(TestFormatNumeric);
TESTCASE_AUTO(TestLocales);
TESTCASE_AUTO_END;
}

Expand Down Expand Up @@ -1296,6 +1300,19 @@ void RelativeDateTimeFormatterTest::TestFormat() {
RunTest("en", kEnglishFormat, UPRV_LENGTHOF(kEnglishFormat), false);
}

void RelativeDateTimeFormatterTest::TestLocales() {
int32_t numLocales = 0;
const Locale *availableLocales = Locale::getAvailableLocales(numLocales);
std::vector<std::unique_ptr<RelativeDateTimeFormatter>> allFormatters;
for (int localeIdx=0; localeIdx<numLocales; localeIdx++) {
const Locale &loc = availableLocales[localeIdx];
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<RelativeDateTimeFormatter> rdtf(new RelativeDateTimeFormatter(loc, status));
allFormatters.push_back(std::move(rdtf));
assertSuccess(loc.getName(), status);
}
}

static const char *kLast2 = "Last_2";
static const char *kLast = "Last";
static const char *kThis = "This";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,9 @@ private static final DateTimeUnit orNullFromString(CharSequence keyword) {
}

EnumMap<Style, EnumMap<AbsoluteUnit, EnumMap<Direction, String>>> qualitativeUnitMap =
new EnumMap<Style, EnumMap<AbsoluteUnit, EnumMap<Direction, String>>>(Style.class);
new EnumMap<>(Style.class);
EnumMap<Style, EnumMap<RelativeUnit, String[][]>> styleRelUnitPatterns =
new EnumMap<Style, EnumMap<RelativeUnit, String[][]>>(Style.class);
new EnumMap<>(Style.class);

StringBuilder sb = new StringBuilder();

Expand Down Expand Up @@ -1023,7 +1023,7 @@ public void consumeTableRelative(UResource.Key key, UResource.Value value) {
// Handle Zero seconds for "now".
EnumMap<Direction, String> unitStrings = absMap.get(AbsoluteUnit.NOW);
if (unitStrings == null) {
unitStrings = new EnumMap<Direction, String>(Direction.class);
unitStrings = new EnumMap<>(Direction.class);
absMap.put(AbsoluteUnit.NOW, unitStrings);
}
if (unitStrings.get(Direction.PLAIN) == null) {
Expand All @@ -1042,12 +1042,12 @@ public void consumeTableRelative(UResource.Key key, UResource.Value value) {
}

if (absMap == null) {
absMap = new EnumMap<AbsoluteUnit, EnumMap<Direction, String>>(AbsoluteUnit.class);
absMap = new EnumMap<>(AbsoluteUnit.class);
qualitativeUnitMap.put(style, absMap);
}
EnumMap<Direction, String> dirMap = absMap.get(absUnit);
if (dirMap == null) {
dirMap = new EnumMap<Direction, String>(Direction.class);
dirMap = new EnumMap<>(Direction.class);
absMap.put(absUnit, dirMap);
}
if (dirMap.get(keyDirection) == null) {
Expand Down Expand Up @@ -1082,7 +1082,7 @@ public void consumeTimeDetail(UResource.Key key, UResource.Value value) {

EnumMap<RelativeUnit, String[][]> unitPatterns = styleRelUnitPatterns.get(style);
if (unitPatterns == null) {
unitPatterns = new EnumMap<RelativeUnit, String[][]>(RelativeUnit.class);
unitPatterns = new EnumMap<>(RelativeUnit.class);
styleRelUnitPatterns.put(style, unitPatterns);
}
String[][] patterns = unitPatterns.get(unit.relUnit);
Expand Down Expand Up @@ -1112,12 +1112,12 @@ private void handlePlainDirection(UResource.Key key, UResource.Value value) {
EnumMap<AbsoluteUnit, EnumMap<Direction, String>> unitMap =
qualitativeUnitMap.get(style);
if (unitMap == null) {
unitMap = new EnumMap<AbsoluteUnit, EnumMap<Direction, String>>(AbsoluteUnit.class);
unitMap = new EnumMap<>(AbsoluteUnit.class);
qualitativeUnitMap.put(style, unitMap);
}
EnumMap<Direction,String> dirMap = unitMap.get(absUnit);
if (dirMap == null) {
dirMap = new EnumMap<Direction,String>(Direction.class);
dirMap = new EnumMap<>(Direction.class);
unitMap.put(absUnit, dirMap);
}
if (dirMap.get(Direction.PLAIN) == null) {
Expand Down
4 changes: 2 additions & 2 deletions icu4j/main/shared/data/icudata.jar
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -973,4 +973,13 @@ public void TestSidewaysDataLoading() {
String w = fmt.format(6.0, Direction.NEXT, RelativeUnit.QUARTERS);
assertEquals("narrow: in 6 qtr", "in 6 qtr", w);
}

@Test
public void TestLocales() {
ULocale[] availableLocales = ULocale.getAvailableLocales();
for (ULocale loc: availableLocales) {
RelativeDateTimeFormatter.getInstance(loc);
}
}

}

0 comments on commit 4689545

Please sign in to comment.