Skip to content

Commit

Permalink
CLDR-5854 Add-examples-for-relative-dates-and-times (unicode-org#4060)
Browse files Browse the repository at this point in the history
  • Loading branch information
haytenf authored Oct 2, 2024
1 parent 555d666 commit 3515383
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ public void setCachingEnabled(boolean enabled) {
}
};

// map relativeTimePattern counts to numeric examples
public static final Map<String, String> COUNTS =
new HashMap<String, String>() {
{
put("zero", "0");
put("one", "1");
put("two", "2");
put("few", "3");
put("many", "5");
put("other", "10");
}
};

public CLDRFile getCldrFile() {
return cldrFile;
}
Expand Down Expand Up @@ -540,6 +553,10 @@ private void constructExampleHtmlExtended(String xpath, String value, List<Strin
handleEras(parts, value, examples);
} else if (parts.contains("quarters")) {
handleQuarters(parts, value, examples);
} else if (parts.contains("relative")
|| parts.contains("relativeTime")
|| parts.contains("relativePeriod")) {
handleRelative(xpath, parts, value, examples);
} else if (parts.contains("dayPeriods")) {
handleDayPeriod(parts, value, examples);
} else if (parts.contains("monthContext")) {
Expand Down Expand Up @@ -2981,6 +2998,47 @@ void handleQuarters(XPathParts parts, String value, List<String> examples) {
examples.add(sdf.format(sample));
}

/* Add relative date/time examples, choosing appropriate
* patterns as needed for relative dates vs relative times.
* Additionally, for relativeTimePattern items, ensure that
* numeric example corresponds to the count represented by the item.
*/
private void handleRelative(
String xpath, XPathParts parts, String value, List<String> examples) {
String skeleton;
String type = parts.findAttributeValue("field", "type");
if (type.startsWith("hour")) {
skeleton = "Hm";
} else if (type.startsWith("minute") || type.startsWith("second")) {
skeleton = "ms";
} else if (type.startsWith("year")
|| type.startsWith("month")
|| type.startsWith("quarter")) {
skeleton = "yMMMM";
} else {
skeleton = "MMMMd";
}
String checkPath =
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/dateTimeFormats/availableFormats/dateFormatItem[@id=\""
+ skeleton
+ "\"]";
String dateFormat = cldrFile.getWinningValue(checkPath);
SimpleDateFormat sdf = icuServiceBuilder.getDateFormat("gregorian", dateFormat);
String sampleDate = sdf.format(DATE_SAMPLE);
String example1 =
value.substring(0, 1).toUpperCase() + value.substring(1) + " (" + sampleDate + ")";
String example2 = sampleDate + " (" + value + ")";
if (parts.contains("relativeTimePattern")) { // has placeholder
String count = parts.getAttributeValue(-1, "count");
String exampleCount = COUNTS.get(count);
examples.add(invertBackground(format(setBackground(example1), exampleCount)));
examples.add(invertBackground(format(setBackground(example2), exampleCount)));
} else {
examples.add(format(example1));
examples.add(format(example2));
}
}

/**
* @param elementToOverride the element that is to be overridden
* @param element the overriding element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,36 @@ public void TestQuarterFormats() {
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/quarters/quarterContext[@type=\"stand-alone\"]/quarterWidth[@type=\"abbreviated\"]/quarter[@type=\"4\"]");
}

public void TestRelative() {
ExampleGenerator exampleGeneratorIt = getExampleGenerator("it");
ExampleGenerator exampleGeneratorAm = getExampleGenerator("am");
checkValue(
"it relative day type 2",
"〖Dopodomani (5 settembre)〗〖5 settembre (dopodomani)〗",
exampleGeneratorIt,
"//ldml/dates/fields/field[@type=\"day\"]/relative[@type=\"2\"]");
checkValue(
"it relative hour future-other",
"〖Tra ❬10❭ ore (18:25)〗〖18:25 (tra ❬10❭ ore)〗",
exampleGeneratorIt,
"//ldml/dates/fields/field[@type=\"hour\"]/relativeTime[@type=\"future\"]/relativeTimePattern[@count=\"other\"]");
checkValue(
"it relative year past-one",
"〖❬1❭ anno fa (settembre 1999)〗〖settembre 1999 (❬1❭ anno fa)〗",
exampleGeneratorIt,
"//ldml/dates/fields/field[@type=\"year\"]/relativeTime[@type=\"past\"]/relativeTimePattern[@count=\"one\"]");
checkValue(
"am relative month future-one",
"〖በ❬1❭ ወር ውስጥ (ሴፕቴምበር 1999)〗〖ሴፕቴምበር 1999 (በ❬1❭ ወር ውስጥ)〗",
exampleGeneratorAm,
"//ldml/dates/fields/field[@type=\"month\"]/relativeTime[@type=\"future\"]/relativeTimePattern[@count=\"one\"]");
checkValue(
"am relative month future-other",
"〖በ❬10❭ ወራት ውስጥ (ሴፕቴምበር 1999)〗〖ሴፕቴምበር 1999 (በ❬10❭ ወራት ውስጥ)〗",
exampleGeneratorAm,
"//ldml/dates/fields/field[@type=\"month\"]/relativeTime[@type=\"future\"]/relativeTimePattern[@count=\"other\"]");
}

static final class MissingKey implements Comparable<MissingKey> {
final SectionId sectionId;
final PageId pageId;
Expand Down

0 comments on commit 3515383

Please sign in to comment.