Skip to content

Commit

Permalink
feat(@formatjs/intl-displaynames): Use native Intl.getCanonicalLocales
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This requires @formatjs/intl-getcanonicallocales for
IE11 and below
  • Loading branch information
longlho committed May 23, 2020
1 parent ab2318a commit 4056fbd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/intl-displaynames/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"chalk": "^4.0.0",
"formatjs-extract-cldr-data": "^10.1.8",
"jest": "^25.4.0",
"ts-jest": "^25.4.0"
"ts-jest": "^25.4.0",
"@formatjs/intl-getcanonicallocales": "1.1.1"
},
"scripts": {
"cldr": "ts-node scripts/cldr",
Expand Down
12 changes: 8 additions & 4 deletions packages/intl-displaynames/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
getInternalSlot,
setInternalSlot,
getOption,
getCanonicalLocales,
createResolveLocale,
invariant,
supportedLocales,
Expand All @@ -13,6 +12,7 @@ import {
DisplayNamesData,
toString,
} from '@formatjs/intl-utils';
import type {getCanonicalLocales} from '@formatjs/intl-getcanonicallocales';

export interface DisplayNamesOptions {
localeMatcher?: 'lookup' | 'best fit';
Expand All @@ -36,7 +36,8 @@ export class DisplayNames {
if (new.target === undefined) {
throw TypeError(`Constructor Intl.DisplayNames requires 'new'`);
}
const requestedLocales = getCanonicalLocales(locales);
const requestedLocales = ((Intl as any)
.getCanonicalLocales as typeof getCanonicalLocales)(locales);

const matcher = getOption(
options!,
Expand Down Expand Up @@ -97,7 +98,9 @@ export class DisplayNames {
) {
return supportedLocales(
DisplayNames.availableLocales,
getCanonicalLocales(locales),
((Intl as any).getCanonicalLocales as typeof getCanonicalLocales)(
locales
),
options
);
}
Expand Down Expand Up @@ -150,7 +153,8 @@ export class DisplayNames {
switch (type) {
// Normalize the locale id and remove the region.
case 'language': {
canonicalCode = getCanonicalLocales(codeAsString)[0];
canonicalCode = ((Intl as any)
.getCanonicalLocales as typeof getCanonicalLocales)(codeAsString)[0];
const regionMatch = /-([a-z]{2}|\d{3})\b/i.exec(canonicalCode);
if (regionMatch) {
// Remove region subtag
Expand Down

0 comments on commit 4056fbd

Please sign in to comment.