
Python i18n: संपूर्ण लोकलाइज़ेशन गाइड
JSON या YAML अनुवाद फ़ाइलों के साथ python-i18n सेट अप करें, प्लेसहोल्डर और बहुवचन सँभालें और फिर AI की मदद से अनुवाद ऑटोमेट करें।
python-i18n इंस्टॉल करें
python-i18n, Python के लिए एक हल्की अंतरराष्ट्रीयकरण लाइब्रेरी है। यह JSON और YAML अनुवाद फ़ाइलों, नेस्टेड कुंजियों, प्लेसहोल्डर इंटरपोलेशन और बहुवचन का बिना अतिरिक्त सेटअप के समर्थन करती है।
pip install python-i18n# To use YAML translation files instead of JSON:
pip install python-i18n[YAML]अनुवाद कॉन्फ़िगर करें
फ़ाइल फ़ॉर्मैट सेट करें, अनुवाद फ़ाइलों के पाथ जोड़ें और अपने डिफ़ॉल्ट तथा फ़ॉलबैक लोकेल कॉन्फ़िगर करें। कोई भी अनुवाद कॉल करने से पहले इस कॉन्फ़िगरेशन को अपने एप्लिकेशन के एंट्री पॉइंट पर इंपोर्ट करें।
import i18n
# Set the file format (json or yaml)
i18n.set("file_format", "json")
# Add the directory containing your translation files
i18n.load_path.append("translations/")
# Set the default locale
i18n.set("locale", "en")
# Set the fallback locale (used when a key is missing)
i18n.set("fallback", "en")
# Enable/disable error on missing translations
i18n.set("error_on_missing_translation", False)अनुवाद फ़ाइलें बनाएँ
JSON या YAML फ़ॉर्मैट में हर भाषा के लिए एक फ़ाइल बनाएँ। स्ट्रिंग को सुविधा या पेज के अनुसार व्यवस्थित करने के लिए नेस्टेड कुंजियों का इस्तेमाल करें। अपनी स्रोत भाषा (आमतौर पर अंग्रेज़ी) को एकमात्र प्रामाणिक स्रोत बनाए रखें।
// translations/en.json
{
"greeting": "Hello!",
"welcome": "Welcome to our application",
"nav": {
"home": "Home",
"about": "About",
"settings": "Settings"
},
"cart": {
"item_count": "%{count} item(s) in your cart"
}
}
// translations/de.json
{
"greeting": "Hallo!",
"welcome": "Willkommen in unserer Anwendung",
"nav": {
"home": "Startseite",
"about": "Über uns",
"settings": "Einstellungen"
},
"cart": {
"item_count": "%{count} Artikel in Ihrem Warenkorb"
}
}अपने कोड में अनुवाद इस्तेमाल करें
अनुवादित स्ट्रिंग खोजने के लिए डॉट से अलग किए गए कुंजी पाथ के साथ i18n.t() को कॉल करें। ग्लोबल सेटिंग बदले बिना हर कॉल के लिए लोकेल ओवरराइड किया जा सकता है।
import i18n
# Simple translation
print(i18n.t("greeting")) # "Hello!"
print(i18n.t("nav.home")) # "Home"
print(i18n.t("nav.about")) # "About"
# Translation with a specific locale
print(i18n.t("greeting", locale="de")) # "Hallo!"
print(i18n.t("nav.home", locale="ja")) # "ホーム"
# Missing key returns a placeholder
print(i18n.t("missing.key")) # "Missing.Key"प्लेसहोल्डर और बहुवचन
python-i18n प्लेसहोल्डर के लिए %{name} सिंटैक्स के साथ इंटरपोलेशन और 'zero', 'one' तथा 'many' उप-कुंजियों के साथ बुनियादी बहुवचन का समर्थन करती है। दोनों सुविधाओं के लिए i18n.t() में कीवर्ड आर्ग्युमेंट पास करें।
# translations/en.json
# {
# "welcome_user": "Welcome, %{name}!",
# "order_status": "Order #%{order_id}: %{status}",
# "file_size": "File size: %{size} %{unit}"
# }
import i18n
# Single placeholder
print(i18n.t("welcome_user", name="Alice"))
# "Welcome, Alice!"
# Multiple placeholders
print(i18n.t("order_status", order_id=12345, status="shipped"))
# "Order #12345: shipped"
# Reusable with different values
print(i18n.t("file_size", size=2.5, unit="MB"))
# "File size: 2.5 MB"
print(i18n.t("file_size", size=800, unit="KB"))
# "File size: 800 KB"# translations/en.json
# {
# "inbox": {
# "zero": "No messages",
# "one": "1 message",
# "many": "%{count} messages"
# }
# }
import i18n
print(i18n.t("inbox", count=0)) # "No messages"
print(i18n.t("inbox", count=1)) # "1 message"
print(i18n.t("inbox", count=42)) # "42 messages"रनटाइम पर लोकेल बदलना
i18n.set('locale', code) से सक्रिय लोकेल को ग्लोबल स्तर पर बदलें या locale कीवर्ड आर्ग्युमेंट से हर कॉल के लिए इसे ओवरराइड करें। वेब फ़्रेमवर्क में अनुरोध से यूज़र की पसंदीदा भाषा पहचानें और रेंडरिंग से पहले लोकेल सेट करें।
import i18n
# Set locale globally
i18n.set("locale", "de")
print(i18n.t("greeting")) # "Hallo!"
# Switch to Japanese
i18n.set("locale", "ja")
print(i18n.t("greeting")) # "こんにちは!"
# Override per-call without changing global locale
i18n.set("locale", "en")
print(i18n.t("greeting")) # "Hello!"
print(i18n.t("greeting", locale="de")) # "Hallo!"from flask import Flask, request, g
import i18n
app = Flask(__name__)
i18n.set("file_format", "json")
i18n.load_path.append("translations/")
SUPPORTED_LOCALES = ["en", "de", "ja", "es", "fr"]
@app.before_request
def set_locale():
# Check URL parameter, cookie, then Accept-Language header
locale = request.args.get("lang")
if not locale:
locale = request.cookies.get("locale")
if not locale:
locale = request.accept_languages.best_match(SUPPORTED_LOCALES)
g.locale = locale or "en"
i18n.set("locale", g.locale)
@app.route("/")
def index():
return i18n.t("welcome")python-i18n-locale-chain के साथ स्मार्ट लोकेल फ़ॉलबैक
डिफ़ॉल्ट रूप से python-i18n केवल एक फ़ॉलबैक लोकेल का समर्थन करती है। जब किसी pt-BR यूज़र के लिए pt-BR अनुवाद उपलब्ध नहीं होते, तो लाइब्रेरी उपयोगी pt-PT अनुवादों को अनदेखा करके सीधे अंग्रेज़ी फ़ॉलबैक पर चली जाती है। python-i18n-locale-chain, 75 लोकेल वैरिएंट को कवर करने वाली कॉन्फ़िगर करने योग्य फ़ॉलबैक चेन से इसे ठीक करती है।
pip install python-i18n-locale-chainfrom locale_chain import configure
import i18n
i18n.set("file_format", "json")
i18n.load_path.append("translations/")
# Activate smart fallback chains (75 built-in chains)
configure()
# Now pt-BR falls back to pt-PT -> pt -> en (instead of just en)
result = i18n.t("greeting", locale="pt-BR")
# es-MX falls back to es-419 -> es -> en
result = i18n.t("greeting", locale="es-MX")
# zh-Hant-HK falls back to zh-Hant-TW -> zh-Hant -> en
result = i18n.t("greeting", locale="zh-Hant-HK")from locale_chain import configure, reset
# Override specific chains
configure(overrides={
"pt-BR": ["pt"], # Skip pt-PT, go straight to pt
"ja-JP": ["ja"], # Add a new chain
})
# Full custom map (no defaults)
configure(
fallbacks={"pt-BR": ["pt-PT"]},
merge_defaults=False
)
# Use German as final fallback instead of English
configure(default_locale="de")
# Restore original i18n.t() behaviour
reset()अनुवाद ऑटोमेट करें
i18n सेटअप पूरा होने के बाद AI की मदद से अपनी लोकेल फ़ाइलों का अनुवाद करें। अपने IDE में AI असिस्टेंट से स्रोत फ़ाइल का अनुवाद करने के लिए कहें या अपनी CI/CD पाइपलाइन में i18n Agent CLI इस्तेमाल करें।
# In your IDE, ask your AI assistant:
> Translate translations/en.json to German, Japanese, and Spanish
translations/de.json created (1.2s)
translations/ja.json created (1.5s)
translations/es.json created (1.1s)
# Or use the CLI in CI/CD:
npx i18n-agent translate translations/en.json --lang de,ja,esअनुवाद की गुणवत्ता ऑटोमेट करें
आम समस्याएँ
अनुवाद के बजाय मूल कुंजियाँ लौटना
YAML फ़ाइलें लोड नहीं होना
नेस्टेड कुंजियों की खोज विफल होना
अनुरोधों के बीच लोकेल बदलाव का असर पड़ना
सुझाई गई फ़ाइल संरचना
my-python-app/
├── translations/
│ ├── en.json # Source language (JSON)
│ ├── de.json # German
│ ├── ja.json # Japanese
│ ├── es.json # Spanish
│ └── pt-BR.json # Brazilian Portuguese
├── app.py # Application entry point
├── i18n_config.py # i18n setup and configuration
├── requirements.txt # pip dependencies
└── pyproject.toml # Project metadata
# Or with YAML files:
my-python-app/
├── translations/
│ ├── en.yml
│ ├── de.yml
│ └── ja.yml
├── app.py
└── ...इनका भी अनुवाद करें:
i18n Agent अभी आज़माएँ
अपनी अनुवाद फ़ाइल यहाँ छोड़ें
JSON, YAML, PO, XML, CSV, Markdown, Properties
या ब्राउज़ करने के लिए क्लिक करें
लक्षित भाषाएँ
python-i18n-locale-chain के साथ लोकेल फ़ॉलबैक
जब es-419 जैसे किसी क्षेत्रीय लोकेल में अनुवाद कुंजी नहीं मिलती, तो python-i18n पहले पैरेंट लोकेल es जाँचने के बजाय सीधे डिफ़ॉल्ट लोकेल पर चली जाती है।
pip install python-i18n-locale-chainfrom i18n_locale_chain import configure_chain
configure_chain('{')
'es': ['en', 'ru'],
'pt-BR': ['pt', 'en'],
'zh-Hant-HK': ['zh-Hant', 'zh', 'en'],
'}')
# Usage: t('greeting', locale='es') — falls back through chainसमर्थित फ़्रेमवर्क और 75 बिल्ट-इन चेन की पूरी सूची के लिए हमारी लोकेल फ़ॉलबैक गाइड देखें। Learn more →