Rewrite dateTime.js to use current localeData#378
Conversation
| 'AZT': '+0400', | ||
| 'B': '+0200', | ||
| 'BDST': '+0100', | ||
| 'BDST': '+0200', |
There was a problem hiding this comment.
These were just mistakes you noticed?
There was a problem hiding this comment.
"Mistakes" AI noticed, but I don't trust it understands the context. I'll make a story to review these, but will revert these from this PR.
| 'short': convertCldrDatePattern(localeData.dateFormatItems.yMd, true), | ||
| 'monthYear': convertCldrDatePattern(localeData.dateFormatItems.yMMMM, false), | ||
| 'monthDay': convertCldrDatePattern(localeData.dateFormatItems.MMMMd, false), | ||
| 'shortMonthDay': convertCldrDatePattern(localeData.dateFormatItems.MMMd, false), |
There was a problem hiding this comment.
This stuff ends up all being static eh? Wonder if it'd be useful doing these calculations at build-time instead of runtime and storing them in locale-data.js?
There was a problem hiding this comment.
I'll look at this, but it's cached after the first run. I do want to avoid storing converted data in locale-data/ so we just have a clean source of truth. A lot of this may go away very soon too with the skeleton work coming up, so we can look at it again then.
fc0bc46 to
b46a9c9
Compare
2490e5a to
be95d77
Compare
be95d77 to
22568a3
Compare
6b3fda0 to
7a5b68a
Compare
7a5b68a to
164c714
Compare
b38836d to
b0ff22e
Compare
50b052a to
946b23b
Compare
946b23b to
d3d4765
Compare
| const documentLocaleSettings = getDocumentLocaleSettings(); | ||
|
|
||
| let currentLocale; | ||
| let localeDataPromise = Promise.resolve(); |
There was a problem hiding this comment.
A promise that can be overwritten each time the locale changes
| export const localeData = new Proxy(localeDataTarget, { | ||
| deleteProperty() { return false; }, | ||
| defineProperty() { return false; } | ||
| }); |
There was a problem hiding this comment.
Since we can't Object.freeze the shared object (so we can update its properties), we proxy it and disallow (re)defining and deleting properties outside this module.
|
|
||
| Object.defineProperty(localeDataTarget, 'then', { value: (...args) => localeDataPromise.then(...args) }); | ||
| Object.defineProperty(localeDataTarget, 'catch', { value: (...args) => localeDataPromise.catch(...args) }); | ||
| Object.defineProperty(localeDataTarget, 'finally', { value: (...args) => localeDataPromise.finally(...args) }); |
There was a problem hiding this comment.
Make localeDataTarget thenable, always redirecting to the dynamic localeDataPromise. We use defineProperty here mostly to prevent enumerability. i.e. before first resolution, Object.keys(localeData) // -> []
This means localeData acts as its own promise, and allows existing and future intl functions like formatDate to remain synchronous, as long as there is a top-level await localeData anywhere before they can be called, which we already do right at the end of this module. Shifting that responsibility to consumers is an option that would prevent blocking while the initial locale data loads, but in practical terms would offer little to no benefit as something else is likely to be imported that already top-level awaits it. After that, any consumer is responsible for managing updates:
// library
import { localeData } from '@brightspace-ui/intl/lib/locale-data/current.js';
import { resolveSupportedLocale } from '@brightspace-ui/intl/lib/locale-data/supported.js';
// current.js already awaited the initial locale's data
export function getMonths(format) {
const months = localeData.months.standAlone[format];
}
export async function changeLanguage(lang) {
document.documentElement.lang = resolveSupportedLocale(lang);
await localeData;
}// component/mixin
...
import { localeData } from '@brightspace-ui/intl/lib/locale-data/current.js';
class MyComponent ... {
...
connectedCallback() {
this.connectedCallback();
this.addEventListener('d2l-locale-data-change', () => requestUpdate('localeData'));
}
render() {
return html`
<ul>${localeData.months.standAlone.wide.map(m => html`
<li>${m}</li>`)}
</ul>
`;
}
}| localeDataPromise = setLocaleData(); | ||
| return localeDataPromise; |
There was a problem hiding this comment.
Update the double proxied promise pointer
| setLocaleData(); | ||
| registerLocaleDataListener(); | ||
| await updateLocaleData(); | ||
| Object.preventExtensions(localeDataTarget); |
There was a problem hiding this comment.
The proxy already prevents consumers adding new properties, but this ensures we aren't adding extraneous properties when we change locales. We can probably remove this in favor of more tests once generating/modifying the locale data files is more settled down, but for now it's useful to catch inconsistencies there.
| delete settings.overrides.date.calendar.months; | ||
| delete settings.overrides.date.calendar.days; | ||
| } | ||
| return merge(JSON.parse(JSON.stringify(descriptor)), settings.overrides.date); |
There was a problem hiding this comment.
Prevents accidental/attempted overwriting of localeData subproperties since without this they would be direct references. We can't use structuredClone yet. Thanks Safari!
GAUD-9717: Rewrite intl/common and intl/dateTime to use contained data sets per locale
Run #1
locale-data/644 failed tests
Run #2
documentLocaleSettingsresetmethod so it runs listeners when it "resets" (changes) the language back to English. This is only used in tests.locale-data/to match the CLDR source names and update how the files are matched for loading304 failed tests
Run #3
242 failed tests
Run #4
LLLL- Stand-alone month in year (e.g. Catalan:MMMM-> "d'abril",LLLL-> "abril")cccc- Stand-alone day of weekK- Hour in day, 12 hour, 0-11 (e.g. "0:29 pm")225 failed tests
Run #5
locale-data/files to maintain existing time formats (minus whitespace improvements and legitimate bugs)ko), addoverridesfiles and rework how they are applied102 test failures
Run #6
locale-data/files to maintain existing date formats (minus whitespace improvements and legitimate bugs)ptandpt-BR0 failed tests