docusaurus/packages/docusaurus-utils/src/codeTranslationsUtils.ts
Sébastien Lorber b743edf5fb
feat(v2): default theme translations: locale "pt" should load "pt-BR" translations (#4581)
* code translation utils => locale "pt" should attempt to load "pt-BR" (not "pt-PT")

* useless async test
2021-04-07 18:28:48 +02:00

56 lines
1.9 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import fs from 'fs-extra';
// Return an ordered list of locales we should try
export function codeTranslationLocalesToTry(locale: string): string[] {
// @ts-expect-error: TODO until available in TS, see https://github.com/microsoft/TypeScript/issues/37326
const intlLocale = Intl.Locale ? new Intl.Locale(locale) : undefined;
if (!intlLocale) {
return [locale];
}
// if locale is just a simple language like "pt", we want to fallback to pt-BR (not pt-PT!)
// see https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
if (intlLocale.language === locale) {
const maximizedLocale = intlLocale.maximize(); // pt-Latn-BR`
// ["pt","pt-BR"]
return [locale, `${maximizedLocale.language}-${maximizedLocale.region}`];
}
// if locale is like "pt-BR", we want to fallback to "pt"
else {
return [locale, intlLocale.language];
}
}
// Useful to implement getDefaultCodeTranslationMessages() in themes
export async function readDefaultCodeTranslationMessages({
dirPath,
locale,
}: {
dirPath: string;
locale: string;
}): Promise<Record<string, string>> {
const localesToTry = codeTranslationLocalesToTry(locale);
// Return the content of the first file that match
// fr_FR.json => fr.json => nothing
// eslint-disable-next-line no-restricted-syntax
for (const fileName of localesToTry) {
const filePath = path.resolve(dirPath, `${fileName}.json`);
// eslint-disable-next-line no-await-in-loop
if (await fs.pathExists(filePath)) {
// eslint-disable-next-line no-await-in-loop
const fileContent = await fs.readFile(filePath, 'utf8');
return JSON.parse(fileContent);
}
}
return {};
}