Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization of dates in date-helpers #94

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
92 changes: 84 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,102 @@
const Handlebars = require('handlebars');
const { readFileSync, readdirSync } = require('fs');
const { join } = require('path');
const i18next = require("i18next");
const registerI18nHelper = require('handlebars-i18next').default;

const {readFileSync, readdirSync} = require('fs');
const {join} = require('path');

const HELPERS = join(__dirname, 'theme/hbs-helpers');

const { birthDate } = require(join(HELPERS, 'birth-date.js'));
const { dateHelpers } = require(join(HELPERS, 'date-helpers.js'));
const { dateHelpers, setLanguage } = require(join(HELPERS, 'date-helpers.js'));
const { paragraphSplit } = require(join(HELPERS, 'paragraph-split.js'));
const { toLowerCase } = require(join(HELPERS, 'to-lower-case.js'));
const { spaceToDash } = require(join(HELPERS, 'space-to-dash.js'));
const {getDateHelpers} = require("./theme/hbs-helpers/date-helpers");



var language = "en-gb";

i18next.init({
lng: 'en', // if you're using a language detector, do not define the lng option
debug: true,
fallbackLng: 'en',
resources: {
en: { // Should be british english
translation: {
resume: {
summary: "Summary",
skills: "Skills",
workExperience: "Work Experience",
projects: "Projects",
volunteer: "Volunteer",
education: "Education",
languages: "Languages",
awards: "Awards",
certificates: "Certificates",
publications: "Publications",
interests: "Interests",
references: "References",
}
}
},

de: {
translation: {
resume: {
summary: "Übersicht",
skills: "Kenntnisse",
workExperience: "Berufserfahrung",
projects: "Projekte",
volunteer: "Ehrenamtliche Tätigkeiten",
education: "Bildung",
languages: "Sprachkenntnisse",
awards: "Auszeichnungen",
certificates: "Zertifikate",
publications: "Veröffentlichungen",
interests: "Interessen",
references: "Empfehlungen",
}
}
}
}
});

registerI18nHelper(Handlebars, i18next);


/**
* Calls this before the render to adjust the language.
* @param languageTemporaryVar supported values are "en-gb" for english (default) and "de" for german
*/
function changeLanguage(languageTemporaryVar) {
let i18NextLanguage = languageTemporaryVar;
switch (languageTemporaryVar) {
case "en-gb":
i18NextLanguage = "en";
break;
}
i18next.changeLanguage(i18NextLanguage);
language = languageTemporaryVar;
}

function registerDateHelpers(language) {
const { MY, Y, DMY } = getDateHelpers(language);
Handlebars.registerHelper('MY', MY);
Handlebars.registerHelper('Y', Y);
Handlebars.registerHelper('DMY', DMY);
}

const { MY, Y, DMY } = dateHelpers;

Handlebars.registerHelper('birthDate', birthDate);
Handlebars.registerHelper('MY', MY); // Date: Month year eg. "Jul 2020"
Handlebars.registerHelper('Y', Y); // Date: Year eg. "2020"
Handlebars.registerHelper('DMY', DMY);
Handlebars.registerHelper('paragraphSplit', paragraphSplit);
Handlebars.registerHelper('toLowerCase', toLowerCase);
Handlebars.registerHelper('spaceToDash', spaceToDash);


function render(resume) {
registerDateHelpers(language);
const css = readFileSync(`${__dirname}/style.css`, 'utf-8');
const template = readFileSync(`${__dirname}/resume.hbs`, 'utf-8');
const partialsDir = join(__dirname, 'theme/partials');
Expand Down Expand Up @@ -51,4 +127,4 @@ const pdfRenderOptions = {
}
}

module.exports = { render, pdfRenderOptions };
module.exports = {render, pdfRenderOptions, changeLanguage};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"main": "index.js",
"dependencies": {
"handlebars": "^4.7.7",
"markdown-it": "^13.0.1",
"moment": "^2.29.2"
"handlebars-i18next": "^1.0.3",
"markdown-it": "^13.0.1"
},
"devDependencies": {
"@jsonresume/schema": "^1.2.1",
"html-validate": "^8.28.0",
"jest": "^29.7.0",
"@jsonresume/schema": "^1.2.1"
"jest": "^29.7.0"
},
"packageManager": "[email protected]+sha512.4abf725084d7bcbafbd728bfc7bee61f2f791f977fd87542b3579dcb23504d170d46337945e4c66485cd12d588a0c0e570ed9c477e7ccdd8507cf05f3f92eaca"
}
4 changes: 0 additions & 4 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,6 @@ section .location {
background: #dfeaf0;
}

.highlights {
margin: 1em 0 0 0;
}

.highlights > li > p {
margin-bottom: 0.5em;
}
Expand Down
36 changes: 36 additions & 0 deletions test/Helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { paragraphSplit } = require('../theme/hbs-helpers/paragraph-split.js');
const { isHTMLValid } = require("./TestHelpers/HTMLValidate");
const { getDateHelpers } = require("../theme/hbs-helpers/date-helpers");
const { birthDate } = require("../theme/hbs-helpers/birth-date");


describe("Test Helpers in theme/hbs-helpers", () => {

describe('Test date-helpers.js', () => {

const date = "2013-12-01";

test("English date helpers", () => {
const dateHelpers = getDateHelpers("en-gb");
const birth = {
"place": "New York",
"state": "USA",
"date": "1988"
};


expect(dateHelpers.MY(date)).toBe("Dec 2013");
expect(dateHelpers.Y(date)).toBe("2013");
expect(dateHelpers.DMY(date)).toBe("1 Dec 2013");

expect(birthDate(birth).toString()).toBe("<div> Born in New York, USA in 1/1/1988</div>");
});

test("German date helpers", () => {
const dateHelpers = getDateHelpers("de");
expect(dateHelpers.MY(date)).toBe("Dez. 2013");
expect(dateHelpers.Y(date)).toBe("2013");
expect(dateHelpers.DMY(date)).toBe("1. Dez. 2013");
});
});
});
43 changes: 18 additions & 25 deletions test/SimpleTests.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
const renderer = require("../index")
const exampleCVJSON = require("@jsonresume/schema/sample.resume.json")
const fs = require('node:fs');
const HtmlValidate = require("html-validate");
const {isHTMLValid} = require("./TestHelpers/HTMLValidate");


const testOutPutPath = `${__dirname}/TestOutput`;

const isHTMLValid = async (html, logResults = true) => {
const htmlvalidate = new HtmlValidate.HtmlValidate({
extends: ["html-validate:recommended"],
rules: {
"doctype-style": "off",
"attr-quotes": "off",
"no-trailing-whitespace": "off",
"void-style": "warn",
},
});

// See https://html-validate.org/guide/api/getting-started.html
const validationReport = await htmlvalidate.validateString(html);

if (logResults) {
validationReport.results.forEach(console.log);
}

return validationReport.valid;
function writeToTestOutput(result, filename) {
const testOutPutPath = `${__dirname}/TestOutput`;
fs.writeFileSync(`${testOutPutPath}/${filename}`, result);
}

describe("SimpleTests", () => {
describe("SimpleTests", () => {

var result;

beforeEach(() => {
result = renderer.render(exampleCVJSON);
fs.writeFileSync(`${testOutPutPath}/SimpleTests->RenderFunction.html`, result);
});

test("Does the render function work for example CV.json?", async () => {
Expand All @@ -49,7 +30,19 @@ describe("SimpleTests", () => {
* This test will fail as soon as you changed the HTML-Output of the render function.
* If this change was intentional, you need to update the snapshot by typing the "jest --updateSnapshot" command
* in the terminal or using "npm run updateTestSnapshots" script, see package.json. */
test("If current rendered HTML has changed from previous taken snapshot", () => {
test("Snapshot-test english translation", () => {
const testName = expect.getState().currentTestName;
renderer.changeLanguage("en-gb");
result = renderer.render(exampleCVJSON);
writeToTestOutput(result, `${testName.replaceAll(" ", "_")}.html`);
expect(result).toMatchSnapshot();
});

test("Snapshot-test german translation", () => {
const testName = expect.getState().currentTestName;
renderer.changeLanguage("de");
result = renderer.render(exampleCVJSON);
writeToTestOutput(result, `${testName.replaceAll(" ", "_")}.html`);
expect(result).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions test/TestHelpers/HTMLValidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const HtmlValidate = require("html-validate");

const isHTMLValid = async (html, logResults = true) => {
const htmlvalidate = new HtmlValidate.HtmlValidate({
extends: ["html-validate:recommended"],
rules: {
"doctype-style": "off",
"attr-quotes": "off",
"no-trailing-whitespace": "off",
"void-style": "warn",
},
});

// See https://html-validate.org/guide/api/getting-started.html
const validationReport = await htmlvalidate.validateString(html);

if (logResults) {
validationReport.results.forEach(console.log);
}

return validationReport.valid;
}

module.exports = { isHTMLValid };
Loading