-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLDR-5854 Add-examples-for-relative-dates-and-times #4060
Merged
srl295
merged 1 commit into
unicode-org:main
from
haytenf:CLDR-5854-Add-examples-for-relative-dates-and-times
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -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")) { | ||
|
@@ -3027,6 +3044,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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to make this "jm" or "Cm" to use hour cycle prefs based on locale. That can be done under CLDR-18025 "Further work on examples for relative dates and times". |
||
} 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need to make this map dependent on locale. That can be done under CLDR-18025 "Further work on examples for relative dates and times".