Skip to content
Snippets Groups Projects
Commit 9aed1417 authored by Alice Poggioli's avatar Alice Poggioli
Browse files

WIP : We get translated variable.

parent d1e7ca8b
No related branches found
No related tags found
1 merge request!104Feature/internationalization
const path = require('path')
const fs = require('fs').promises
const objectAssignDeep = require(`object-assign-deep`)
const yaml = require('js-yaml')
const pug = require('pug')
const chokidar = require('chokidar')
const default_lang = 'en'
const pugFile = './src/index.pug'
const dataDir = './translation'
const outDir = './www'
async function compile() {
const langs = (await fs.readdir(dataDir))
.filter(f => path.extname(f) === '.yml')
.map(f => path.basename(f, '.yml'))
const langData = {}
await Promise.all(
langs.map(lang =>
fs
.readFile(`${dataDir}/${lang}.yml`, 'utf8')
.then(text => (langData[lang] = yaml.safeLoad(text))),
),
)
for (const lang in langData) {
if (lang === default_lang) continue
langData[lang] = objectAssignDeep(
{},
langData[default_lang],
langData[lang],
)
}
const options = JSON.parse(await fs.readFile('./src/config.json', 'utf-8'))
const pugFct = pug.compileFile(pugFile)
await fs.mkdir(outDir, { recursive: true })
await Promise.all(
Object.entries(langData).map(([lang, data]) => {
options.lang=lang
options.data=data
const html = pugFct(options)
const filename = `${outDir}/index-${lang}.html`
console.log(`write ${filename}`)
return fs.writeFile(filename, html)
}),
)
}
compile().then(() => {
if (!process.argv.includes('-w') && !process.argv.includes('--watch')) return
console.log('watching for changes…')
chokidar.watch([pugFile, dataDir]).on('change', editedFilePath => {
console.log(`\nchanged: ${editedFilePath}`)
compile()
})
})
---
title: My amazing website
welcome: Hi! welcome to our website
contact: click here to contact us
email: contact@example.com
\ No newline at end of file
---
title: Mon superbe site
welcome: Bonjour ! bienvenu sur notre site
contact: cliquez ici pour nous contacter
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment