|
| 1 | +/** |
| 2 | + * @fileoverview Intl stub polyfill - Install all Intl stubs if Intl is missing. |
| 3 | + * |
| 4 | + * This module installs minimal Intl stubs when Node.js is built with --with-intl=none. |
| 5 | + * It provides basic internationalization functionality sufficient for CLI tools. |
| 6 | + */ |
| 7 | + |
| 8 | +export { CollatorStub } from './collator.mts' |
| 9 | +export { DateTimeFormatStub } from './date-time-format.mts' |
| 10 | +export { DisplayNamesStub } from './display-names.mts' |
| 11 | +export { ListFormatStub } from './list-format.mts' |
| 12 | +export { LocaleStub } from './locale.mts' |
| 13 | +export { NumberFormatStub } from './number-format.mts' |
| 14 | +export { PluralRulesStub } from './plural-rules.mts' |
| 15 | +export { RelativeTimeFormatStub } from './relative-time-format.mts' |
| 16 | +export { SegmenterStub } from './segmenter.mts' |
| 17 | + |
| 18 | +import { CollatorStub } from './collator.mts' |
| 19 | +import { DateTimeFormatStub } from './date-time-format.mts' |
| 20 | +import { DisplayNamesStub } from './display-names.mts' |
| 21 | +import { getCanonicalLocales, supportedValuesOf } from './helpers.mts' |
| 22 | +import { ListFormatStub } from './list-format.mts' |
| 23 | +import { LocaleStub } from './locale.mts' |
| 24 | +import { NumberFormatStub } from './number-format.mts' |
| 25 | +import { PluralRulesStub } from './plural-rules.mts' |
| 26 | +import { RelativeTimeFormatStub } from './relative-time-format.mts' |
| 27 | +import { SegmenterStub } from './segmenter.mts' |
| 28 | + |
| 29 | +/** |
| 30 | + * Install Intl stubs globally if Intl is not defined. |
| 31 | + * This happens automatically when this module is imported. |
| 32 | + */ |
| 33 | +if (typeof globalThis.Intl === 'undefined') { |
| 34 | + // Create Intl global object with all stubs. |
| 35 | + ;(globalThis as typeof globalThis & { Intl: typeof Intl }).Intl = { |
| 36 | + Collator: CollatorStub as unknown as typeof Intl.Collator, |
| 37 | + DateTimeFormat: DateTimeFormatStub as unknown as typeof Intl.DateTimeFormat, |
| 38 | + DisplayNames: DisplayNamesStub as unknown as typeof Intl.DisplayNames, |
| 39 | + ListFormat: ListFormatStub as unknown as typeof Intl.ListFormat, |
| 40 | + Locale: LocaleStub as unknown as typeof Intl.Locale, |
| 41 | + NumberFormat: NumberFormatStub as unknown as typeof Intl.NumberFormat, |
| 42 | + PluralRules: PluralRulesStub as unknown as typeof Intl.PluralRules, |
| 43 | + RelativeTimeFormat: |
| 44 | + RelativeTimeFormatStub as unknown as typeof Intl.RelativeTimeFormat, |
| 45 | + Segmenter: SegmenterStub as unknown as typeof Intl.Segmenter, |
| 46 | + |
| 47 | + // Static methods. |
| 48 | + getCanonicalLocales, |
| 49 | + supportedValuesOf, |
| 50 | + } |
| 51 | + |
| 52 | + // Make it non-configurable like the real Intl. |
| 53 | + Object.defineProperty(globalThis, 'Intl', { |
| 54 | + configurable: false, |
| 55 | + enumerable: false, |
| 56 | + value: (globalThis as typeof globalThis & { Intl: typeof Intl }).Intl, |
| 57 | + writable: false, |
| 58 | + }) |
| 59 | +} |
0 commit comments