Skip to main content

Getting Started

An implementation of i18next's filesystem backend for Sapphire. It allows you to use a JSON-based languages directory to add internationalization for your bot using SapphireClient's fetchLanguage hook and a custom message extension, adding features such as sendTranslated and resolveKey.

Installation

npm install @sapphire/plugin-i18next @sapphire/framework discord.js

Usage

This registers the methods and options necessary for message translations in the Sapphire client.

// Main bot file
// Be sure to register the plugin before instantiating the client.
require('@sapphire/plugin-i18next/register');

The basic structure of a translation file is as follows:

{
"success": "Pong!",
"success_with_args": "Pong! Took me {{latency}}ms to reply"
}

The resolveKey function can be used anywhere to get translated text by its key. In this example, it is used in a method to send a message.

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

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}

public async messageRun(message: Message) {
await message.channel.send(await resolveKey('commands/ping:success'));
}
}

sendLocalized will send translated text resolved from a key to a specified channel.

import { sendLocalized } from '@sapphire/plugin-i18next';
import { Command } from '@sapphire/framework';

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}

public async messageRun(message: Message) {
await sendLocalized(message, 'commands/ping:success');
}
}

editLocalized edits a message, replacing its content with translated text resolved from its key.

import { editLocalized } from '@sapphire/plugin-i18next';
import { Command } from '@sapphire/framework';

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}

public async messageRun(message: Message) {
await editLocalized(message, 'commands/ping:success_args', { latency: ws.ping });
}
}

replyLocalized replies to a message, sending the text resolved from its key.

import { replyLocalized } from '@sapphire/plugin-i18next';
import { Command } from '@sapphire/framework';

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}

public async messageRun(message: Message) {
await replyLocalized(message, 'commands/ping:success_args', { latency: ws.ping });
}
}

fetchLanguage returns the guild-specific language that the client is using.

import { fetchLanguage } from '@sapphire/plugin-i18next';
import { Command } from '@sapphire/framework';

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}

public async messageRun(message: Message) {
return message.channel.send(await fetchLanguage(message));
// ===> en-US
}
}