-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Updated @tryghost/i18n to support ESM #29144
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
+475
−135
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d37f18
SPIKE: dual-format @tryghost/i18n (CJS fs-backed + ESM static registry)
9larsons 36aea48
Added dual-format @tryghost/i18n with per-namespace ESM registries
9larsons 3e9429a
Updated app tests to i18n per-namespace subpath imports
9larsons e1ea74c
Applied review polish to @tryghost/i18n dual-format
9larsons 1851928
Fixed require leak crashing public apps under the i18n ESM registry path
9larsons 565d8a6
Cleaned up @tryghost/i18n dual-format tooling per review
9larsons 90734c5
Replaced generated locale registry with Vite import.meta.glob
9larsons 6ce110d
Moved shared registry wiring into the esm-factory
9larsons 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
2 changes: 1 addition & 1 deletion
2
apps/comments-ui/test/unit/components/content/pagination.test.jsx
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| declare module '@tryghost/i18n'; | ||
| declare module '@tryghost/i18n/registry/signup-form'; |
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
This file was deleted.
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,27 @@ | ||
| import type {i18n as I18nextInstance} from 'i18next'; | ||
|
|
||
| export interface LocaleDataEntry { | ||
| code: string; | ||
| name: string; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| export type Namespace = 'ghost' | 'portal' | 'signup-form' | 'comments' | 'search' | 'theme'; | ||
|
|
||
| export interface I18nFactory { | ||
| (locale?: string, ns?: Namespace | string, options?: Record<string, unknown>): I18nextInstance; | ||
| LOCALE_DATA: LocaleDataEntry[]; | ||
| SUPPORTED_LOCALES: string[]; | ||
| generateResources(locales: string[], ns: string): Record<string, Record<string, Record<string, string>>>; | ||
| default: I18nFactory; | ||
| } | ||
|
|
||
| declare const i18n: I18nFactory; | ||
|
|
||
| export default i18n; | ||
| export declare const LOCALE_DATA: LocaleDataEntry[]; | ||
| export declare const SUPPORTED_LOCALES: string[]; | ||
| export declare function generateResources( | ||
| locales: string[], | ||
| ns: string | ||
| ): Record<string, Record<string, Record<string, string>>>; |
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,63 @@ | ||
| /** | ||
| * Shared ESM factory for the browser/static-registry build. | ||
| * | ||
| * Given a single-namespace registry (locale-code -> resource JSON), returns an | ||
| * i18n factory with the SAME call signature the CJS package exposes: | ||
| * i18n(locale, ns, options) -> initialised i18next instance | ||
| * | ||
| * Theme resources are stubbed to `{}` — themes are a Node-only, fs-backed concept | ||
| * and never reach the browser (matches the old index.browser.js). | ||
| * | ||
| * The namespace is fixed at build time by which per-namespace registry module is | ||
| * imported, so bundlers include ONLY that namespace's locale files. | ||
| * | ||
| * Note: this imports the ESM core (./i18n-core.mjs), NOT the CJS ./i18n-core.js. | ||
| * The CJS core uses require('i18next'), which leaks a bare `require(...)` into the | ||
| * browser UMD bundle and throws "require is not defined" at load. | ||
| */ | ||
| import * as i18nCore from './i18n-core.mjs'; | ||
|
|
||
| const {createI18n, createGenerateResources, LOCALE_DATA, SUPPORTED_LOCALES} = i18nCore; | ||
|
|
||
| /** | ||
| * Shared body of every per-namespace browser entry. Collects a Vite | ||
| * `import.meta.glob` result (path -> resource JSON) into a locale-keyed registry | ||
| * and builds the namespaced i18n instance. | ||
| * | ||
| * This exists because Vite requires the `import.meta.glob` pattern to be a string | ||
| * literal, so each namespace needs its own tiny entry to hold that literal — but | ||
| * the parsing/wiring is identical, so it lives here once. | ||
| */ | ||
| export function i18nFromGlob(globModules, namespace) { | ||
| const registry = {}; | ||
| for (const [filePath, resource] of Object.entries(globModules)) { | ||
| registry[filePath.match(/\/locales\/([^/]+)\//)[1]] = resource; | ||
| } | ||
| return createNamespacedI18n(registry, namespace); | ||
| } | ||
|
|
||
| export function createNamespacedI18n(registry, boundNamespace) { | ||
| // Registry loader: returns undefined for an unknown locale so the core falls | ||
| // back to English (mirrors the CJS try/catch fallback). | ||
| const generateResources = createGenerateResources(locale => registry[locale]); | ||
|
|
||
| function generateThemeResources(lng) { | ||
| return { | ||
| [lng]: { | ||
| theme: {} | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const i18n = createI18n({generateResources, generateThemeResources}); | ||
|
|
||
| i18n.LOCALE_DATA = LOCALE_DATA; | ||
| i18n.SUPPORTED_LOCALES = SUPPORTED_LOCALES; | ||
| i18n.generateResources = generateResources; | ||
| i18n.namespace = boundNamespace; | ||
| i18n.default = i18n; | ||
|
|
||
| return i18n; | ||
| } | ||
|
|
||
| export {LOCALE_DATA, SUPPORTED_LOCALES}; |
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.
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.