mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-25 14:07:50 +02:00
feat(v2): Add i18n default code translation bundles (#4215)
* Add default code translation bundles * fix tests
This commit is contained in:
parent
1b3c9be530
commit
6a94ad989c
12 changed files with 430 additions and 5 deletions
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"id1": "message 1 en",
|
||||
"id2": "message 2 en"
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"id1": "message 1 fr",
|
||||
"id2": "message 2 fr"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"id1": "message 1 fr_FR",
|
||||
"id2": "message 2 fr_FR",
|
||||
"id3": "message 3 fr_FR"
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {
|
||||
fileToPath,
|
||||
simpleHash,
|
||||
|
@ -33,6 +34,7 @@ import {
|
|||
findFolderContainingFile,
|
||||
getFolderContainingFile,
|
||||
updateTranslationFileMessages,
|
||||
readDefaultCodeTranslationMessages,
|
||||
} from '../index';
|
||||
import {sum} from 'lodash';
|
||||
|
||||
|
@ -718,3 +720,89 @@ describe('updateTranslationFileMessages', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
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,
|
||||
}),
|
||||
).resolves.toEqual({});
|
||||
});
|
||||
|
||||
test('for unexisting locale', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: 'es',
|
||||
dirPath,
|
||||
}),
|
||||
).resolves.toEqual({});
|
||||
});
|
||||
|
||||
test('for fr but bad folder', async () => {
|
||||
await expect(
|
||||
readDefaultCodeTranslationMessages({
|
||||
locale: '',
|
||||
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'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -602,3 +602,35 @@ export function updateTranslationFileMessages(
|
|||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export async function readDefaultCodeTranslationMessages({
|
||||
dirPath,
|
||||
locale,
|
||||
}: {
|
||||
dirPath: string;
|
||||
locale: string;
|
||||
}): Promise<Record<string, string>> {
|
||||
const fileNamesToTry = [locale];
|
||||
|
||||
if (locale.includes('_')) {
|
||||
const language = locale.split('_')[0];
|
||||
if (language) {
|
||||
fileNamesToTry.push(language);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the content of the first file that match
|
||||
// fr_FR.json => fr.json => nothing
|
||||
for (const fileName of fileNamesToTry) {
|
||||
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 {};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue