Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damego/interactions-i18n
Add localization support to your interactions.py bot
https://github.com/damego/interactions-i18n
bot discord i18n interactions localization
Last synced: 20 days ago
JSON representation
Add localization support to your interactions.py bot
- Host: GitHub
- URL: https://github.com/damego/interactions-i18n
- Owner: Damego
- License: mit
- Created: 2022-11-22T11:03:54.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-28T17:31:04.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T23:29:15.551Z (about 1 month ago)
- Topics: bot, discord, i18n, interactions, localization
- Language: Python
- Homepage:
- Size: 52.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# interactions-i18n
Add localization support to your interactions.py bot## Installation
1. `pip uninstall discord-py-interactions`
2. `pip install git+https://github.com/interactions-py/[email protected]`
3. `pip install --upgrade interactions-i18n`## Usage
```py
import interactions
from interactions.ext.i18n import setupclient = interactions.Client(...)
# Load an i18n extension
i18n = setup(client)... # your cool commands and cogs loads
# Load a folder with localization
i18n.load("./locales/de")
client.start()
```### Usage in Extension
```py
from interactions import Extension
from interactions.ext.i18n import Localizationclass MyExt(Extension):
def __init__(self, client):
self.client = clientself.i18n: Localization = self.client.i18n
```## Creating localization files
1. Choose a language you want and find their code in the [Discord Locales Docs](https://discord.com/developers/docs/reference#locales)
2. Create a `[CODE]` folder with found code and put it in the folder with locales.
3. Create two files. First is `commands.json` for your commands and second is `custom.json` for anything you want## Example
Let's create a command with name `info` with some subcommands
```py
@client.command()
async def info(ctx: interactions.CommandContext):
...@info.group()
async def my_group(ctx: interactions.CommandContext):
...@my_group.subcommand()
@interactions.option()
async def user(ctx: interactions.CommandContext, member: interactions.Member):
loc = i18n.get_translate("some_key", ctx.locale)
await ctx.send(loc)
```### Structure of json files
`locales/de/commands.json`
This file will contain localizations for your commands
```json
{
"info": { // command name
"name": "...", // localized name
"description": "...", // localized description
"options": { // options of command. Command groups and subcommands are options btw
"my_group": {
"name": "...",
"description": "...",
"options": {
"user": {
"name": "...",
"description": "...",
"options": {
"member": {
"name": "...",
"description": "...",
// if your option have choices you can do:
"choices": {
"choice_name": "..."
}
}
}
}
}
}
}
}
}
````locales/de/custom.json`
This file will contain your custom localizations for anything
```json
{
"SOME_KEY": "Some value"
}
```## Automatic file generation
It seems difficult to write every command in the json, so you can generate file with your commands.
You need to only fill empty strings with your language```python
from interactions import Client, Locale
from interactions.ext.i18n import setupbot = Client(...)
i18n = setup(bot)
# i18n.load("./locales/de")... # your cool commands
# call this function in the end of main file
i18n.generate_files(Locale.GERMAN, "./locales/")
# bot.start() # comment line where starts your bot```