From a0c63da8b663defc3665b1dddd63081d46dcae1a Mon Sep 17 00:00:00 2001 From: Helena Aytenfisu Date: Tue, 27 Aug 2024 16:28:21 -0400 Subject: [PATCH] CLDR-5854 First attempt with examples for two cases per item --- .../unicode/cldr/test/ExampleGenerator.java | 59 +++++++++++++++++++ .../cldr/unittest/TestExampleGenerator.java | 19 ++++++ 2 files changed, 78 insertions(+) diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java b/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java index a83dad880e8..707aa05c0a3 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/test/ExampleGenerator.java @@ -319,6 +319,19 @@ public void setCachingEnabled(boolean enabled) { } }; + // map relativeTimePattern counts to numeric examples + public static final Map COUNTS = + new HashMap() { + { + put("zero", "0"); + put("one", "1"); + put("two", "2"); + put("few", "3"); + put("many", "5"); + put("other", "10"); + } + }; + public CLDRFile getCldrFile() { return cldrFile; } @@ -517,6 +530,10 @@ private String constructExampleHtml(String xpath, String value, boolean nonTrivi result = handleEras(parts, value); } else if (parts.contains("quarters")) { result = handleQuarters(parts, value); + } else if (parts.contains("relative") + || parts.contains("relativeTime") + || parts.contains("relativePeriod")) { + result = handleRelative(xpath, parts, value); } else if (parts.contains("dayPeriods")) { result = handleDayPeriod(parts, value); } else if (parts.contains("monthContext")) { @@ -3008,6 +3025,48 @@ private String handleQuarters(XPathParts parts, String value) { return 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 String handleRelative(String xpath, XPathParts parts, String value) { + List examples = new ArrayList<>(); + 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)); + } + return formatExampleList(examples); + } + /** * @param elementToOverride the element that is to be overridden * @param element the overriding element diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java index cdfb17560b0..9adc2dbf667 100644 --- a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java @@ -1799,6 +1799,25 @@ public void TestQuarterFormats() { "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/quarters/quarterContext[@type=\"stand-alone\"]/quarterWidth[@type=\"abbreviated\"]/quarter[@type=\"4\"]"); } + public void TestRelative() { + ExampleGenerator exampleGenerator = getExampleGenerator("it"); + checkValue( + "it relative day type 2", + "〖Dopodomani (5 settembre)〗〖5 settembre (dopodomani)〗", + exampleGenerator, + "//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)〗", + exampleGenerator, + "//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)〗", + exampleGenerator, + "//ldml/dates/fields/field[@type=\"year\"]/relativeTime[@type=\"past\"]/relativeTimePattern[@count=\"one\"]"); + } + static final class MissingKey implements Comparable { final SectionId sectionId; final PageId pageId;