mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 13:22: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
259
packages/docusaurus-plugin-content-docs/src/translations.ts
Normal file
259
packages/docusaurus-plugin-content-docs/src/translations.ts
Normal file
|
@ -0,0 +1,259 @@
|
|||
/**
|
||||
* 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 {
|
||||
LoadedVersion,
|
||||
Sidebar,
|
||||
LoadedContent,
|
||||
Sidebars,
|
||||
SidebarItem,
|
||||
} from './types';
|
||||
|
||||
import {chain, mapValues, flatten, keyBy} from 'lodash';
|
||||
import {
|
||||
collectSidebarCategories,
|
||||
transformSidebarItems,
|
||||
collectSidebarLinks,
|
||||
} from './sidebars';
|
||||
import {
|
||||
TranslationFileContent,
|
||||
TranslationFile,
|
||||
TranslationFiles,
|
||||
} from '@docusaurus/types';
|
||||
import {mergeTranslations} from '@docusaurus/utils';
|
||||
import {CURRENT_VERSION_NAME} from './constants';
|
||||
|
||||
function getVersionFileName(versionName: string): string {
|
||||
if (versionName === CURRENT_VERSION_NAME) {
|
||||
return versionName;
|
||||
} else {
|
||||
// I don't like this "version-" prefix,
|
||||
// but it's for consistency with site/versioned_docs
|
||||
return `version-${versionName}`;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO legacy, the sidebar name is like "version-2.0.0-alpha.66/docs"
|
||||
// input: "version-2.0.0-alpha.66/docs"
|
||||
// output: "docs"
|
||||
function getNormalizedSidebarName({
|
||||
versionName,
|
||||
sidebarName,
|
||||
}: {
|
||||
versionName: string;
|
||||
sidebarName: string;
|
||||
}): string {
|
||||
if (versionName === CURRENT_VERSION_NAME || !sidebarName.includes('/')) {
|
||||
return sidebarName;
|
||||
}
|
||||
const [, ...rest] = sidebarName.split('/');
|
||||
return rest.join('/');
|
||||
}
|
||||
|
||||
/*
|
||||
// Do we need to translate doc metadatas?
|
||||
// It seems translating frontmatter labels is good enough
|
||||
function getDocTranslations(doc: DocMetadata): TranslationFileContent {
|
||||
return {
|
||||
[`${doc.unversionedId}.title`]: {
|
||||
message: doc.title,
|
||||
description: `The title for doc with id=${doc.unversionedId}`,
|
||||
},
|
||||
...(doc.sidebar_label
|
||||
? {
|
||||
[`${doc.unversionedId}.sidebar_label`]: {
|
||||
message: doc.sidebar_label,
|
||||
description: `The sidebar label for doc with id=${doc.unversionedId}`,
|
||||
},
|
||||
}
|
||||
: undefined),
|
||||
};
|
||||
}
|
||||
function translateDoc(
|
||||
doc: DocMetadata,
|
||||
docsTranslations: TranslationFileContent,
|
||||
): DocMetadata {
|
||||
return {
|
||||
...doc,
|
||||
title: docsTranslations[`${doc.unversionedId}.title`]?.message ?? doc.title,
|
||||
sidebar_label:
|
||||
docsTranslations[`${doc.unversionedId}.sidebar_label`]?.message ??
|
||||
doc.sidebar_label,
|
||||
};
|
||||
}
|
||||
|
||||
function getDocsTranslations(version: LoadedVersion): TranslationFileContent {
|
||||
return mergeTranslations(version.docs.map(getDocTranslations));
|
||||
}
|
||||
function translateDocs(
|
||||
docs: DocMetadata[],
|
||||
docsTranslations: TranslationFileContent,
|
||||
): DocMetadata[] {
|
||||
return docs.map((doc) => translateDoc(doc, docsTranslations));
|
||||
}
|
||||
*/
|
||||
|
||||
function getSidebarTranslationFileContent(
|
||||
sidebar: Sidebar,
|
||||
sidebarName: string,
|
||||
): TranslationFileContent {
|
||||
const categories = collectSidebarCategories(sidebar);
|
||||
const categoryContent: TranslationFileContent = chain(categories)
|
||||
.keyBy((category) => `sidebar.${sidebarName}.category.${category.label}`)
|
||||
.mapValues((category) => ({
|
||||
message: category.label,
|
||||
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
|
||||
}))
|
||||
.value();
|
||||
|
||||
const links = collectSidebarLinks(sidebar);
|
||||
const linksContent: TranslationFileContent = chain(links)
|
||||
.keyBy((link) => `sidebar.${sidebarName}.link.${link.label}`)
|
||||
.mapValues((link) => ({
|
||||
message: link.label,
|
||||
description: `The label for link ${link.label} in sidebar ${sidebarName}, linking to ${link.href}`,
|
||||
}))
|
||||
.value();
|
||||
|
||||
return mergeTranslations([categoryContent, linksContent]);
|
||||
}
|
||||
|
||||
function translateSidebar({
|
||||
sidebar,
|
||||
sidebarName,
|
||||
sidebarsTranslations,
|
||||
}: {
|
||||
sidebar: Sidebar;
|
||||
sidebarName: string;
|
||||
sidebarsTranslations: TranslationFileContent;
|
||||
}): Sidebar {
|
||||
return transformSidebarItems(
|
||||
sidebar,
|
||||
(item: SidebarItem): SidebarItem => {
|
||||
if (item.type === 'category') {
|
||||
return {
|
||||
...item,
|
||||
label:
|
||||
sidebarsTranslations[
|
||||
`sidebar.${sidebarName}.category.${item.label}`
|
||||
]?.message ?? item.label,
|
||||
};
|
||||
}
|
||||
if (item.type === 'link') {
|
||||
return {
|
||||
...item,
|
||||
label:
|
||||
sidebarsTranslations[`sidebar.${sidebarName}.link.${item.label}`]
|
||||
?.message ?? item.label,
|
||||
};
|
||||
}
|
||||
return item;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function getSidebarsTranslations(
|
||||
version: LoadedVersion,
|
||||
): TranslationFileContent {
|
||||
return mergeTranslations(
|
||||
Object.entries(version.sidebars).map(([sidebarName, sidebar]) => {
|
||||
const normalizedSidebarName = getNormalizedSidebarName({
|
||||
sidebarName,
|
||||
versionName: version.versionName,
|
||||
});
|
||||
return getSidebarTranslationFileContent(sidebar, normalizedSidebarName);
|
||||
}),
|
||||
);
|
||||
}
|
||||
function translateSidebars(
|
||||
version: LoadedVersion,
|
||||
sidebarsTranslations: TranslationFileContent,
|
||||
): Sidebars {
|
||||
return mapValues(version.sidebars, (sidebar, sidebarName) => {
|
||||
return translateSidebar({
|
||||
sidebar,
|
||||
sidebarName: getNormalizedSidebarName({
|
||||
sidebarName,
|
||||
versionName: version.versionName,
|
||||
}),
|
||||
sidebarsTranslations,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getVersionTranslationFiles(version: LoadedVersion): TranslationFiles {
|
||||
const versionTranslations: TranslationFileContent = {
|
||||
'version.label': {
|
||||
message: version.versionLabel,
|
||||
description: `The label for version ${version.versionName}`,
|
||||
},
|
||||
};
|
||||
|
||||
const sidebarsTranslations: TranslationFileContent = getSidebarsTranslations(
|
||||
version,
|
||||
);
|
||||
|
||||
// const docsTranslations: TranslationFileContent = getDocsTranslations(version);
|
||||
|
||||
return [
|
||||
{
|
||||
path: getVersionFileName(version.versionName),
|
||||
content: mergeTranslations([
|
||||
versionTranslations,
|
||||
sidebarsTranslations,
|
||||
// docsTranslations,
|
||||
]),
|
||||
},
|
||||
];
|
||||
}
|
||||
function translateVersion(
|
||||
version: LoadedVersion,
|
||||
translationFiles: Record<string, TranslationFile>,
|
||||
): LoadedVersion {
|
||||
const versionTranslations =
|
||||
translationFiles[getVersionFileName(version.versionName)].content;
|
||||
return {
|
||||
...version,
|
||||
versionLabel: versionTranslations['version.label']?.message,
|
||||
sidebars: translateSidebars(version, versionTranslations),
|
||||
// docs: translateDocs(version.docs, versionTranslations),
|
||||
};
|
||||
}
|
||||
|
||||
function getVersionsTranslationFiles(
|
||||
versions: LoadedVersion[],
|
||||
): TranslationFiles {
|
||||
return flatten(versions.map(getVersionTranslationFiles));
|
||||
}
|
||||
function translateVersions(
|
||||
versions: LoadedVersion[],
|
||||
translationFiles: Record<string, TranslationFile>,
|
||||
): LoadedVersion[] {
|
||||
return versions.map((version) => translateVersion(version, translationFiles));
|
||||
}
|
||||
|
||||
export function getLoadedContentTranslationFiles(
|
||||
loadedContent: LoadedContent,
|
||||
): TranslationFiles {
|
||||
return getVersionsTranslationFiles(loadedContent.loadedVersions);
|
||||
}
|
||||
export function translateLoadedContent(
|
||||
loadedContent: LoadedContent,
|
||||
translationFiles: TranslationFile[],
|
||||
): LoadedContent {
|
||||
const translationFilesMap: Record<string, TranslationFile> = keyBy(
|
||||
translationFiles,
|
||||
(f) => f.path,
|
||||
);
|
||||
|
||||
return {
|
||||
loadedVersions: translateVersions(
|
||||
loadedContent.loadedVersions,
|
||||
translationFilesMap,
|
||||
),
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue