-
Notifications
You must be signed in to change notification settings - Fork 1k
[localize-tools] Keep expressions in attribute values for transform mode #2692
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@lit/localize-tools': patch | ||
| --- | ||
|
|
||
| Fix issue with placing expressions as html attribute values in transform mode |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,6 +353,78 @@ test('msg(string(<b>msg(string)</b>)) translated', () => { | |
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with msg as attr value', () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add one with a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, and also added additional tests for having multiple attributes in a single template expression so as to test the logic for looking at both the head and the middle.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| checkTransform( | ||
| 'html`Hello <b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`Hello <b bar=${"World"}>World</b>!`;' | ||
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with msg as attr value translated', () => { | ||
| checkTransform( | ||
| 'html`Hello <b bar=${msg("world", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`Hello <b bar=${`Mundo`}>World</b>!`;', | ||
| { | ||
| messages: [ | ||
| { | ||
| name: 'bar', | ||
| contents: ['Mundo'], | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with multiple msg as attr value', () => { | ||
| checkTransform( | ||
| 'html`<b foo=${msg("Hello", {id: "foo"})}>${"Hello"}</b>' + | ||
| '<b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`<b foo=${"Hello"}>Hello</b><b bar=${"World"}>World</b>!`;' | ||
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with multiple msg as attr value translated', () => { | ||
| checkTransform( | ||
| 'html`<b foo=${msg("Hello", {id: "foo"})}>${"Hello"}</b>' + | ||
| '<b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`<b foo=${`Hola`}>Hello</b><b bar=${`Mundo`}>World</b>!`;', | ||
| { | ||
| messages: [ | ||
| { | ||
| name: 'foo', | ||
| contents: ['Hola'], | ||
| }, | ||
| { | ||
| name: 'bar', | ||
| contents: ['Mundo'], | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with msg as property attr value', () => { | ||
| checkTransform( | ||
| 'html`Hello <b .bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`Hello <b .bar=${"World"}>World</b>!`;' | ||
| ); | ||
| }); | ||
|
|
||
| test('html(msg(string)) with msg as property attr value translated', () => { | ||
| checkTransform( | ||
| 'html`Hello <b .bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;', | ||
| 'html`Hello <b .bar=${`Mundo`}>World</b>!`;', | ||
| { | ||
| messages: [ | ||
| { | ||
| name: 'bar', | ||
| contents: ['Mundo'], | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test('import * as litLocalize', () => { | ||
| checkTransform( | ||
| ` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Do the tests in this PR fail if this is switched back to
template? If not, is there a test we could add that would cover it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon taking a closer look now, it seems we never enter this code block if a translation does occur because then
newTemplatewould always be ats.TemplateLiteral. The only time we enter this block is when we don't have to translate, i.e. generating original locale's version of the source, in which casenewTemplatewould not have changed from it's original assignment fromtemplatehere:lit/packages/localize-tools/src/modes/transform.ts
Line 335 in f78b4cc
So.. it was technically an oversight but of no consequence, hence none of our existing tests failed. For readability's sake I think keeping
newTemplatehere still makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm