-
-
Notifications
You must be signed in to change notification settings - Fork 103
feat(core): Global lazy Intl polyfills and locale-sensitive date formatting #1608
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
Merged
+202
−22
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fbbdbf1
fix(core): Resolve Webpack build errors by using relative paths for I…
erral f6521ba
tech(core): Integrate lazy Intl polyfills into multiple patterns
erral 5a09b8c
fix(core): Use Webpack alias for Intl locale data to avoid build warn…
erral c377809
maint(core): Add functional tests for lazy Intl polyfill loading
erral 78e511d
fix(pat-filemanager): Add error handling and fallback to formatDate
erral 9c5107d
fix(core): remove unused variable in intl-loader catch block
erral d4b9090
Merge branch 'master' into fix-global-intl-polyfills
MrTango bd470b8
Update src/pat/filemanager/src/App.svelte
erral 7f2ecd3
Apply suggestion from @MrTango
MrTango d16233f
fix(pat-filemanager): suggestion merge issue
MrTango 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 hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * Global Intl polyfill loader for Plone Mockup. | ||
| * Detects if the current browser supports the site's language and lazily | ||
| * loads the required polyfills and locale data if not. | ||
| */ | ||
|
|
||
| export async function ensureIntlSupport(lang) { | ||
| if (!lang) return; | ||
| const normalizedLang = lang.replace("_", "-"); | ||
| const baseLang = normalizedLang.split("-")[0]; | ||
|
|
||
| // Check if natively supported | ||
| try { | ||
| if ( | ||
| typeof Intl !== "undefined" && | ||
| Intl.DateTimeFormat && | ||
| Intl.DateTimeFormat.supportedLocalesOf(normalizedLang).length > 0 | ||
| ) { | ||
| return; | ||
| } | ||
| } catch { | ||
| // Fall through to loading polyfill if supportedLocalesOf fails | ||
| } | ||
|
|
||
| console.info(`Locale "${normalizedLang}" not supported. Loading polyfill...`); | ||
|
|
||
| // Load polyfill core if native support is missing for this locale. | ||
| try { | ||
| // Use polyfill-force to ensure we get a version that supports adding locale data, | ||
| // as native versions might not have the hooks for the locale-data files. | ||
| await import("@formatjs/intl-datetimeformat/polyfill-force.js"); | ||
| } catch (e) { | ||
| console.error("Failed to load Intl polyfill core", e); | ||
| } | ||
|
|
||
| // Load specific locale data via Webpack dynamic chunk. | ||
| try { | ||
| // Use the package name with explicit .js extension. | ||
| // We have an alias in webpack.config.js to help resolve this path correctly | ||
| // without triggering package export warnings in Webpack 5. | ||
| await import(`@formatjs/intl-datetimeformat/locale-data/${baseLang}.js`); | ||
|
|
||
| if (Intl.DateTimeFormat.supportedLocalesOf(normalizedLang).length > 0) { | ||
| console.info(`Locale "${normalizedLang}" is now supported.`); | ||
| } | ||
| } catch (e) { | ||
| console.warn(`Could not load Intl data for ${baseLang}`, e); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { ensureIntlSupport } from "./intl-loader"; | ||
|
|
||
| describe("intl-loader", () => { | ||
| let originalDateTimeFormat; | ||
|
|
||
| beforeAll(() => { | ||
| originalDateTimeFormat = Intl.DateTimeFormat; | ||
| jest.spyOn(console, "info").mockImplementation(() => {}); | ||
| jest.spyOn(console, "warn").mockImplementation(() => {}); | ||
| jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| Intl.DateTimeFormat = originalDateTimeFormat; | ||
| console.info.mockRestore(); | ||
| console.warn.mockRestore(); | ||
| console.error.mockRestore(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| // Reset to original before each test to have a clean state | ||
| // Note: some polyfill effects might persist if they touch other global objects | ||
| Intl.DateTimeFormat = originalDateTimeFormat; | ||
| }); | ||
|
|
||
| it("should detect supported locales correctly", async () => { | ||
| // 'en' should be supported | ||
| await ensureIntlSupport("en"); | ||
| expect(console.info).not.toHaveBeenCalledWith(expect.stringContaining("not supported")); | ||
| }); | ||
|
|
||
| it("should handle locale normalization", async () => { | ||
| await ensureIntlSupport("pt_BR"); | ||
| // pt-BR is likely supported, but we just check it doesn't crash | ||
| }); | ||
|
|
||
| it("should load polyfill and provide Basque (eu) formatting", async () => { | ||
| // We force a mock that says 'eu' is NOT supported | ||
| const mockSupportedLocalesOf = jest.fn().mockImplementation((locales) => { | ||
| const l = Array.isArray(locales) ? locales[0] : locales; | ||
| if (l.startsWith('eu')) return []; | ||
| return originalDateTimeFormat.supportedLocalesOf(locales); | ||
| }); | ||
|
|
||
| // We need to mock the property because it might be a getter | ||
| Object.defineProperty(Intl, 'DateTimeFormat', { | ||
| value: class extends originalDateTimeFormat { | ||
| static supportedLocalesOf = mockSupportedLocalesOf; | ||
| }, | ||
| configurable: true | ||
| }); | ||
|
|
||
| await ensureIntlSupport("eu"); | ||
|
|
||
| expect(mockSupportedLocalesOf).toHaveBeenCalled(); | ||
| expect(console.info).toHaveBeenCalledWith(expect.stringContaining("not supported")); | ||
|
|
||
| // After ensureIntlSupport, Intl.DateTimeFormat should have been replaced by the polyfill | ||
| // since we used polyfill-force.js (or at least it was called). | ||
|
|
||
| // Verify formatting using UTC to avoid timezone shifts | ||
| const date = new Date(Date.UTC(2020, 5, 1)); // June 1st UTC | ||
| const dtf = new Intl.DateTimeFormat("eu", { month: "short", timeZone: "UTC" }); | ||
| const formatted = dtf.format(date); | ||
|
|
||
| // Basque short month for June is 'eka.' | ||
| expect(formatted.toLowerCase()).toContain("eka"); | ||
| }); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.