@necord/localization
Version:
Localize your Discord bot with Necord!
173 lines (141 loc) • 6.19 kB
Markdown
<div align="center">
<h1>
<a href="#"><img src="https://necord.org/img/logo.png"></a>
</h1>
🌍 A lightweight Localization module for <b><a href="https://necord.org/">Necord</a></b>
<br/><br/>
<a href="https://necord.org">Documentation ✨</a>   <a href="https://github.com/SocketSomeone/necord">Source code 🪡</a>   <a href="https://github.com/necordjs/samples">Examples 🛠️</a>   <a href="https://discord.gg/mcBYvMTnwP">Community 💬</a>
</div>
<br/>
<p align="center">
<a href='https://img.shields.io/npm/v/necord'><img src="https://img.shields.io/npm/v/necord" alt="NPM Version" /></a>
<a href='https://img.shields.io/npm/l/necord'><img src="https://img.shields.io/npm/l/necord" alt="NPM License" /></a>
<a href='https://img.shields.io/npm/dm/necord'><img src="https://img.shields.io/npm/dm/necord" alt="NPM Downloads" /></a>
<a href='https://img.shields.io/github/last-commit/necordjs/necord'><img src="https://img.shields.io/github/last-commit/SocketSomeone/necord" alt="Last commit" /></a>
</p>
## About
`/localization` is a lightweight localization module for [Necord](https://necord.org/). It allows you to easily localize your bot's
commands and messages. The module provides a simple API for managing locales and translations, as well as a powerful localization adapter
system.
## Installation
**Node.js 18.0.0 or newer is required.**
```bash
$ npm i /localization necord discord.js
$ yarn add /localization necord discord.js
$ pnpm add /localization necord discord.js
```
## Usage
Once the installation process is complete, we can import the `NecordLocalizationModule` with your `NecordModule` into the
root `AppModule`:
```typescript
import { NecordModule } from 'necord';
import { Module } from '@nestjs/common';
import { NecordLocalizationModule, DefaultLocalizationAdapter, UserResolver } from '@necord/localization';
import { AppService } from './app.service';
class AppModule {
}
```
Also, you can create your own localization adapter. Just implement the `LocalizationAdapter` interface:
```typescript
import { BaseLocalizationAdapter } from '@necord/localization';
interface CustomLocalizationOptions {
fallbackLocale: string;
locales: Record<string, Record<string, string>>;
}
export class CustomLocalizationAdapter extends BaseLocalizationAdapter<CustomLocalizationOptions> {
public getTranslation(key: string, locale: string, ...args: any[]): string {
return `${key} by ${locale}`;
}
}
```
Then, we can inject the `LOCALIZATION_ADAPTER` into our service and use it to localize our commands and messages:
```typescript
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import { DefaultLocalizationAdapter, localizationMapByKey, LOCALIZATION_ADAPTER } from '@necord/localization';
import { Context, SlashCommand, SlashCommandContext } from 'necord';
export class AppService implements OnModuleInit {
public constructor(
private readonly localizationAdapter: DefaultLocalizationAdapter
) {
}
public ping(
[interaction]: SlashCommandContext,
t: TranslationFn
) {
const message = t('commands.ping.description');
return interaction.reply(message);
}
}
```
Or you can use `` decorator to get the current translation from context:
```typescript
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import { DefaultLocalizationAdapter, CurrentTranslate, TranslationFn, localizationMapByKey } from '@necord/localization';
import { Context, SlashCommand, SlashCommandContext } from 'necord';
export class AppService implements OnModuleInit {
public ping(
[interaction]: SlashCommandContext,
t: TranslationFn
) {
const message = t('commands.ping.description');
return interaction.reply(message);
}
}
```
Congratulations! You have successfully created your first localized command with Necord!
## Backers
<a href="https://opencollective.com/necord" target="_blank"><img src="https://opencollective.com/necord/backers.svg?width=1000"></a>
## Stay in touch
* Author - [Alexey Filippov](https://t.me/socketsomeone)
* Twitter - [](https://twitter.com/SocketSomeone)
## License
[MIT](https://github.com/necordjs/necord/blob/master/LICENSE) © [Alexey Filippov](https://github.com/SocketSomeone)