Skip to content

Commit

Permalink
Implement i18n support, add German
Browse files Browse the repository at this point in the history
  • Loading branch information
baltpeter committed Mar 24, 2021
1 parent 8f8a86b commit 3bc5624
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"files": [
"/dist",
"/templates"
"/res"
],
"husky": {
"hooks": {
Expand Down
10 changes: 10 additions & 0 deletions res/i18n/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"subject": "Betreff:",
"from": "Von:",
"date": "Datum:",
"to": "An:",
"cc": "Kopie (CC):",
"bcc": "Blindkopie (BCC):",
"priority": "Priorität:",
"attachments": "Anhänge:"
}
10 changes: 10 additions & 0 deletions res/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"subject": "Subject:",
"from": "From:",
"date": "Date:",
"to": "To:",
"cc": "CC:",
"bcc": "BCC:",
"priority": "Priority:",
"attachments": "Attachments:"
}
16 changes: 8 additions & 8 deletions templates/thunderbird.hbr → res/templates/thunderbird.hbr
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,24 @@
<body>
<div id="header">
<ul class="br-list">
<li><strong>Subject:</strong> {{subject}}</li>
<li><strong>{{i18n.subject}}</strong> {{subject}}</li>
{{#if from}}
<li><strong>From:</strong> {{from}}</li>
<li><strong>{{i18n.from}}</strong> {{from}}</li>
{{/if}}
{{#if date}}
<li><strong>Date:</strong> {{date}}</li>
<li><strong>{{i18n.date}}</strong> {{date}}</li>
{{/if}}
{{#if to}}
<li><strong>To:</strong> {{to}}</li>
<li><strong>{{i18n.to}}</strong> {{to}}</li>
{{/if}}
{{#if cc}}
<li><strong>CC:</strong> {{cc}}</li>
<li><strong>{{i18n.cc}}</strong> {{cc}}</li>
{{/if}}
{{#if bcc}}
<li><strong>BCC:</strong> {{bcc}}</li>
<li><strong>{{i18n.bcc}}</strong> {{bcc}}</li>
{{/if}}
{{#if priority}}
<li><strong>Priority:</strong> {{priority}}</li>
<li><strong>{{i18n.priority}}</strong> {{priority}}</li>
{{/if}}
</ul>
</div>
Expand All @@ -104,7 +104,7 @@
</div>
{{#if attachments}}
<div id="attachments">
<h2><span>Attachments:</span></h2>
<h2><span>{{i18n.attachments}}</span></h2>
<table id="attachments-table">
{{#each attachments}}
<tr>
Expand Down
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import Stream = StreamModule.Stream;

type EmlInput = string | Buffer | Stream;
type Template = 'thunderbird';
type Language = 'en';
type Language = 'en' | 'de';

const loadLanguage = async (lang: string) =>
JSON.parse((await fs.readFile(join(__dirname, '..', 'res', 'i18n', lang + '.json'))).toString());
// The header and footer templates don't respect the page styles. They need their own styles, otherwise they will be
// tiny, see: https://github.com/puppeteer/puppeteer/issues/1822#issuecomment-530533300
const headerFooter = (html: string, align = 'left') =>
Expand All @@ -25,7 +27,7 @@ const addressesToString = (addr?: AddressObject | AddressObject[]) =>
* each one, either provide a file path as a string, which will then be read from the filesystem, or the
* content of the .eml file as a `Buffer` or `Stream`.
* @param options.language The language to be used for the labels in the generated PDF (optional, defaults to `en`).
* Currently, only English is supported.
* Currently, English (`en`) and German (`de`) are supported.
* @param options.template_name The template to use (optional, defaults to `thunderbird`). Currently, only `thunderbird`
* is supported which will generate output similar to Thunderbird’s “Print to PDF” feature.
*
Expand All @@ -39,16 +41,19 @@ export = async function mail2pdf(
options = { language: 'en', template_name: 'thunderbird', ...options };
eml = Array.isArray(eml) ? eml : [eml];

const i18n = await loadLanguage(options.language!);
const i18n_fallback = await loadLanguage('en');
const template = handlebars.compile(
(await fs.readFile(join(__dirname, '..', 'res', 'templates', options?.template_name + '.hbr'))).toString()
);

const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();

const result = await Promise.all(
eml.map(async (e) => {
const mail = await simpleParser(typeof e === 'string' ? await fs.readFile(e) : e);

const template = handlebars.compile(
(await fs.readFile(join(__dirname, '..', 'templates', options?.template_name + '.hbr'))).toString()
);
const html = template({
subject: mail.subject,
has_html_body: mail.html !== false,
Expand All @@ -62,6 +67,7 @@ export = async function mail2pdf(
attachments: mail.attachments
.filter((a) => a.contentDisposition === 'attachment')
.map((a) => ({ ...a, filename: a.filename || '<unnamed>', prettySize: prettyBytes(a.size) })),
i18n: { ...i18n_fallback, ...i18n },
});
await page.setContent(html);

Expand Down

0 comments on commit 3bc5624

Please sign in to comment.