A pycord extension for internationalization and localization..
Project description
pycord-localizer
A pycord extension for internationalization and localization.
Installation
# linux / macOS
pip3 install pycord-localizer
# windows
pip install pycord-localizer
Quick Start
1. Create Localization Files
Create JSON files for each language you want to support:
zh-TW.json
{
"strings": {
"Hello, world!": "你好,世界!"
},
"commands": {
"greet": {
"name": "問候",
"description": "向使用者問候",
"options": {
"user": {
"name": "使用者",
"description": "選擇要問候的使用者"
}
}
}
},
"context_menus": {
"user_info": {
"name": "使用者資訊"
}
}
}
2. Setup in Your Bot
import json
from discord import Bot, Option
from pycord_localizer import I18n, t
# Load localization files
with open("zh-TW.json", encoding="utf-8") as f:
zh_tw = json.load(f)
with open("ja.json", encoding="utf-8") as f:
ja = json.load(f)
# Create bot and setup i18n
bot = Bot()
i18n = I18n(bot, zh_TW=zh_tw, ja=ja)
# Define commands
@i18n.localize
@bot.slash_command()
async def greet(ctx, user: Option(discord.Member, "Select a user")):
await ctx.respond(t("Hello, {0}!", user.mention))
bot.run("YOUR_TOKEN")
Documentation
Supported Locales
"id", "da", "de", "en-GB", "en-US", "es-ES", "es-419", "fr", "hr", "it",
"lt", "hu", "nl", "no", "pl", "pt-BR", "ro", "fi", "sv-SE", "vi", "tr",
"cs", "el", "bg", "ru", "uk", "hi", "th", "zh-CN", "ja", "zh-TW", "ko"
Localization Structure
{
"strings": {
"key": "translated text",
"format {0}": "格式化 {0}"
},
"commands": {
"command_name": {
"name": "localized_name",
"description": "localized_description",
"options": {
"option_name": {
"name": "localized_option",
"description": "localized_description",
"choices": {
"choice_value": "localized_choice_name"
}
}
}
}
},
"context_menus": {
"context_menu_name": {
"name": "localized_menu_name"
}
}
}
Examples
Slash Command with Choices
from discord import Option, OptionChoice
@i18n.localize
@bot.slash_command()
async def language(
ctx,
lang: Option(
str,
"Choose your preferred language",
choices=[
OptionChoice(name="English", value="en-US"),
OptionChoice(name="Traditional Chinese", value="zh-TW"),
OptionChoice(name="Japanese", value="ja"),
OptionChoice(name="German", value="de"),
]
)
):
await ctx.respond(f"Language set to: {lang}")
Localization file:
{
"commands": {
"language": {
"name": "語言",
"description": "選擇你的偏好語言",
"options": {
"lang": {
"name": "語言",
"description": "選擇你偏好的語言",
"choices": {
"en-US": "英文",
"zh-TW": "繁體中文",
"ja": "日文",
"de": "德文"
}
}
}
}
}
}
Context Menu Commands
# User command (right-click on user)
@i18n.localize
@bot.user_command(name="user_info")
async def user_info(ctx, member):
await ctx.respond(_("User: {0}", member.name))
# Message command (right-click on message)
@i18n.localize
@bot.message_command(name="get_id")
async def get_id(ctx, message):
await ctx.respond(_("Message ID: {0}", message.id))
Batch Localization
Instead of decorating each command, you can localize all at once:
# Define all commands first
@bot.slash_command()
async def hello(ctx):
await ctx.respond(_("Hello!"))
@bot.user_command(name="user_info")
async def user_info(ctx, member):
await ctx.respond(f"User: {member.name}")
# Then localize all commands
i18n.localize_commands()
String Formatting
# Simple formatting
await ctx.respond(_("Hello, {0}!", user.name))
# Multiple arguments
await ctx.respond(_("User {0} has {1} points", name, points))
User vs Server Locale
# Default: Use server locale
i18n = I18n(bot, zh_TW=zh_tw)
# Use user's preferred locale
i18n = I18n(bot, consider_user_locale=True, zh_TW=zh_tw)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycord_localizer-0.1.3.tar.gz.
File metadata
- Download URL: pycord_localizer-0.1.3.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
590586995f79122e73da23510be619a165415fa2d8311d2aa8caef7d8ee9e498
|
|
| MD5 |
e41dac010c6cb9c5963ed8394e9db761
|
|
| BLAKE2b-256 |
3e3fcb46377da5924b70ede2d867d1e4fd4f6f1c11d26f327e9beec5ed573232
|
Provenance
The following attestation bundles were made for pycord_localizer-0.1.3.tar.gz:
Publisher:
publish.yml on ParrotXray/pycord-localizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycord_localizer-0.1.3.tar.gz -
Subject digest:
590586995f79122e73da23510be619a165415fa2d8311d2aa8caef7d8ee9e498 - Sigstore transparency entry: 591393588
- Sigstore integration time:
-
Permalink:
ParrotXray/pycord-localizer@dac5ae16bd9b27be18848fadfbcec4781d402dda -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/ParrotXray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dac5ae16bd9b27be18848fadfbcec4781d402dda -
Trigger Event:
release
-
Statement type:
File details
Details for the file pycord_localizer-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pycord_localizer-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
439f39caab08c42f9870ed5b8c294503a188a8c84fc6b4a5fbd96c98cc9a38cf
|
|
| MD5 |
28955ffb0bf4abd7d95cc77bd5a9497f
|
|
| BLAKE2b-256 |
6b4b0c72d381876ff76d9e87618c95986eb39f49037d7499b0c6ede9f226ff60
|
Provenance
The following attestation bundles were made for pycord_localizer-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on ParrotXray/pycord-localizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycord_localizer-0.1.3-py3-none-any.whl -
Subject digest:
439f39caab08c42f9870ed5b8c294503a188a8c84fc6b4a5fbd96c98cc9a38cf - Sigstore transparency entry: 591393594
- Sigstore integration time:
-
Permalink:
ParrotXray/pycord-localizer@dac5ae16bd9b27be18848fadfbcec4781d402dda -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/ParrotXray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dac5ae16bd9b27be18848fadfbcec4781d402dda -
Trigger Event:
release
-
Statement type: