Added translations using i18next

ref #5
This commit is contained in:
Nicolai Ort 2021-02-05 21:20:05 +01:00
parent 19697692bc
commit 75eb925267
3 changed files with 22 additions and 5 deletions

View File

@ -46,6 +46,8 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"handlebars": "^4.7.6",
"i18next": "^19.8.7",
"i18next-fs-backend": "^1.0.8",
"puppeteer": "^7.0.1",
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0-alpha.6",
@ -75,4 +77,4 @@
"publish": false
}
}
}
}

View File

@ -1,8 +1,11 @@
import fs from "fs";
import Handlebars from 'handlebars';
import i18next from "i18next";
import Backend from 'i18next-fs-backend';
import path from 'path';
import puppeteer from "puppeteer";
import { Runner } from './models/Runner';
import Handlebars = require('handlebars');
/**
* This class is responsible for all things pdf creation.
* This uses the html templates from src/templates.
@ -12,12 +15,25 @@ export class PdfCreator {
private browser;
constructor() {
i18next
.use(Backend)
.init({
fallbackLng: 'en',
lng: 'en',
backend: {
loadPath: path.join(__dirname, '/locales/{{lng}}.json')
}
});
Handlebars.registerHelper('__',
function (str) {
return i18next.t(str).toString();
}
);
puppeteer.launch({ headless: true }).then(browser => {
this.browser = browser;
});
}
//TODO: Accept the runner class
public async generateSponsoringContract(runner: Runner): Promise<any> {
const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
const template = Handlebars.compile(template_source);

View File

@ -13,9 +13,8 @@ export class PdfController {
@Post('/contracts')
@OpenAPI({ description: "Generate Sponsoring contract pdfs from runner objects." })
async generateContracts(@Body({ validate: true }) runner: Runner, @Res() res: any) {
console.log(runner)
const contracts = await this.pdf.generateSponsoringContract(runner);
res.setHeader('content-type', 'application/pdf');
return await contracts;
}
}
}