diff --git a/packages/macro/src/macroJs.ts b/packages/macro/src/macroJs.ts index e57207db7..1640a9eef 100644 --- a/packages/macro/src/macroJs.ts +++ b/packages/macro/src/macroJs.ts @@ -193,7 +193,12 @@ export default class MacroJs { // if there's `message` property, replace macros with formatted message const node = descriptor.properties[messageIndex] - const tokens = this.tokenizeNode(node.value, true) + + // Inside message descriptor the `t` macro in `message` prop is optional. + // Template strings are always processed as if they were wrapped by `t`. + const tokens = this.types.isTemplateLiteral(node.value) + ? this.tokenizeTemplateLiteral(node.value) + : this.tokenizeNode(node.value, true) let messageNode = node.value if (tokens != null) { @@ -351,7 +356,6 @@ export default class MacroJs { } } - /** * Custom matchers */ diff --git a/packages/macro/test/js-defineMessage.ts b/packages/macro/test/js-defineMessage.ts index d44a74558..bf4efa47c 100644 --- a/packages/macro/test/js-defineMessage.ts +++ b/packages/macro/test/js-defineMessage.ts @@ -36,11 +36,11 @@ export default [ `, }, { - name: "should left string message intact - template literal", + name: "should transform template literals", input: ` import { defineMessage } from '@lingui/macro'; const message = defineMessage({ - message: \`Message\` + message: \`Message \${name}\` }) `, expected: ` @@ -48,7 +48,10 @@ export default [ const message = /*i18n*/ { - id: \`Message\` + id: "Message {name}", + values: { + name: name + } }; `, }, diff --git a/packages/macro/test/js-t.ts b/packages/macro/test/js-t.ts index 4f41b6495..b1bf76668 100644 --- a/packages/macro/test/js-t.ts +++ b/packages/macro/test/js-t.ts @@ -82,6 +82,23 @@ export default [ i18n._("Multiline\\nstring") `, }, + { + name: "Support template strings in t macro message", + input: ` + import { t } from '@lingui/macro' + const msg = t({ message: \`Hello \${name}\` }) + `, + expected: `import { i18n } from "@lingui/core"; + const msg = + i18n._(/*i18n*/ + { + id: "Hello {name}", + values: { + name: name, + }, + }); + `, + }, { name: "Support id and comment in t macro as callExpression", input: `