Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ all locales with `npm run build` before submitting a change.
When changing or adding an English guide, review and update all seven
translations in the same delivery. Review the prose for fluent
language and the technical contract for accuracy. Keep executable code blocks
identical to the English source. The build checks this for Spanish, Brazilian
Portuguese, Simplified Chinese, Japanese, French and German. A missing translation
identical to the English source. The build checks this for German, Spanish,
French, Brazilian Portuguese, Ukrainian, Japanese and Simplified Chinese. A missing translation
or English fallback does not satisfy a changed guide's acceptance. Artifact
version placeholders stay shared with English and update automatically.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Waterline підтримує англійську, бразильську пор
Laravel-застосунку або контейнера сервісу:

```dotenv
WATERLINE_LOCALE=uk
WATERLINE_LOCALE=es
```

| Мова | Налаштування |
Expand Down
56 changes: 54 additions & 2 deletions scripts/check-doc-translation-updates.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const {createHash} = require('node:crypto');
const {execFileSync} = require('node:child_process');

const locales = ['de', 'es', 'fr', 'pt-BR', 'uk', 'ja', 'zh-Hans'];
const localeLabels = {
de: 'German',
es: 'Spanish',
fr: 'French',
'pt-BR': 'Brazilian Portuguese',
uk: 'Ukrainian',
ja: 'Japanese',
'zh-Hans': 'Simplified Chinese',
};
const locales = Object.keys(localeLabels);

function translationFailures(root, locale, relative, source, hash) {
const localeRoot = path.join(root, 'i18n', locale);
Expand Down Expand Up @@ -83,4 +93,46 @@
console.log(`Translation updates match reviewed English sources for ${count} changed guides in all seven other locales.`);
}

module.exports = {checkTranslationUpdates, checkCurrentTranslations, checkTranslationUpdatesFromGit};
function codeBlocks(file) {
const text = fs.readFileSync(file, 'utf8');
return [...text.matchAll(/^```[^\n]*\n[\s\S]*?^```\s*$/gm)].map(match => match[0].trimEnd());
}

// Every offered locale keeps its executable examples identical to English.
// Returns, per locale, the current English source hash of each checked guide.
function checkCodeBlocks(root, languages = locales) {
const hashes = {};
for (const locale of languages) {
const label = localeLabels[locale] ?? locale;
const translatedRoot = path.join(root, 'i18n', locale, 'docusaurus-plugin-content-docs/current');
const current = {};

function visit(directory) {

Check warning on line 110 in scripts/check-doc-translation-updates.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move function 'visit' to the outer scope.

See more on https://sonarcloud.io/project/issues?id=durable-workflow_durable-workflow.github.io&issues=AaIYVneZfJFq9bdoFqYA&open=AaIYVneZfJFq9bdoFqYA&pullRequest=624
for (const entry of fs.readdirSync(directory, {withFileTypes: true})) {
const file = path.join(directory, entry.name);
if (entry.isDirectory()) {
visit(file);
} else if (entry.isFile() && /\.mdx?$/.test(entry.name)) {
const relative = path.relative(translatedRoot, file).split(path.sep).join('/');
const source = path.join(root, 'docs', relative);
assert.deepEqual(codeBlocks(file), codeBlocks(source),
`${label} executable examples differ from English: ${relative}`);
current[relative] = createHash('sha256').update(fs.readFileSync(source)).digest('hex');
}
}
}

visit(translatedRoot);
hashes[locale] = current;
}
return hashes;
}

module.exports = {
locales,
localeLabels,
checkTranslationUpdates,
checkCurrentTranslations,
checkTranslationUpdatesFromGit,
checkCodeBlocks,
};
28 changes: 27 additions & 1 deletion scripts/check-doc-translation-updates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const os = require('node:os');
const path = require('node:path');
const {createHash} = require('node:crypto');
const {test} = require('node:test');
const {checkTranslationUpdates, checkCurrentTranslations} = require('./check-doc-translation-updates');
const {
locales,
checkTranslationUpdates,
checkCurrentTranslations,
checkCodeBlocks,
} = require('./check-doc-translation-updates');

const root = fs.mkdtempSync(path.join(os.tmpdir(), 'doc-translation-updates-'));
const relative = 'features/timers.md';
Expand Down Expand Up @@ -119,3 +124,24 @@ test('full inventory requires all seven offered non-English locales', t => {
}
assert.equal(checkCurrentTranslations(directory), 2);
});

test('code block check covers every offered locale, including Ukrainian', t => {
const directory = inventoryFixture(t);
const english = '# Locale\n\nSet it:\n\n```dotenv\nWATERLINE_LOCALE=es\n```\n';
const localized = '# Localized\n\nSet it:\n\n```dotenv\nWATERLINE_LOCALE=es\n```\n';
write(path.join(directory, 'docs/locale.md'), english);
for (const locale of locales) {
translate(directory, locale, 'locale.md', english, localized);
}

assert.deepEqual(locales, ['de', 'es', 'fr', 'pt-BR', 'uk', 'ja', 'zh-Hans']);
const hashes = checkCodeBlocks(directory);
assert.deepEqual(Object.keys(hashes), locales);
assert.equal(hashes.uk['locale.md'], createHash('sha256').update(english).digest('hex'));

write(path.join(directory, 'i18n/uk/docusaurus-plugin-content-docs/current/locale.md'),
localized.replace('WATERLINE_LOCALE=es', 'WATERLINE_LOCALE=uk'));
assert.throws(() => checkCodeBlocks(directory),
/Ukrainian executable examples differ from English: locale\.md/);
assert.equal(Object.keys(checkCodeBlocks(directory, ['de'])).length, 1);
});
44 changes: 12 additions & 32 deletions scripts/check-doc-translations.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const {createHash} = require('node:crypto');
const {checkCurrentTranslations, checkTranslationUpdatesFromGit} = require('./check-doc-translation-updates');
const {
locales,
localeLabels,
checkCodeBlocks,
checkCurrentTranslations,
checkTranslationUpdatesFromGit,
} = require('./check-doc-translation-updates');

const root = path.resolve(__dirname, '..');
const currentCount = checkCurrentTranslations(root);
console.log(`All seven other locales contain ${currentCount} current guides with current reviewed-source hashes.`);
checkTranslationUpdatesFromGit(root);
const locales = {es: 'Spanish', 'pt-BR': 'Brazilian Portuguese', 'zh-Hans': 'Simplified Chinese', ja: 'Japanese', fr: 'French', de: 'German'};

function codeBlocks(file) {
const text = fs.readFileSync(file, 'utf8');
return [...text.matchAll(/^```[^\n]*\n[\s\S]*?^```\s*$/gm)].map(match => match[0].trimEnd());
}

for (const [locale, label] of Object.entries(locales)) {
const translatedRoot = path.join(root, `i18n/${locale}/docusaurus-plugin-content-docs/current`);
const hashes = checkCodeBlocks(root);
for (const locale of locales) {
const label = localeLabels[locale];
const currentHashes = hashes[locale];
const reviewFile = path.join(root, `i18n/${locale}/source-hashes.json`);
const currentHashes = {};
let checked = 0;
function visit(directory) {
for (const entry of fs.readdirSync(directory, {withFileTypes: true})) {
const file = path.join(directory, entry.name);
if (entry.isDirectory()) {
visit(file);
} else if (/\.mdx?$/.test(entry.name)) {
const relative = path.relative(translatedRoot, file);
const source = path.join(root, 'docs', relative);
assert.deepEqual(codeBlocks(file), codeBlocks(source),
`${label} executable examples differ from English: ${relative}`);
currentHashes[relative] = createHash('sha256')
.update(fs.readFileSync(source)).digest('hex');
checked += 1;
}
}
}

visit(translatedRoot);
if (process.argv.includes('--record-review')) {
fs.writeFileSync(reviewFile, `${JSON.stringify(currentHashes, null, 2)}\n`);
} else {
Expand All @@ -48,5 +28,5 @@ for (const [locale, label] of Object.entries(locales)) {
console.warn(`${label} translation review needed: ${changedSources.join(', ')}. See README.md.`);
}
}
console.log(`${label} code blocks match English in ${checked} guides.`);
console.log(`${label} code blocks match English in ${Object.keys(currentHashes).length} guides.`);
}