1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/script/test-render-translation.js
James M. Greene 43d354c1d9 Crowdin script tweaks (#18516)
* Make script/fix-translation-errors.js executable

* Make script/test-render-translation.js executable

* Update script/test-render-translation.js README

Co-authored-by: Vanessa Yuen <6842965+vanessayuenn@users.noreply.github.com>
2021-03-31 17:53:55 +00:00

66 lines
2.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
// [start-readme]
//
// Run this script to test-render all the translation files that have been changed (when compared to the `main` branch).
//
// [end-readme]
const renderContent = require('../lib/render-content')
const loadSiteData = require('../lib/site-data')
const { loadPages } = require('../lib/pages')
const languages = require('../lib/languages')
const path = require('path')
const { execSync } = require('child_process')
const fs = require('fs')
const frontmatter = require('../lib/frontmatter')
const chalk = require('chalk')
const main = async () => {
const siteData = loadSiteData()
const pages = await loadPages()
const contextByLanguage = {}
for (const lang in languages) {
const langObj = languages[lang]
const [crowdinLangCode] = langObj.dir === '' ? 'en' : langObj.dir.split('/').slice(1)
if (!crowdinLangCode) continue
contextByLanguage[crowdinLangCode] = {
site: siteData[langObj.code].site,
currentLanguage: langObj.code,
currentVersion: 'free-pro-team@latest'
}
}
const rootDir = path.join(__dirname, '..')
const changedFilesRelPaths = execSync('git diff --name-only origin/main | egrep "^translations/.*/.+.md$"', { maxBuffer: 1024 * 1024 * 100 })
.toString()
.split('\n')
.filter(path => path !== '' && !path.endsWith('README.md'))
.sort()
console.log(`Found ${changedFilesRelPaths.length} translated files.`)
changedFilesRelPaths.forEach(async (relPath) => {
const fullPath = path.join(rootDir, relPath)
const lang = relPath.split('/')[1]
const context = {
...contextByLanguage[lang],
pages,
page: pages.find(page => page.fullPath === fullPath),
redirects: {}
}
if (!context.page && !relPath.includes('data/reusables')) return
const fileContents = await fs.promises.readFile(fullPath, 'utf8')
const { content } = frontmatter(fileContents)
try {
await renderContent.liquid.parseAndRender(content, context)
} catch (err) {
console.log(chalk.bold(relPath))
console.log(chalk.red(` error message: ${err.message}`))
}
})
}
main()