Function: t()
function t<K>(
locale,
key,
params?): Locale<K>
Formats a localized message by key with strongly-typed params inferred from your MF2 message.
- Supports
LocaleLike
: pass a locale string ('en-US'
) or any object with{ locale }
or{ guildLocale }
. - Accepts nested params (e.g.,
{ user: { name: 'Robo' } }
) which are auto-flattened to dotted paths. - Handles MF2 number/date/time (e.g.,
{$n :number}
,{$ts :time}
,{$ts :date}
); for date/time the param can beDate | number
.
π About namespaced keysβ
Keys are namespaced by file path:
/locales/<locale>/common.json
βcommon:<json-key>
/locales/<locale>/shared/common.json
βshared/common:<json-key>
- Deeper folders keep slash-separated segments (e.g.,
shared/common/example:<json-key>
). Thekey
argument must be the full namespaced key and is type-safe viaLocaleKey
.
Type Parametersβ
Type Parameter | Description |
---|---|
K extends LocaleKey | A key from your generated LocaleKey union (namespaced). |
Parametersβ
Parameter | Type | Description |
---|---|---|
locale | any | A LocaleLike ('en-US' , { locale: 'en-US' } , or a Discord Interaction/guild context). |
key | K | A namespaced key present in your /locales folder (e.g., common:hello.user ). |
params ? | any | Parameters inferred from the MF2 message (ParamsFor<K> ). Nested objects are allowed. |
Returnsβ
Locale
<K
>
The formatted string for the given locale and key.
Examplesβ
// /locales/en-US/common.json:
// { "hello.user": "Hello {$user.name}!" }
// Namespaced key becomes: "common:hello.user"
import { t } from '@robojs/i18n'
t('en-US', 'common:hello.user', { user: { name: 'Robo' } }) // "Hello Robo!"
// MF2 plural-style match (file: /locales/en-US/stats.json):
// { "pets.count": ".input {$count :number}\n.match $count\n one {{You have {$count} pet}}\n * {{You have {$count} pets}}" }
t('en-US', 'stats:pets.count', { count: 1 }) // "You have 1 pet"
t('en-US', 'stats:pets.count', { count: 3 }) // "You have 3 pets"
// Date/time (file: /locales/en-US/common.json):
// { "when.run": "Ran at {$ts :time style=short} on {$ts :date style=medium}" }
t('en-US', 'common:when.run', { ts: Date.now() })
// Using a Discord interaction object (has {locale} or {guildLocale}):
export default (interaction: ChatInputCommandInteraction) => {
return t(interaction, 'common:hello.user', { user: { name: interaction.user.username } })
}
Throwsβ
If the locale is unknown (no /locales/<locale>
loaded) or the key is missing in that locale.
Remarksβ
- You can also pass dotted params directly:
t('en-US', 'common:hello.user', { 'user.name': 'Robo' })
. - If different locales disagree on a paramβs kind, the generator safely widens the param type.