Js-lingui: Function `t` macros do not extract template strings

Created on 13 Nov 2020  路  9Comments  路  Source: lingui/js-lingui

Describe the bug
You cannot use template strings with the t function:

t({message: `template`})

results in

msgid "undefined"
msgstr "undefined"

Expected behavior

msgid "template"
msgstr "template"

Additional context

  • jsLingui version 3.2.1
  • Babel version 7.12.3
  • Your Babel config: Create React App
accepted 馃悶bug 馃摝 macro

All 9 comments

Same occurs with defineMessage macro, what should we do here? @tricoder42

Thanks, Sergio. Just wanted to add this remark after trying to find a workaround using defineMessage :-)

The problem is that neither macro handles template string.

// This workds
t({message: t`template`})

// Also this
t({message: "template"})

// This doesn't work
t({message: `template`})

I'm already working on the fix. Meanwhile please use these workaround 鈽濓笍

Nice! Works for me :-)

It is is also not possible to use components for t function.

i18n._(
  t({
    id: 'testID',
    components: [<a href="" />],
  })
),

It this supposed to work? In documentation it is specified that t is equivalent for https://lingui.js.org/tutorials/react-patterns.html?highlight=i18n%20_#element-attributes-and-string-only-translations

@MartinCerny-awin This definitely isn't possible. t macro and the whole lowercase macros family only returns strings. You must use Trans and other capitalized macros if you want to use components.

t - function - strings only
Trans - React component - renders string or components

@tricoder42 I wonder if this behavior is as expected:

// src/that/file.ts
import {t} from '@lingui/macro'

// ...

const nonComponentFn = (error?: any, messageId?: string) => {
  const helperText = error
    ? messageId
      ? t({id: messageId}) // this gets extracted as `undefined`
      : t`error` // this gets extracted as expected
    : undefined

  return {helperText}
}

// ...

The idea is that someone can pass an id of translation to that function so we should translate it here

And, extracted output is

#: src/that/file.ts:24
msgid "undefined"
msgstr ""

AFAIU, such t calls should not be extracted

Yeah, you're right. It shouldn't get extracted. Would you like to send a PR? This could be easy but grateful fix

@tricoder42 sure, I'll take a look

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ImADrafter picture ImADrafter  路  4Comments

ppozniak picture ppozniak  路  4Comments

smeevil picture smeevil  路  3Comments

benbender picture benbender  路  5Comments

MartinCerny-awin picture MartinCerny-awin  路  6Comments