mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 12:22:45 +02:00
refactor: define own translations in other themes (#5849)
Co-authored-by: Armano <armano2@users.noreply.github.com>
This commit is contained in:
parent
87a486a02f
commit
63bd6b9025
120 changed files with 750 additions and 446 deletions
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"id1": "message 1 en",
|
||||
"id2": "message 2 en"
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"id1": "message 1 fr_FR",
|
||||
"id2": "message 2 fr_FR",
|
||||
"id3": "message 3 fr_FR"
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"id1": "message 1 fr",
|
||||
"id2": "message 2 fr"
|
||||
}
|
|
@ -1,112 +0,0 @@
|
|||
/**
|
||||
* 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';
|
||||
import {
|
||||
codeTranslationLocalesToTry,
|
||||
readDefaultCodeTranslationMessages,
|
||||
} from '../codeTranslationsUtils';
|
||||
|
||||
describe('codeTranslationLocalesToTry', () => {
|
||||
test('should return appropriate locale lists', () => {
|
||||
expect(codeTranslationLocalesToTry('fr')).toEqual(['fr', 'fr-FR']);
|
||||
expect(codeTranslationLocalesToTry('fr-FR')).toEqual(['fr-FR', 'fr']);
|
||||
// Note: "pt" is expanded into "pt-BR", not "pt-PT", as "pt-BR" is more widely used!
|
||||
// See https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
|
||||
expect(codeTranslationLocalesToTry('pt')).toEqual(['pt', 'pt-BR']);
|
||||
expect(codeTranslationLocalesToTry('pt-BR')).toEqual(['pt-BR', 'pt']);
|
||||
expect(codeTranslationLocalesToTry('pt-PT')).toEqual(['pt-PT', 'pt']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readDefaultCodeTranslationMessages', () => {
|
||||
const dirPath = path.resolve(
|
||||
__dirname,
|
||||
'__fixtures__',
|
||||
'defaultCodeTranslations',
|
||||
);
|
||||
|
||||
async function readAsJSON(filename: string) {
|
||||
return JSON.parse(
|
||||
await fs.readFile(path.resolve(dirPath, filename), 'utf8'),
|
||||
);
|
||||
}
|
||||
|
||||
test('for empty locale', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: '',
|
||||
dirPath,
|
||||
}),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"First argument to Intl.Locale constructor can't be empty or missing"`,
|
||||
);
|
||||
});
|
||||
|
||||
test('for unexisting locale', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'es',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual({});
|
||||
});
|
||||
|
||||
test('for fr but bad folder', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'fr',
|
||||
dirPath: __dirname,
|
||||
}),
|
||||
).resolves.toEqual({});
|
||||
});
|
||||
|
||||
test('for fr', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'fr',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual(await readAsJSON('fr.json'));
|
||||
});
|
||||
|
||||
test('for fr-FR', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'fr-FR',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual(await readAsJSON('fr-FR.json'));
|
||||
});
|
||||
|
||||
test('for en', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'en',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual(await readAsJSON('en.json'));
|
||||
});
|
||||
|
||||
test('for en-US', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'en-US',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual(await readAsJSON('en.json'));
|
||||
});
|
||||
|
||||
test('for en-WHATEVER', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'en-WHATEVER',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual(await readAsJSON('en.json'));
|
||||
});
|
||||
});
|
|
@ -1,53 +0,0 @@
|
|||
/**
|
||||
* 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[] {
|
||||
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`);
|
||||
|
||||
if (await fs.pathExists(filePath)) {
|
||||
const fileContent = await fs.readFile(filePath, 'utf8');
|
||||
return JSON.parse(fileContent);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
|
@ -30,7 +30,6 @@ export * from './tags';
|
|||
|
||||
export const posixPath = posixPathImport;
|
||||
|
||||
export * from './codeTranslationsUtils';
|
||||
export * from './markdownParser';
|
||||
export * from './markdownLinks';
|
||||
export * from './escapePath';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue