Skip to content

Commit

Permalink
feat: STRF-11908 Provide public endpoints for setting templates and t…
Browse files Browse the repository at this point in the history
…ranslations (#361)
  • Loading branch information
jairo-bc authored Apr 16, 2024
1 parent 952dc59 commit 21b9c9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Paper {
break;
}

this.preProcessor = this.renderer.getPreProcessor();
this.preProcessor = this.getPreProcessor();

this.logger = logger;
}
Expand Down Expand Up @@ -198,6 +198,14 @@ class Paper {
});
}

addTemplates(templates) {
return this.renderer.addTemplates(templates);
}

getPreProcessor() {
return this.renderer.getPreProcessor();
}

/**
* Is the given template loaded?
*
Expand All @@ -215,13 +223,15 @@ class Paper {
* @return {Promise} Promise to load the translations into the renderer.
*/
loadTranslations(acceptLanguage) {
return this._assembler.getTranslations().then(translations => {
const translator = Translator.create(acceptLanguage, translations, this.logger);
this.renderer.setTranslator(translator);
return translations;
});
return this._assembler.getTranslations().then(translations => this.addTranslations(translations, acceptLanguage));
};

addTranslations(translations, acceptLanguage) {
const translator = Translator.create(acceptLanguage, translations, this.logger);
this.renderer.setTranslator(translator);
return translations;
}

/**
* Render a string with the given context.
*
Expand Down
52 changes: 35 additions & 17 deletions spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ describe('render()', function() {
done();
});
});

it('should add templates to paper', async (done) => {
const paper = new Paper(null, null, assembler);
paper.addTemplates(paper.getPreProcessor()({
'pages/product': '<html>{{> pages/partial}}</html>',
'pages/partial': '<p>{{variable}}</p>',
}));

const result = await paper.render('pages/product', context);
expect(result).to.be.equal('<html><p>hello world</p></html>');
});
});

describe('renderTheme()', function() {
Expand Down Expand Up @@ -173,27 +184,28 @@ describe('renderTheme()', function() {


describe('loadTranslations', () => {
const translations = {
'en': {
hello: 'Hello {name}',
level1: {
level2: 'we are in the second level'
}
},
'fr': {
hello: 'Bonjour {name}',
level1: {
level2: 'nous sommes dans le deuxième niveau'
}
},
'fr-CA': {
hello: 'Salut {name}'
}
};
it('should load translations in normal flow (transforming and flattening translations)', done => {
const assembler = {
getTemplates: () => Promise.resolve({}),
getTranslations: () => {
return Promise.resolve({
'en': {
hello: 'Hello {name}',
level1: {
level2: 'we are in the second level'
}
},
'fr': {
hello: 'Bonjour {name}',
level1: {
level2: 'nous sommes dans le deuxième niveau'
}
},
'fr-CA': {
hello: 'Salut {name}'
}
});
return Promise.resolve(translations);
}
};
const paper = new Paper(null, null, assembler);
Expand All @@ -202,6 +214,12 @@ describe('loadTranslations', () => {
done();
});
});

it('should add translations to paper', async () => {
const paper = new Paper(null, null, {});
paper.addTranslations(translations, 'en');
expect(paper.renderer.getTranslator().translate('hello', {name: 'Mario'})).to.equal('Hello Mario');
});
});

describe('Request Params', function() {
Expand Down

0 comments on commit 21b9c9b

Please sign in to comment.