mirror of
https://github.com/elyby/docs.git
synced 2026-11-29 19:03:23 +05:30
Integrate Crowdin and restore CI
- Add crowdin.yml for the Docusaurus layout: documentation pages and UI strings (navbar, footer) with Russian as the source language. Docusaurus theme strings (code.json) are excluded, since the theme already has translations - Add the CI workflow: uploads sources to Crowdin and downloads translations on master, builds and deploys to GitHub Pages, nightly rebuild with fresh translations. Local translations (i18n/en) are uploaded to Crowdin only on demand: with "[crowdin upload translations]" in the commit message or via a manual run - Publish languages other than Russian and English only when their translation progress in Crowdin is at least 60% (scripts/crowdin-locales.mjs, DOCS_LOCALES) - Add Chinese (zh-Hans, served under /zh/) to the locales configuration - Generate the root index.html and robots.txt from the actually built locales - Keep only the English translation in the repository, other i18n dirs are generated
This commit is contained in:
1 parent
a5dac3b611
commit
930c7e63e2
18 files changed
+280
-1353
No files matched your search
@@ -0,0 +1,42 @@
|
||||
// Prints the comma-separated list of locales to publish (the DOCS_LOCALES env variable for the build).
|
||||
// Russian (the source language) and English (also stored in the repository) are always published,
|
||||
// other languages only when their translation progress in Crowdin reaches MIN_PROGRESS.
|
||||
//
|
||||
// Requires the CROWDIN_PERSONAL_TOKEN env variable.
|
||||
|
||||
const API_URL = 'https://ely.api.crowdin.com/api/v2';
|
||||
const PROJECT_ID = 2;
|
||||
const MIN_PROGRESS = 60;
|
||||
const ALWAYS_PUBLISHED = ['ru', 'en'];
|
||||
|
||||
// Crowdin language id => Docusaurus locale. Must match "languages_mapping" in crowdin.yml
|
||||
// and the localeConfigs in docusaurus.config.ts
|
||||
const LOCALES_MAPPING = {
|
||||
'zh-CN': 'zh-Hans',
|
||||
};
|
||||
|
||||
const token = process.env.CROWDIN_PERSONAL_TOKEN;
|
||||
if (!token) {
|
||||
console.error('The CROWDIN_PERSONAL_TOKEN env variable is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_URL}/projects/${PROJECT_ID}/languages/progress?limit=500`, {
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
});
|
||||
if (!response.ok) {
|
||||
console.error(`Crowdin API responded with ${response.status}: ${await response.text()}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const locales = [...ALWAYS_PUBLISHED];
|
||||
for (const {data: progress} of (await response.json()).data) {
|
||||
const locale = LOCALES_MAPPING[progress.languageId] ?? progress.languageId;
|
||||
const published = progress.translationProgress >= MIN_PROGRESS;
|
||||
console.error(`${progress.languageId} (${locale}): ${progress.translationProgress}%${published ? '' : `, less than ${MIN_PROGRESS}%, skipping`}`);
|
||||
if (published && !locales.includes(locale)) {
|
||||
locales.push(locale);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(locales.join(','));
|
||||
+75
-8
@@ -1,24 +1,91 @@
|
||||
// Assembles the site root after "docusaurus build".
|
||||
// Every locale is built into its own build/<locale>/ directory (see i18n.localeConfigs in
|
||||
// Every locale is built into its own build/<prefix>/ directory (see i18n.localeConfigs in
|
||||
// docusaurus.config.ts), so files that must live in the site root are placed here.
|
||||
import {cpSync, copyFileSync, existsSync} from 'node:fs';
|
||||
import {cpSync, copyFileSync, existsSync, readdirSync, readFileSync, writeFileSync} from 'node:fs';
|
||||
import {dirname, join} from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
|
||||
const SITE_URL = 'https://docs.ely.by';
|
||||
const DEFAULT_PREFIX = 'ru';
|
||||
// Browser languages, whose speakers are more likely to understand Russian than English
|
||||
const RUSSIAN_SPEAKING = ['ru', 'be', 'uk', 'kk'];
|
||||
|
||||
const projectDir = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const buildDir = join(projectDir, 'build');
|
||||
const defaultLocale = 'ru';
|
||||
|
||||
if (!existsSync(join(buildDir, defaultLocale))) {
|
||||
if (!existsSync(join(buildDir, DEFAULT_PREFIX, 'index.html'))) {
|
||||
// Partial build (e.g. "docusaurus build --locale en"): there is nothing to assemble
|
||||
console.warn(`[postbuild] build/${defaultLocale} not found, skipping the site root assembly`);
|
||||
console.warn(`[postbuild] build/${DEFAULT_PREFIX} not found, skipping the site root assembly`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// CNAME, .nojekyll, robots.txt and the language-detecting index.html
|
||||
// Collect built locales: { prefix: "zh", lang: "zh-CN" }
|
||||
const locales = readdirSync(buildDir, {withFileTypes: true})
|
||||
.filter((entry) => entry.isDirectory() && existsSync(join(buildDir, entry.name, 'index.html')))
|
||||
.map(({name: prefix}) => {
|
||||
const html = readFileSync(join(buildDir, prefix, 'index.html'), 'utf8');
|
||||
const lang = html.match(/<html[^>]*\slang="([^"]+)"/)?.[1] ?? prefix;
|
||||
|
||||
return {prefix, lang};
|
||||
});
|
||||
|
||||
// Language name in the language itself: "русский", "English", "中文(中国)"
|
||||
function languageName(lang) {
|
||||
const name = new Intl.DisplayNames([lang], {type: 'language'}).of(lang) ?? lang;
|
||||
|
||||
return name.charAt(0).toLocaleUpperCase(lang) + name.slice(1);
|
||||
}
|
||||
|
||||
// CNAME and .nojekyll
|
||||
cpSync(join(projectDir, 'root'), buildDir, {recursive: true});
|
||||
|
||||
// GitHub Pages only serves the 404.html from the site root
|
||||
copyFileSync(join(buildDir, defaultLocale, '404.html'), join(buildDir, '404.html'));
|
||||
copyFileSync(join(buildDir, DEFAULT_PREFIX, '404.html'), join(buildDir, '404.html'));
|
||||
|
||||
console.log('[postbuild] Site root assembled');
|
||||
// The site root redirects to the documentation in the browser's language
|
||||
const languagesMap = Object.fromEntries([
|
||||
...RUSSIAN_SPEAKING.map((lang) => [lang, DEFAULT_PREFIX]),
|
||||
...locales.map(({prefix, lang}) => [lang.toLowerCase().split('-')[0], prefix]),
|
||||
]);
|
||||
const fallbackPrefix = locales.some(({prefix}) => prefix === 'en') ? 'en' : DEFAULT_PREFIX;
|
||||
|
||||
writeFileSync(join(buildDir, 'index.html'), `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Ely.by Documentation</title>
|
||||
${locales.map(({prefix, lang}) => ` <link rel="alternate" hreflang="${lang}" href="${SITE_URL}/${prefix}/">`).join('\n')}
|
||||
<link rel="alternate" hreflang="x-default" href="${SITE_URL}/${DEFAULT_PREFIX}/">
|
||||
<script>
|
||||
(function () {
|
||||
var map = ${JSON.stringify(languagesMap)};
|
||||
var languages = navigator.languages || [navigator.language || ''];
|
||||
var target = ${JSON.stringify(fallbackPrefix)};
|
||||
for (var i = 0; i < languages.length; i++) {
|
||||
var lang = String(languages[i]).toLowerCase().split('-')[0];
|
||||
if (map[lang]) {
|
||||
target = map[lang];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
window.location.replace('/' + target + '/' + window.location.search + window.location.hash);
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
${locales.map(({prefix, lang}) => ` <li><a href="/${prefix}/" hreflang="${lang}">${languageName(lang)}</a></li>`).join('\n')}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
|
||||
writeFileSync(join(buildDir, 'robots.txt'), `User-agent: *
|
||||
Allow: /
|
||||
|
||||
${locales.filter(({prefix}) => existsSync(join(buildDir, prefix, 'sitemap.xml'))).map(({prefix}) => `Sitemap: ${SITE_URL}/${prefix}/sitemap.xml`).join('\n')}
|
||||
`);
|
||||
|
||||
console.log(`[postbuild] Site root assembled for locales: ${locales.map(({prefix}) => prefix).join(', ')}`);
|
||||
Reference in new issue
Block a user