-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PluralRules locale data shimming (#200)
* Remove @formatjs/intl-pluralrule * Add custom PluralRules * Add more tests; Fix supportedLocalesOf and getCanonicalLocales * Add localize test * Fix lint * Cleanup * Remove redundant getCanonicalLocales call
- Loading branch information
1 parent
2b8d554
commit e6f11dd
Showing
7 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,78 @@ | ||
const localeData = { | ||
mi: { | ||
aliases: ['mri', 'mao'], | ||
options: { | ||
cardinal: { | ||
locale: 'mi', | ||
pluralCategories : [ 'one', 'other' ], | ||
shim: true | ||
}, | ||
ordinal: { | ||
locale: 'mi', | ||
pluralCategories : [ 'other' ], | ||
shim: true | ||
}, | ||
}, | ||
select(n, ord) { | ||
return !ord && n === 1 ? 'one' : 'other'; | ||
} | ||
} | ||
}; | ||
|
||
function getCanonicalLocales(locales) { | ||
const mappedLocales = [locales].flat().map(locale => { | ||
for (const canonicalLocale in localeData) { | ||
if (localeData[canonicalLocale].aliases.includes(locale)) { | ||
return canonicalLocale; | ||
} | ||
} | ||
return locale; | ||
}); | ||
return Intl.getCanonicalLocales(mappedLocales); | ||
} | ||
|
||
class PluralRules extends Intl.PluralRules { | ||
|
||
static shim = true; | ||
static supportedLocalesOf(locales) { | ||
return [locales].flat().map(l => { | ||
const canonicalLocale = getCanonicalLocales(l)[0]; | ||
if (localeData[canonicalLocale]) { | ||
return canonicalLocale; | ||
} | ||
return super.supportedLocalesOf(l); | ||
}).flat(); | ||
} | ||
#localeData; | ||
#locale; | ||
#type; | ||
|
||
constructor(locales, options = {}) { | ||
super(locales, options); | ||
this.#locale = PluralRules.supportedLocalesOf(locales)[0]; | ||
this.#type = options.type ?? 'cardinal'; | ||
if (localeData[this.#locale]) { | ||
this.#localeData = localeData[this.#locale]; | ||
} | ||
} | ||
|
||
resolvedOptions() { | ||
return { ...super.resolvedOptions(), ...this.#localeData?.options[this.#type] }; | ||
} | ||
|
||
select(n) { | ||
if (this.#localeData) { | ||
return this.#localeData.select(n, this.#type === 'ordinal'); | ||
} else { | ||
return super.select(n); | ||
} | ||
} | ||
|
||
} | ||
|
||
Object.defineProperty(Intl, 'PluralRules', { | ||
value: PluralRules, | ||
writable: true, | ||
enumerable: false, | ||
configurable: true, | ||
}); |
This file contains 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 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 |
---|---|---|
|
@@ -44,7 +44,6 @@ | |
"sinon": "^19.0.2" | ||
}, | ||
"dependencies": { | ||
"@formatjs/intl-pluralrules": "^1", | ||
"intl-messageformat": "^10" | ||
} | ||
} |
This file contains 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,79 @@ | ||
import '../lib/PluralRules.js'; | ||
import { expect } from '@brightspace-ui/testing'; | ||
import { getDocumentLocaleSettings } from '../lib/common.js'; | ||
|
||
describe('PluralRules', () => { | ||
|
||
const documentLocaleSettings = getDocumentLocaleSettings(); | ||
|
||
afterEach(() => documentLocaleSettings.reset()); | ||
|
||
it('extends native Intl.PluralRules', () => { | ||
const native = Object.getPrototypeOf(Intl.PluralRules); | ||
expect(Intl.PluralRules.shim).to.be.true; | ||
expect(native).to.have.property('name', 'PluralRules'); | ||
expect(native).to.not.have.property('shim'); | ||
}); | ||
|
||
it('uses native data by default', () => { | ||
const shim = new Intl.PluralRules('cy'); | ||
const native = new (Object.getPrototypeOf(Intl.PluralRules))('cy'); | ||
expect(shim.resolvedOptions()).to.deep.equal(native.resolvedOptions()); | ||
expect(shim.select(2)).to.equal('two'); | ||
}); | ||
|
||
it('resolves to canonical locales', () => { | ||
expect(new Intl.PluralRules('mao').resolvedOptions().locale).to.equal('mi'); | ||
expect(new Intl.PluralRules('mri').resolvedOptions().locale).to.equal('mi'); | ||
expect(new Intl.PluralRules(['abcdefg', 'mri']).resolvedOptions().locale).to.equal('mi'); | ||
}); | ||
|
||
it('includes custom locales as supported', () => { | ||
expect(Intl.PluralRules.supportedLocalesOf(['abc', 'mao', 'en'])).to.deep.equal(['mi', 'en']); | ||
}); | ||
|
||
[ | ||
{ | ||
locale: 'mi', | ||
type: 'cardinal', | ||
options: { | ||
locale: 'mi', | ||
shim: true, | ||
pluralCategories: [ 'one', 'other' ] | ||
}, | ||
select: { | ||
one: [1], | ||
other: [0, 2, 3, 11] | ||
} | ||
}, | ||
{ | ||
locale: 'mi', | ||
type: 'ordinal', | ||
options: { | ||
locale: 'mi', | ||
shim: true, | ||
pluralCategories: [ 'other' ] | ||
}, | ||
select: { | ||
other: [0, 1, 2, 3, 11] | ||
} | ||
} | ||
].forEach(({ locale, type, options, select }) => { | ||
|
||
documentLocaleSettings.language = locale; | ||
const pluralRules = new Intl.PluralRules(locale, { type }); | ||
|
||
it(`should use custom ${type} data for "${locale}"`, () => { | ||
expect(pluralRules.resolvedOptions()).to.deep.include(options); | ||
}); | ||
|
||
it(`should select the correct ${type} number categories for "${locale}"`, () => { | ||
options.pluralCategories.forEach(cat => { | ||
select[cat].forEach(num => { | ||
expect(pluralRules.select(num)).to.equal(cat); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
This file contains 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