Skip to content

Commit

Permalink
feat(i18n): support for [text]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayan751 committed Aug 27, 2019
1 parent 93ec221 commit 2576139
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/__tests__/i18n/t/translation-integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,19 @@ describe('translation-integration', function () {
assertTextContent(host, `span#b`, translation.simple.text);
});

it('works for textContent replacement with explicit [text] attribute - `t="[text]key"`', async function () {

@customElement({
name: 'app', template: `<span id='a' t='[text]simple.text'></span>`
})
class App { }

const host = DOM.createElement('app');
const { en } = await setup(host, new App());

assertTextContent(host, 'span', en.simple.text);
});

it('works for innerHTML replacement - `t="[html]key"`', async function () {

@customElement({
Expand Down
12 changes: 9 additions & 3 deletions packages/i18n/src/t/translation-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface ContentValue {
append?: string;
}

const attributeAliases = new Map([['text', 'textContent'], ['html', 'innerHTML']]);

@connectable()
export class TranslationBinding implements IPartialConnectableBinding {
public id!: number;
Expand Down Expand Up @@ -177,10 +179,14 @@ export class TranslationBinding implements IPartialConnectableBinding {
if (attributes.length === 0) {
attributes = this.target.tagName === 'IMG' ? ['src'] : ['textContent'];
}
const htmlIndex = attributes.findIndex((attr) => attr === 'html');
if (htmlIndex > -1) {
attributes.splice(htmlIndex, 1, 'innerHTML');

for (const [alias, attribute] of attributeAliases) {
const aliasIndex = attributes.findIndex((attr) => attr === alias);
if (aliasIndex > -1) {
attributes.splice(aliasIndex, 1, attribute);
}
}

return attributes;
}

Expand Down

0 comments on commit 2576139

Please sign in to comment.