intlMsg
intlMsg(key, values?, options?) resolves a message key from runtime messages and returns a plain string. Use this for server-side rendering, app-level formatting, and logic where you need only the text result.
It matches message behavior used by <intl-msg> so keys and placeholders are consistent across JS + components.
For rich HTML output use <intl-msg> instead and keep HTML in runtime messages only when trusted.
Signature
ts
1function intlMsg(2 key: string,3 values?: Record<string, unknown>,4 options?: {5 scope?: IntlRuntime6 locale?: string7 missing?: string | ((key: string) => string)8 }9): stringkey can use dot notation (checkout.total) for nested message objects.
What values means
values maps placeholders to replacements in the message template:
ts
1intlMsg('invoice.total', { amount: '$42.00' }, { scope: runtime })When a placeholder is missing, null, undefined, or omitted, it renders as an empty string.
Option map
| Option | Type | Default | Effect |
|---|---|---|---|
scope | IntlRuntime | getIntl() | Use explicit runtime instead of default runtime |
locale | string | scope/default locale | Render with a one-off locale |
missing | string | ((key) => string) | key | Fallback when message is not found |
Examples
Basic message + interpolation
ts
1import { createIntl, intlMsg } from '@beforesemicolon/intl'2 3const runtime = createIntl({4 locale: 'en-US',5 messages: {6 greeting: 'Hello {name}',7 invoice: { total: 'Total: {amount}' },8 items: {9 remaining: 'You have {count} items',10 },11 },12})13 14intlMsg('greeting', { name: 'Ari' }, { scope: runtime })15intlMsg('items.remaining', { count: 3 }, { scope: runtime })Nested key paths
ts
1intlMsg('invoice.total', { amount: '$42.00' }, { scope: runtime })2intlMsg('invoice.total', { amount: '$42.00' }, { locale: 'fr-FR' })Missing key behavior
ts
1intlMsg('missing', { name: 'Ari' }, { scope: runtime })2intlMsg('missing', { name: 'Ari' }, { scope: runtime, missing: 'fallback' })3intlMsg('missing', { name: 'Ari' }, {4 scope: runtime,5 missing: (key) => `[${key}]`,6})Empty and invalid inputs
ts
1intlMsg('', { name: 'Ari' }, { scope: runtime }) // ''