Skip to content

Commit

Permalink
fix(Intl): fall back to ISO language 2-code name when the expanded va…
Browse files Browse the repository at this point in the history
…riant isn't available
  • Loading branch information
wessberg committed Oct 22, 2020
1 parent 7436ca4 commit 6545898
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/service/polyfill-builder/polyfill-builder-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ import {IPolyfillDictEntryBase} from "../../polyfill/polyfill-dict";
import {PolyfillContext} from "../../polyfill/polyfill-context";
import {IPolyfillRequest} from "../../polyfill/i-polyfill-request";

function normalizeIntlLocale(locale: string): string {
const localeData = new (Intl as any).Locale(locale);
switch (localeData.basename) {
case "pt-BR":
// @formatjs does not provide data for brazilian Portuguese,
// so we'll have to fall back to the base language here
// TODO: Remove this when @formatjs eventually supports pt-BR.
// Track this issue: https://github.com/formatjs/formatjs/issues/2237
return localeData.language;
default:
// For all other languages, use the base name
return localeData.basename;
}
}

const SYNC_OPTIONS = {cwd: __dirname};

function selectMetaPaths<Meta extends Exclude<IPolyfillDictEntryBase["meta"], undefined>>(value: Meta[keyof Meta], context: PolyfillContext): string[] {
Expand Down Expand Up @@ -206,12 +191,20 @@ export class PolyfillBuilderService implements IPolyfillBuilderService {
requestedLocales.map(async requestedLocale => {
// Resolve the absolute path
for (const localeDir of selectMetaPaths(meta.localeDir, request.context)) {
const localePathInput = join(rootDirectory, localeDir, `${normalizeIntlLocale(requestedLocale)}.js`);
const resolvedLocalePath = sync(localePathInput, SYNC_OPTIONS);
if (resolvedLocalePath != null) {
absolutePaths.push(resolvedLocalePath);
} else {
throw new ReferenceError(`Unresolved path: '${localePathInput}'`);
const locale = new (Intl as any).Locale(requestedLocale);
let addedLocale = false;
for (const currentLocale of [locale.baseName, locale.language]) {
const localePathInput = join(rootDirectory, localeDir, `${currentLocale}.js`);
const resolvedLocalePath = sync(localePathInput, SYNC_OPTIONS);
if (resolvedLocalePath != null) {
addedLocale = true;
absolutePaths.push(resolvedLocalePath);
break;
}
}

if (!addedLocale) {
throw new ReferenceError(`Unresolved locale: '${requestedLocale}'`);
}
}
})
Expand Down

0 comments on commit 6545898

Please sign in to comment.