{"id":16709404,"url":"https://github.com/necordjs/localization","last_synced_at":"2025-03-21T20:32:57.253Z","repository":{"id":231665209,"uuid":"782367901","full_name":"necordjs/localization","owner":"necordjs","description":"🌍 Localize your Discord bot with Necord!","archived":false,"fork":false,"pushed_at":"2024-10-28T14:51:38.000Z","size":2013,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-10-29T14:22:01.815Z","etag":null,"topics":["bot","discord","i18n","localization","necord","nestjs"],"latest_commit_sha":null,"homepage":"https://necord.org/recipes/localization","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/necordjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["SocketSomeone","necordjs"],"open_collective":"necord"}},"created_at":"2024-04-05T06:54:01.000Z","updated_at":"2024-10-28T14:51:35.000Z","dependencies_parsed_at":"2024-11-05T16:19:45.627Z","dependency_job_id":"fa8cbe86-26df-4a38-9520-a01e3d80c0e8","html_url":"https://github.com/necordjs/localization","commit_stats":{"total_commits":191,"total_committers":3,"mean_commits":"63.666666666666664","dds":"0.19895287958115182","last_synced_commit":"00a5a4c52ee663f32d0a1ed9ca9720bae29f99c6"},"previous_names":["necordjs/localization"],"tags_count":13,"template":false,"template_full_name":"SocketSomeone/nestjs-open-source-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necordjs%2Flocalization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necordjs%2Flocalization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necordjs%2Flocalization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necordjs%2Flocalization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/necordjs","download_url":"https://codeload.github.com/necordjs/localization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244866102,"owners_count":20523464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","discord","i18n","localization","necord","nestjs"],"created_at":"2024-10-12T20:04:44.929Z","updated_at":"2025-03-21T20:32:57.189Z","avatar_url":"https://github.com/necordjs.png","language":"TypeScript","funding_links":["https://github.com/sponsors/SocketSomeone","https://github.com/sponsors/necordjs","https://opencollective.com/necord"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n   \u003ch1\u003e\n       \u003ca href=\"#\"\u003e\u003cimg src=\"https://necord.org/img/logo.png\"\u003e\u003c/a\u003e\n   \u003c/h1\u003e\n  🌍 A lightweight Localization module for \u003cb\u003e\u003ca href=\"https://necord.org/\"\u003eNecord\u003c/a\u003e\u003c/b\u003e\n   \u003cbr/\u003e\u003cbr/\u003e\n   \u003ca href=\"https://necord.org\"\u003eDocumentation ✨\u003c/a\u003e \u0026emsp; \u003ca href=\"https://github.com/SocketSomeone/necord\"\u003eSource code 🪡\u003c/a\u003e \u0026emsp; \u003ca href=\"https://github.com/necordjs/samples\"\u003eExamples 🛠️\u003c/a\u003e \u0026emsp; \u003ca href=\"https://discord.gg/mcBYvMTnwP\"\u003eCommunity 💬\u003c/a\u003e\n\u003c/div\u003e\n\n\n\u003cbr/\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href='https://img.shields.io/npm/v/necord'\u003e\u003cimg src=\"https://img.shields.io/npm/v/necord\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\n    \u003ca href='https://img.shields.io/npm/l/necord'\u003e\u003cimg src=\"https://img.shields.io/npm/l/necord\" alt=\"NPM License\" /\u003e\u003c/a\u003e\n    \u003ca href='https://img.shields.io/npm/dm/necord'\u003e\u003cimg src=\"https://img.shields.io/npm/dm/necord\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\n    \u003ca href='https://img.shields.io/github/last-commit/necordjs/necord'\u003e\u003cimg src=\"https://img.shields.io/github/last-commit/SocketSomeone/necord\" alt=\"Last commit\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## About\n\n`@necord/localization` is a lightweight localization module for [Necord](https://necord.org/). It allows you to easily localize your bot's\ncommands and messages. The module provides a simple API for managing locales and translations, as well as a powerful localization adapter\nsystem.\n\n## Installation\n\n**Node.js 18.0.0 or newer is required.**\n\n```bash\n$ npm i @necord/localization necord discord.js\n$ yarn add @necord/localization necord discord.js\n$ pnpm add @necord/localization necord discord.js\n```\n\n## Usage\n\nOnce the installation process is complete, we can import the `NecordLocalizationModule` with your `NecordModule` into the\nroot `AppModule`:\n\n```typescript\nimport { NecordModule } from 'necord';\nimport { Module } from '@nestjs/common';\nimport { NecordLocalizationModule, DefaultLocalizationAdapter, UserResolver } from '@necord/localization';\nimport { AppService } from './app.service';\n\n@Module({\n    imports: [\n        NecordModule.forRoot({\n            token: process.env.DISCORD_TOKEN,\n            intents: [\n                IntentsBitField.Flags.Guilds,\n                IntentsBitField.Flags.DirectMessages,\n                IntentsBitField.Flags.GuildMembers,\n                IntentsBitField.Flags.GuildMessages,\n                IntentsBitField.Flags.MessageContent\n            ],\n            prefix: '!',\n            development: [process.env.DISCORD_TEST_GUILD]\n        }),\n        NecordLocalizationModule.forRoot({\n            resolvers: UserResolver,\n            // Also you can provide class for support injection by @Inject\n            adapter: new DefaultLocalizationAdapter({\n                fallbackLocale: 'en-US',\n                locales: {\n                    'en-US': {\n                        'commands.ping.name': 'ping',\n                        'commands.ping.description': 'Pong!'\n                    },\n                    ru: {\n                        'commands.ping.name': 'пинг',\n                        'commands.ping.description': 'Понг!'\n                    }\n                }\n            })\n        })\n    ],\n    providers\n})\nclass AppModule {\n}\n```\nAlso, you can create your own localization adapter. Just implement the `LocalizationAdapter` interface:\n\n```typescript\nimport { BaseLocalizationAdapter } from '@necord/localization';\n\ninterface CustomLocalizationOptions {\n    fallbackLocale: string;\n    locales: Record\u003cstring, Record\u003cstring, string\u003e\u003e;\n}\n\nexport class CustomLocalizationAdapter extends BaseLocalizationAdapter\u003cCustomLocalizationOptions\u003e {\n    public getTranslation(key: string, locale: string, ...args: any[]): string {\n        return `${key} by ${locale}`;\n    }\n}\n\n```\n\n\nThen, we can inject the `LOCALIZATION_ADAPTER` into our service and use it to localize our commands and messages:\n\n```typescript\nimport { Injectable, Inject, OnModuleInit } from '@nestjs/common';\nimport { DefaultLocalizationAdapter, localizationMapByKey, LOCALIZATION_ADAPTER } from '@necord/localization';\nimport { Context, SlashCommand, SlashCommandContext } from 'necord';\n\n@Injectable()\nexport class AppService implements OnModuleInit {\n    public constructor(\n        @Inject(LOCALIZATION_ADAPTER)\n        private readonly localizationAdapter: DefaultLocalizationAdapter\n    ) {\n    }\n\n    @SlashCommand({\n        name: 'ping',\n        description: 'Pong!',\n        nameLocalizations: localizationMapByKey('commands.ping.name'),\n        descriptionLocalizations: localizationMapByKey('commands.ping.description')\n    })\n    public ping(\n        @Context() [interaction]: SlashCommandContext,\n        @CurrentTranslate() t: TranslationFn\n    ) {\n        const message = t('commands.ping.description');\n        return interaction.reply(message);\n    }\n}\n```\n\nOr you can use `@CurrentTranslate` decorator to get the current translation from context:\n\n```typescript\nimport { Injectable, Inject, OnModuleInit } from '@nestjs/common';\nimport { DefaultLocalizationAdapter, CurrentTranslate, TranslationFn, localizationMapByKey } from '@necord/localization';\nimport { Context, SlashCommand, SlashCommandContext } from 'necord';\n\n@Injectable()\nexport class AppService implements OnModuleInit {\n    @SlashCommand({\n        name: 'ping',\n        description: 'Pong!',\n        nameLocalizations: localizationMapByKey('commands.ping.name'),\n        descriptionLocalizations: localizationMapByKey('commands.ping.description')\n    })\n    public ping(\n        @Context() [interaction]: SlashCommandContext,\n        @CurrentTranslate() t: TranslationFn\n    ) {\n        const message = t('commands.ping.description');\n        return interaction.reply(message);\n    }\n}\n```\n\nCongratulations! You have successfully created your first localized command with Necord!\n\n## Backers\n\n\u003ca href=\"https://opencollective.com/necord\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/necord/backers.svg?width=1000\"\u003e\u003c/a\u003e\n\n## Stay in touch\n\n* Author - [Alexey Filippov](https://t.me/socketsomeone)\n* Twitter - [@SocketSomeone](https://twitter.com/SocketSomeone)\n\n## License\n\n[MIT](https://github.com/necordjs/necord/blob/master/LICENSE) © [Alexey Filippov](https://github.com/SocketSomeone)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnecordjs%2Flocalization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnecordjs%2Flocalization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnecordjs%2Flocalization/lists"}