Skip to content

Support id and comment in t macro #800

@tricoder42

Description

@tricoder42

This is often requested feature — t macro should accept custom id and/or comment. Using defineMessage in combination with i18n._ is unnecessary verbose.

Proposal

TLDR: Same api as defineMessage, but the output will be wrapped in i18n._ as t and other macros does:

t({
  id: 'msgId,
  message: 'text'
})
// ↓ ↓ ↓ ↓ ↓ ↓
i18n._(/*i18n*/{ id: "msgId", values: { name }, message: 'text' })


t({
  id: 'msgId,
  message: 'text',
  comment: 'description for translators'
})
// ↓ ↓ ↓ ↓ ↓ ↓
i18n._(/*i18n*/{ id: "msgId", values: { name }, message: 'text', comment: 'description for translators' })


t({
  id: 'msgId,
  message: plural(value, { one: '...', other: '...' })
})

// ↓ ↓ ↓ ↓ ↓ ↓
i18n._(/*i18n*/{ id: "msgId", values: { name }, message: '{value, plural, one {...} other {...}' })

// Important! This should still work:
t`Hello World`
i18n._(/*i18n*/{ id: 'Hello World')

This should be fairly easy, because all logic for parsing messge descriptors is already inside defineMessage macro. It just needs an extra if in the right location, so the t macro can be used either as a template literal tag or a function.

Tasks

  • update t macro in macroJs.ts to also accept message descriptor, the same way how defineMessage does
  • add tests to js-t.ts to cover new usecases
  • update t macro docs

Related issues: #797 #701

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions