Support CLDR 48 relative date formats - #1306
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1306 +/- ##
==========================================
+ Coverage 92.19% 92.21% +0.02%
==========================================
Files 27 27
Lines 4764 4779 +15
==========================================
+ Hits 4392 4407 +15
Misses 372 372
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR updates Babel to take advantage of new CLDR 48 data for relative date wording and locale-specific patterns for combining relative dates with times (e.g., “tomorrow at 3:30 PM”).
Changes:
- Add
dates.get_relative_name()to retrieve CLDR literal relative names for specific offsets (e.g., yesterday/today/tomorrow; weekday-relative). - Add
dates.format_relative_datetime()and import CLDR 48 relative datetime combination patterns. - Expand documentation and add comprehensive tests across multiple locales and widths.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_dates.py | Adds coverage for relative-name retrieval and relative date+time formatting behavior across locales/lengths. |
| scripts/import_cldr.py | Extends CLDR import to ingest literal relative names and relative datetime combination patterns. |
| docs/api/dates.rst | Exposes new APIs in the generated documentation. |
| babel/dates.py | Implements get_relative_name() and format_relative_datetime(). |
| babel/core.py | Adds Locale.datetime_formats_relative accessor for CLDR 48 relative datetime patterns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| .. versionadded:: 2.19 | ||
| """ | ||
| return self._data['datetime_formats_relative'] |
| locale = Locale.parse(locale or LC_TIME) | ||
| pattern = ( | ||
| locale.datetime_formats_relative.get(format) | ||
| or locale.datetime_formats[format] | ||
| ) |
| assert dates.format_relative_datetime( | ||
| 'demain', time(15, 30), format='full', locale='fr', | ||
| ) == 'demain à 15:30:00 temps universel coordonné' | ||
| # Czech has no relative pattern, so falls back to datetime_formats |
There was a problem hiding this comment.
Surprised there are no relative patterns since they should work fine here, but I guess they'll add them eventually..
| locale = Locale.parse(locale or LC_TIME) | ||
| date_fields = locale._data['date_fields'] | ||
| key = field if length == 'long' else f'{field}-{length}' | ||
| data = date_fields.get(key) or date_fields.get(field) |
There was a problem hiding this comment.
So if the specified length does not exist, you get the long version by default?
New data in CLDR 48, let's make use of it!