Skip to content

Commit

Permalink
feature: Add i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Schurter committed Jun 14, 2023
1 parent 0bd8bc2 commit 70f65bf
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 45 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ npx github:fabschurt/microgen src dist
* parse the data from `src/data.json`, pass it to the `src/index.pug` template,
and finally render the whole thing as `dist/index.html`.

(The `self` option is set to `true` in the Pug config, so you’ll have to prefix
any variable name in the template with `self.`)

## Why?

I mainly use this package to generate some very simple HTML/CSS content that I
Expand Down
8 changes: 6 additions & 2 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import buildProject from '#src/index'
const params = docopt(`
A minimalist staticgen for generating simple, single-page websites.
Usage: microgen <src-dir> <build-dir>
Usage: microgen [--lang=<lang>] <src-dir> <build-dir>
`)

buildProject(params['<src-dir>'], params['<build-dir>'])
buildProject(
params['<src-dir>'],
params['<build-dir>'],
params['--lang'],
)
4 changes: 2 additions & 2 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export function renderIndex(
withBuildDir,
readFile,
writeFile,
parseData,
renderTemplate,
data,
) {
return withSrcDir((prefixWithSrcDir) => {
return withBuildDir(async (prefixWithBuildDir) => {
const renderedTemplate = renderTemplate(
await readFile(prefixWithSrcDir(INDEX_TEMPLATE_NAME)),
await parseData(),
data,
)

return writeFile(
Expand Down
29 changes: 29 additions & 0 deletions src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from 'node:assert'
import { join } from 'node:path'

const LANG_PATTERN = /^[a-z]{2}$/
const TRANSLATION_DIR_PATH = 'i18n'
const TRANSLATION_FILE_PREFIX = '.json'

export async function parseTranslations(
withSrcDir,
ifPathExists,
readFile,
parseJson,
lang,
) {
assert.match(lang, LANG_PATTERN)

return await withSrcDir(async (prefixWithSrcDir) => {
const translationFilePath = join(
prefixWithSrcDir(TRANSLATION_DIR_PATH),
lang + TRANSLATION_FILE_PREFIX,
)

return await ifPathExists(
translationFilePath,
async () => parseJson(await readFile(translationFilePath)),
{}
)
})
}
39 changes: 13 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,27 @@ import {
} from '#src/utils/fs'
import { renderIndex, copyAssetDir } from '#src/build'
import { parseFromJsonFile, parseFromEnv } from '#src/data'
import { parseTranslations } from '#src/i18n'
import { parseJson } from '#src/utils/json'
import renderTemplate from '#src/renderTemplate/pug'

export default async function buildProject(srcDirPath, buildDirPath) {
export default async function buildProject(srcDirPath, buildDirPath, lang = null) {
await ifPathExists(buildDirPath, rmDir)

const withSrcDir = await withDir(srcDirPath)
const withBuildDir = await withScratchDir(buildDirPath)
const parseData = async () => ({
...(
await parseFromJsonFile(
withSrcDir,
ifPathExists,
readFile,
parseJson,
)
),
...parseFromEnv(process.env),
})

const data = {
...(await parseDataFromJsonFile(withSrcDir, ifPathExists, readFile, parseJson)),
...parseDataFromEnv(process.env),
}

if (lang) {
data.t = await parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, lang)
}

return Promise.all([
renderIndex(
withSrcDir,
withBuildDir,
readFile,
writeFile,
parseData,
renderTemplate,
),
copyAssetDir(
withSrcDir,
withBuildDir,
copyDir,
ifPathExists,
),
renderIndex(withSrcDir, withBuildDir, readFile, writeFile, renderTemplate, data),
copyAssetDir(withSrcDir, withBuildDir, copyDir, ifPathExists),
])
}
7 changes: 2 additions & 5 deletions src/renderTemplate/pug.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { render } from 'pug'

export default function renderTemplate(templateStream, data) {
return render(templateStream, {
...data,
self: true,
})
export default function renderTemplate(templateStr, data) {
return render(templateStr, data)
}
53 changes: 53 additions & 0 deletions tests/i18n.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it } from 'node:test'
import assert from 'node:assert'
import { join } from 'node:path'
import { mkdir, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { withTempDir } from '#tests/helpers'
import { withDir, ifPathExists, readFile } from '#src/utils/fs'
import { parseJson } from '#src/utils/json'
import { parseTranslations } from '#src/i18n'

describe('#src/i18n', () => {
describe('parseTranslations()', () => {
it('parses a translation file from a pre-defined directory', async () => {
await withTempDir(async (prefixWithTempDir) => {
const srcDirPath = prefixWithTempDir('src')
const translationDirPath = join(srcDirPath, 'i18n')
const file1Path = join(translationDirPath, 'fr.json')
const file2Path = join(translationDirPath, 'en.json')

await mkdir(translationDirPath, { recursive: true })
await writeFile(file1Path, '{"greetings": "Bonjour"}')
await writeFile(file2Path, '{"greetings": "Hello"}')

const withSrcDir = withDir(srcDirPath)

assert.deepStrictEqual(
await parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, 'fr'),
{ greetings: 'Bonjour' },
)
assert.deepStrictEqual(
await parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, 'en'),
{ greetings: 'Hello' },
)
assert.deepStrictEqual(
await parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, 'de'),
{},
)
})
})

it('throws if an invalid `lang` parameter is passed', async () => {
await assert.rejects(
parseTranslations(
withDir(tmpdir()),
ifPathExists,
readFile,
parseJson,
'fAiL',
)
)
})
})
})
12 changes: 5 additions & 7 deletions tests/renderTemplate/pug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import renderTemplate from '#src/renderTemplate/pug'
describe('#src/renderTemplate/pug', () => {
describe('renderTemplate()', () => {
it('should render Pug as HTML', () => {
const data = {
name: 'Gill',
surname: 'Bates',
}

assert.strictEqual(
renderTemplate(
'doctype html\nhtml\n body\n p Hello, #{self.name} #{self.surname}.',
data,
'doctype html\nhtml\n body\n p Hello, #{name} #{surname}.',
{
name: 'Gill',
surname: 'Bates',
},
),
'<!DOCTYPE html><html><body><p>Hello, Gill Bates.</p></body></html>',
)
Expand Down

0 comments on commit 70f65bf

Please sign in to comment.