Skip to content

Internationalization API ​

Location: src/lib/i18n.ts

Internationalization (i18n) helpers for multi-language support.

Supported Languages ​

typescript
import { AVAILABLE_LANGUAGES } from '#lib/i18n.js';

// [
//   { code: 'en-US', name: 'English (US)', flag: 'πŸ‡ΊπŸ‡Έ' },
//   { code: 'es-ES', name: 'EspaΓ±ol', flag: 'πŸ‡ͺπŸ‡Έ' },
//   { code: 'fr-FR', name: 'FranΓ§ais', flag: 'πŸ‡«πŸ‡·' },
// ]

Translation Files ​

Translation files are located in languages/:

languages/
β”œβ”€β”€ en-US/
β”‚   └── commands/
β”‚       β”œβ”€β”€ ping.json
β”‚       └── ...
└── es-ES/
    └── commands/
        β”œβ”€β”€ ping.json
        └── ...

Example translation file (languages/en-US/commands/ping.json):

json
{
  "description": "Check the bot latency",
  "response": "Pong! Latency: {{latency}}ms"
}

Functions ​

getGuildLanguage(guildId) ​

Get the language setting for a guild.

typescript
import { getGuildLanguage } from '#lib/i18n.js';

const language = await getGuildLanguage(guild.id);
// 'en-US' (default) or the guild's configured language
ParameterTypeDescription
guildIdstringDiscord guild ID

Returns: Promise<string> - Language code (falls back to 'en-US')


getLanguageName(code) ​

Get the display name for a language code.

typescript
import { getLanguageName } from '#lib/i18n.js';

getLanguageName('en-US'); // 'πŸ‡ΊπŸ‡Έ English (US)'
getLanguageName('es-ES'); // 'πŸ‡ͺπŸ‡Έ EspaΓ±ol'
getLanguageName('unknown'); // 'unknown'
ParameterTypeDescription
codestringLanguage code

Returns: string - Display name with flag emoji


isValidLanguage(code) ​

Check if a language code is supported.

typescript
import { isValidLanguage } from '#lib/i18n.js';

isValidLanguage('en-US'); // true
isValidLanguage('de-DE'); // false
ParameterTypeDescription
codestringLanguage code to check

Returns: boolean


resolveKeyForInteraction(interaction, key, values?) ​

Resolve a translation key for an interaction.

typescript
import { resolveKeyForInteraction } from '#lib/i18n.js';

const message = await resolveKeyForInteraction(
  interaction,
  'commands/ping:response',
  { latency: client.ws.ping }
);
// 'Pong! Latency: 50ms'
ParameterTypeDescription
interactionInteractionDiscord interaction
keystringTranslation key (namespace:key format)
valuesRecord<string, unknown>?Interpolation values

Returns: Promise<string>


resolveKeyForMessage(message, key, values?) ​

Resolve a translation key for a message.

typescript
import { resolveKeyForMessage } from '#lib/i18n.js';

const content = await resolveKeyForMessage(
  message,
  'commands/help:title'
);
ParameterTypeDescription
messageMessageDiscord message
keystringTranslation key
valuesRecord<string, unknown>?Interpolation values

Returns: Promise<string>


Using i18n in Commands ​

Basic Usage ​

typescript
import { Command } from '@sapphire/framework';
import { resolveKeyForInteraction } from '#lib/i18n.js';

export class PingCommand extends Command {
  public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
    const latency = this.container.client.ws.ping;

    const response = await resolveKeyForInteraction(
      interaction,
      'commands/ping:response',
      { latency }
    );

    return interaction.reply(response);
  }
}

With Sapphire's resolveKey ​

You can also use Sapphire's built-in resolveKey:

typescript
import { resolveKey } from '@sapphire/plugin-i18next';

const message = await resolveKey(interaction, 'commands/ping:response', {
  latency: client.ws.ping
});

Command Descriptions ​

Use i18n for command descriptions:

typescript
import { ApplyOptions } from '@sapphire/decorators';
import { Command } from '@sapphire/framework';

@ApplyOptions<Command.Options>({
  name: 'ping',
  description: 'commands/ping:description',
})
export class PingCommand extends Command {
  // ...
}

Adding a New Language ​

  1. Create a new directory in languages/:

    languages/de-DE/
  2. Add translation files matching the structure:

    languages/de-DE/commands/ping.json
  3. Add the language to AVAILABLE_LANGUAGES in src/lib/i18n.ts:

    typescript
    export const AVAILABLE_LANGUAGES = [
      { code: 'en-US', name: 'English (US)', flag: 'πŸ‡ΊπŸ‡Έ' },
      { code: 'es-ES', name: 'EspaΓ±ol', flag: 'πŸ‡ͺπŸ‡Έ' },
      { code: 'fr-FR', name: 'FranΓ§ais', flag: 'πŸ‡«πŸ‡·' },
      { code: 'de-DE', name: 'Deutsch', flag: 'πŸ‡©πŸ‡ͺ' }, // New
    ] as const;

Interpolation ​

Use syntax for dynamic values:

json
{
  "welcome": "Welcome {{username}} to {{server}}!",
  "level_up": "Congratulations! You reached level {{level}}."
}
typescript
await resolveKeyForInteraction(interaction, 'messages:welcome', {
  username: user.username,
  server: guild.name
});

CATTO v2.x