LogoRobo.js
Packages@robojs/i18n

withLocale()

Function: withLocale()

Call Signature

function withLocale(local): <K>(key, params?) => Locale<K>

Binds a LocaleLike to produce a curried translator.

  • Loose mode (default): returns a t$ that accepts optional params based on ParamsFor<K>.
  • Strict mode: pass { strict: true } to return a tr$ that requires all params for keys that have any (using your MaybeArgs<K> tuple).

Overloads:

  • withLocale(local)<K>(key: K, params?: ParamsFor<K>) => ReturnOf<K>
  • withLocale(local, { strict: true })<K>(key: K, ...args: MaybeArgs<K>) => ReturnOf<K>

Parameters

ParameterTypeDescription
localanyA LocaleLike (string, { locale }, { guildLocale }, or a Discord Interaction).

Returns

Function

Type Parameters

Type Parameter
K extends Locale

Parameters

ParameterType
keyK
params?Locale<K>

Returns

Locale<K>

Examples

// Loose (default): params optional when message has params
const t$ = withLocale('en-US')
t$('common:hello.user', { user: { name: 'Robo' } })
t$('stats:pets.count', { count: 2 })
t$('common:ping') // key with no params
// Strict: params required when message has params
const tr$ = withLocale('en-US', { strict: true })
tr$('common:hello.user', { user: { name: 'Robo' } })  // ✅ required
// tr$('common:hello.user')                           // ❌ compile-time error
tr$('common:ping')                                    // ✅ key with no params

Call Signature

function withLocale(local, opts): <K>(key, ...args) => Locale<K>

Binds a LocaleLike to produce a curried translator.

  • Loose mode (default): returns a t$ that accepts optional params based on ParamsFor<K>.
  • Strict mode: pass { strict: true } to return a tr$ that requires all params for keys that have any (using your MaybeArgs<K> tuple).

Overloads:

  • withLocale(local)<K>(key: K, params?: ParamsFor<K>) => ReturnOf<K>
  • withLocale(local, { strict: true })<K>(key: K, ...args: MaybeArgs<K>) => ReturnOf<K>

Parameters

ParameterTypeDescription
localanyA LocaleLike (string, { locale }, { guildLocale }, or a Discord Interaction).
opts{ strict: true; }-
opts.stricttrue-

Returns

Function

Type Parameters

Type Parameter
K extends Locale

Parameters

ParameterType
keyK
...argsMaybeArgs<K>

Returns

Locale<K>

Examples

// Loose (default): params optional when message has params
const t$ = withLocale('en-US')
t$('common:hello.user', { user: { name: 'Robo' } })
t$('stats:pets.count', { count: 2 })
t$('common:ping') // key with no params
// Strict: params required when message has params
const tr$ = withLocale('en-US', { strict: true })
tr$('common:hello.user', { user: { name: 'Robo' } })  // ✅ required
// tr$('common:hello.user')                           // ❌ compile-time error
tr$('common:ping')                                    // ✅ key with no params

On this page