-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fabien Schurter
committed
Jun 14, 2023
1 parent
0bd8bc2
commit 70f65bf
Showing
8 changed files
with
110 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
{} | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters