mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +02:00
feat(v2): core v2 i18n support + Docusaurus site Crowdin integration (#3325)
* docs i18n initial poc * docs i18n initial poc * docs i18n initial poc * docs i18n initial poc * crowdin-v2 attempt * fix source * use crowdin env variable * try to install crowdin on netlify * try to install crowdin on netlify * try to use crowdin jar directly * try to curl the crowdin jar * add java version cmd * try to run crowdin jar in netlify * fix translatedDocsDirPath * fix loadContext issue due to site baseUrl not being modified in generted config file * real validateLocalesFile * add locale option to deploy command * better LocalizationFile type * create util getPluginI18nPath * better core localization context loading code * More explicit VersionMetadata type for localized docs folders * Ability to translate blog posts with Crowdin! * blog: refactor markdown loader + report broken links + try to get linkify working better * upgrade crowdin config to upload all docs folder files except source code related files * try to support translated pages * make markdown pages translation work * add write-translations cli command template * fix site not reloaded with correct options * refactor a bit the read/write of @generated/i18n.json file * Add <Translate> + translate() API + use it on the docusaurus homepage * watch locale translation dir * early POC of adding babel parsing for translation extraction * fs.stat => pathExists * add install:fast script * TSC: noUnusedLocals false as it's already checked by eslint * POC of extracting translations from source code * minor typo * fix extracted key to code * initial docs extracted translations * stable plugin translations POC * add crowdin commands * quickfix for i18n deployment * POC of themeConfig translation * add ability to have localized site without path prefix * sidebar typo * refactor translation system to output multiple translation files * translate properly the docs plugin * improve theme classic translation * rework translation extractor to handle new Chrome I18n JSON format (include id/description) * writeTranslations: allow to pass locales cli arg * fix ThemeConfig TS issues * fix localizePath errors * temporary add write-translations to netlify deploy preview * complete example of french translated folder * update fr folder * remove all translations from repo * minor translation refactors * fix all docs-related tests * fix blog feed tests * fix last blog tests * refactor i18n context a bit, extract codeTranslations in an extra generated file * improve @generated/i18n type * fix some i18n todos * minor refactor * fix logo typing issue after merge * move i18n.json to siteConfig instead * try to fix windows CI build * fix config test * attempt to fix windows non-posix path * increase v1 minify css jest timeout due to flaky test * proper support for localizePath on windows * remove non-functional install:fast * docs, fix docsDirPathLocalized * fix Docs i18n / md linkify issues * ensure theme-classic swizzling will use "nextjs" sources (transpiled less aggressively, to make them human readable) * fix some snapshots * improve themeConfig translation code * refactor a bit getPluginI18nPath * readTranslationFileContent => ensure files are valid, fail fast * fix versions tests * add extractSourceCodeAstTranslations comments/resource links * ignore eslint: packages/docusaurus-theme-classic/lib-next/ * fix windows CI with cross-env * crowdin ignore .DS_Store * improve writeTranslations + add exhaustive tests for translations.ts * remove typo * Wire currentLocale to algolia search * improve i18n locale error * Add tests for translationsExtractor.ts * better code translation extraction regarding statically evaluable code * fix typo * fix typo * improve theme-classic transpilation * refactor + add i18n tests * typo * test new utils * add missing snapshots * fix snapshot * blog onBrokenMarkdownLink * add sidebars tests * theme-classic index should now use ES modules * tests for theme-classic translations * useless comment * add more translation tests * simplify/cleanup writeTranslations * try to fix Netlify fr deployment * blog: test translated md is used during feed generation * blog: better i18n tests regarding editUrl + md translation application * more i18n tests for docs plugin * more i18n tests for docs plugin * Add tests for pages i18n * polish docusaurus build i18n logs
This commit is contained in:
parent
85fe96d112
commit
3166fab307
107 changed files with 5447 additions and 649 deletions
|
@ -0,0 +1,8 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`getFolderContainingFile throw if no folder contain such file 1`] = `
|
||||
"relativeFilePath=[index.test.ts] does not exist in any of these folders:
|
||||
- /abcdef
|
||||
- /gehij
|
||||
- /klmn]"
|
||||
`;
|
|
@ -27,7 +27,14 @@ import {
|
|||
getFilePathForRoutePath,
|
||||
addLeadingSlash,
|
||||
getElementsAround,
|
||||
mergeTranslations,
|
||||
mapAsyncSequencial,
|
||||
findAsyncSequential,
|
||||
findFolderContainingFile,
|
||||
getFolderContainingFile,
|
||||
updateTranslationFileMessages,
|
||||
} from '../index';
|
||||
import {sum} from 'lodash';
|
||||
|
||||
describe('load utils', () => {
|
||||
test('aliasedSitePath', () => {
|
||||
|
@ -560,3 +567,151 @@ describe('getElementsAround', () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergeTranslations', () => {
|
||||
test('should merge translations', () => {
|
||||
expect(
|
||||
mergeTranslations([
|
||||
{
|
||||
T1: {message: 'T1 message', description: 'T1 desc'},
|
||||
T2: {message: 'T2 message', description: 'T2 desc'},
|
||||
T3: {message: 'T3 message', description: 'T3 desc'},
|
||||
},
|
||||
{
|
||||
T4: {message: 'T4 message', description: 'T4 desc'},
|
||||
},
|
||||
{T2: {message: 'T2 message 2', description: 'T2 desc 2'}},
|
||||
]),
|
||||
).toEqual({
|
||||
T1: {message: 'T1 message', description: 'T1 desc'},
|
||||
T2: {message: 'T2 message 2', description: 'T2 desc 2'},
|
||||
T3: {message: 'T3 message', description: 'T3 desc'},
|
||||
T4: {message: 'T4 message', description: 'T4 desc'},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('mapAsyncSequencial', () => {
|
||||
function sleep(timeout: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, timeout));
|
||||
}
|
||||
|
||||
test('map sequentially', async () => {
|
||||
const itemToTimeout: Record<string, number> = {
|
||||
'1': 50,
|
||||
'2': 150,
|
||||
'3': 100,
|
||||
};
|
||||
const items = Object.keys(itemToTimeout);
|
||||
|
||||
const itemMapStartsAt: Record<string, number> = {};
|
||||
const itemMapEndsAt: Record<string, number> = {};
|
||||
|
||||
const timeBefore = Date.now();
|
||||
await expect(
|
||||
mapAsyncSequencial(items, async (item) => {
|
||||
const itemTimeout = itemToTimeout[item];
|
||||
itemMapStartsAt[item] = Date.now();
|
||||
await sleep(itemTimeout);
|
||||
itemMapEndsAt[item] = Date.now();
|
||||
return `${item} mapped`;
|
||||
}),
|
||||
).resolves.toEqual(['1 mapped', '2 mapped', '3 mapped']);
|
||||
const timeAfter = Date.now();
|
||||
|
||||
const timeTotal = timeAfter - timeBefore;
|
||||
|
||||
const totalTimeouts = sum(Object.values(itemToTimeout));
|
||||
expect(timeTotal > totalTimeouts);
|
||||
|
||||
expect(itemMapStartsAt['1'] > 0);
|
||||
expect(itemMapStartsAt['2'] > itemMapEndsAt['1']);
|
||||
expect(itemMapStartsAt['3'] > itemMapEndsAt['2']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAsyncSequencial', () => {
|
||||
function sleep(timeout: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, timeout));
|
||||
}
|
||||
|
||||
test('find sequentially', async () => {
|
||||
const items = ['1', '2', '3'];
|
||||
|
||||
const findFn = jest.fn(async (item: string) => {
|
||||
await sleep(50);
|
||||
return item === '2';
|
||||
});
|
||||
|
||||
const timeBefore = Date.now();
|
||||
await expect(findAsyncSequential(items, findFn)).resolves.toEqual('2');
|
||||
const timeAfter = Date.now();
|
||||
|
||||
expect(findFn).toHaveBeenCalledTimes(2);
|
||||
expect(findFn).toHaveBeenNthCalledWith(1, '1');
|
||||
expect(findFn).toHaveBeenNthCalledWith(2, '2');
|
||||
|
||||
const timeTotal = timeAfter - timeBefore;
|
||||
expect(timeTotal > 100);
|
||||
expect(timeTotal < 150);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findFolderContainingFile', () => {
|
||||
test('find appropriate folder', async () => {
|
||||
await expect(
|
||||
findFolderContainingFile(
|
||||
['/abcdef', '/gehij', __dirname, '/klmn'],
|
||||
'index.test.ts',
|
||||
),
|
||||
).resolves.toEqual(__dirname);
|
||||
});
|
||||
|
||||
test('return undefined if no folder contain such file', async () => {
|
||||
await expect(
|
||||
findFolderContainingFile(['/abcdef', '/gehij', '/klmn'], 'index.test.ts'),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFolderContainingFile', () => {
|
||||
test('get appropriate folder', async () => {
|
||||
await expect(
|
||||
getFolderContainingFile(
|
||||
['/abcdef', '/gehij', __dirname, '/klmn'],
|
||||
'index.test.ts',
|
||||
),
|
||||
).resolves.toEqual(__dirname);
|
||||
});
|
||||
|
||||
test('throw if no folder contain such file', async () => {
|
||||
await expect(
|
||||
getFolderContainingFile(['/abcdef', '/gehij', '/klmn'], 'index.test.ts'),
|
||||
).rejects.toThrowErrorMatchingSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateTranslationFileMessages', () => {
|
||||
test('should update messages', () => {
|
||||
expect(
|
||||
updateTranslationFileMessages(
|
||||
{
|
||||
path: 'abc',
|
||||
content: {
|
||||
t1: {message: 't1 message', description: 't1 desc'},
|
||||
t2: {message: 't2 message', description: 't2 desc'},
|
||||
t3: {message: 't3 message', description: 't3 desc'},
|
||||
},
|
||||
},
|
||||
(message) => `prefix ${message} suffix`,
|
||||
),
|
||||
).toEqual({
|
||||
path: 'abc',
|
||||
content: {
|
||||
t1: {message: 'prefix t1 message suffix', description: 't1 desc'},
|
||||
t2: {message: 'prefix t2 message suffix', description: 't2 desc'},
|
||||
t3: {message: 'prefix t3 message suffix', description: 't3 desc'},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -14,10 +14,15 @@ import kebabCase from 'lodash.kebabcase';
|
|||
import escapeStringRegexp from 'escape-string-regexp';
|
||||
import fs from 'fs-extra';
|
||||
import {URL} from 'url';
|
||||
import {ReportingSeverity} from '@docusaurus/types';
|
||||
import {
|
||||
ReportingSeverity,
|
||||
TranslationFileContent,
|
||||
TranslationFile,
|
||||
} from '@docusaurus/types';
|
||||
|
||||
// @ts-expect-error: no typedefs :s
|
||||
import resolvePathnameUnsafe from 'resolve-pathname';
|
||||
import {mapValues} from 'lodash';
|
||||
|
||||
const fileHash = new Map();
|
||||
export async function generate(
|
||||
|
@ -439,6 +444,89 @@ export function getElementsAround<T extends unknown>(
|
|||
return {previous, next};
|
||||
}
|
||||
|
||||
export function getPluginI18nPath({
|
||||
siteDir,
|
||||
locale,
|
||||
pluginName,
|
||||
pluginId = 'default', // TODO duplicated constant
|
||||
subPaths = [],
|
||||
}: {
|
||||
siteDir: string;
|
||||
locale: string;
|
||||
pluginName: string;
|
||||
pluginId?: string | undefined;
|
||||
subPaths?: string[];
|
||||
}) {
|
||||
return path.join(
|
||||
siteDir,
|
||||
'i18n',
|
||||
// namespace first by locale: convenient to work in a single folder for a translator
|
||||
locale,
|
||||
// Make it convenient to use for single-instance
|
||||
// ie: return "docs", not "docs-default" nor "docs/default"
|
||||
`${pluginName}${
|
||||
// TODO duplicate constant :(
|
||||
pluginId === 'default' ? '' : `-${pluginId}`
|
||||
}`,
|
||||
...subPaths,
|
||||
);
|
||||
}
|
||||
|
||||
export async function mapAsyncSequencial<T extends unknown, R extends unknown>(
|
||||
array: T[],
|
||||
action: (t: T) => Promise<R>,
|
||||
): Promise<R[]> {
|
||||
const results: R[] = [];
|
||||
for (const t of array) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const result = await action(t);
|
||||
results.push(result);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function findAsyncSequential<T>(
|
||||
array: T[],
|
||||
predicate: (t: T) => Promise<boolean>,
|
||||
): Promise<T | undefined> {
|
||||
for (const t of array) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
if (await predicate(t)) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// return the first folder path in which the file exists in
|
||||
export async function findFolderContainingFile(
|
||||
folderPaths: string[],
|
||||
relativeFilePath: string,
|
||||
): Promise<string | undefined> {
|
||||
return findAsyncSequential(folderPaths, (folderPath) =>
|
||||
fs.pathExists(path.join(folderPath, relativeFilePath)),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getFolderContainingFile(
|
||||
folderPaths: string[],
|
||||
relativeFilePath: string,
|
||||
): Promise<string> {
|
||||
const maybeFolderPath = await findFolderContainingFile(
|
||||
folderPaths,
|
||||
relativeFilePath,
|
||||
);
|
||||
// should never happen, as the source was read from the FS anyway...
|
||||
if (!maybeFolderPath) {
|
||||
throw new Error(
|
||||
`relativeFilePath=[${relativeFilePath}] does not exist in any of these folders: \n- ${folderPaths.join(
|
||||
'\n- ',
|
||||
)}]`,
|
||||
);
|
||||
}
|
||||
return maybeFolderPath;
|
||||
}
|
||||
|
||||
export function reportMessage(
|
||||
message: string,
|
||||
reportingSeverity: ReportingSeverity,
|
||||
|
@ -464,6 +552,14 @@ export function reportMessage(
|
|||
}
|
||||
}
|
||||
|
||||
export function mergeTranslations(
|
||||
contents: TranslationFileContent[],
|
||||
): TranslationFileContent {
|
||||
return contents.reduce((acc, content) => {
|
||||
return {...acc, ...content};
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function getSwizzledComponent(
|
||||
componentPath: string,
|
||||
): string | undefined {
|
||||
|
@ -477,3 +573,18 @@ export function getSwizzledComponent(
|
|||
? swizzledComponentPath
|
||||
: undefined;
|
||||
}
|
||||
|
||||
// Useful to update all the messages of a translation file
|
||||
// Used in tests to simulate translations
|
||||
export function updateTranslationFileMessages(
|
||||
translationFile: TranslationFile,
|
||||
updateMessage: (message: string) => string,
|
||||
): TranslationFile {
|
||||
return {
|
||||
...translationFile,
|
||||
content: mapValues(translationFile.content, (translation) => ({
|
||||
...translation,
|
||||
message: updateMessage(translation.message),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue