Google Translate library with support for free and API key-based translation services
—
pygtrans provides comprehensive language support with predefined dictionaries containing language codes and their corresponding names. The library supports 242 source languages (including auto-detection) and 243 target languages, covering virtually all world languages and many regional variants.
Contains all supported source languages including the special "auto" option for automatic language detection.
SOURCE_LANGUAGES: dict[str, str] = {
"auto": "检测语言",
"ab": "阿布哈兹语",
"sq": "阿尔巴尼亚语",
"aa": "阿法尔语",
"ar": "阿拉伯语",
"am": "阿姆哈拉语",
"en": "英语",
"zh-CN": "中文",
"fr": "法语",
"es": "西班牙语",
"de": "德语",
"ja": "日语",
"ko": "韩语",
"pt": "葡萄牙语(巴西)",
"pt-PT": "葡萄牙语(葡萄牙)",
"ru": "俄语",
"it": "意大利语",
"hi": "印地语",
"th": "泰语",
"vi": "越南语",
"tr": "土耳其语",
# ... and 222 more languages
}Usage Examples:
from pygtrans import SOURCE_LANGUAGES
# Check if a language is supported as source
if 'en' in SOURCE_LANGUAGES:
print(f"English is supported: {SOURCE_LANGUAGES['en']}") # '英语'
# List all supported source languages
print(f"Total source languages: {len(SOURCE_LANGUAGES)}") # 242
# Display first 10 source languages
for i, (code, name) in enumerate(SOURCE_LANGUAGES.items()):
if i < 10:
print(f"{code}: {name}")
# auto: 检测语言
# ab: 阿布哈兹语
# sq: 阿尔巴尼亚语
# ...
# Find language code by name (reverse lookup)
def find_language_code(language_name):
for code, name in SOURCE_LANGUAGES.items():
if name == language_name:
return code
return None
code = find_language_code('英语')
print(code) # 'en'Contains all supported target languages (excludes "auto" since you cannot translate TO auto-detection).
TARGET_LANGUAGES: dict[str, str] = {
"ab": "阿布哈兹语",
"sq": "阿尔巴尼亚语",
"aa": "阿法尔语",
"ar": "阿拉伯语",
"am": "阿姆哈拉语",
"en": "英语",
"zh-CN": "中文(简体)",
"zh-TW": "中文(繁体)",
"fr": "法语",
"es": "西班牙语",
"de": "德语",
"ja": "日语",
"ko": "韩语",
"pt": "葡萄牙语(巴西)",
"pt-PT": "葡萄牙语(葡萄牙)",
"ru": "俄语",
"it": "意大利语",
"hi": "印地语",
"th": "泰语",
"vi": "越南语",
"tr": "土耳其语",
# ... and 222 more languages
}Usage Examples:
from pygtrans import TARGET_LANGUAGES
# Check if a language is supported as target
if 'zh-CN' in TARGET_LANGUAGES:
print(f"Simplified Chinese: {TARGET_LANGUAGES['zh-CN']}") # '中文(简体)'
# List all Chinese language variants
chinese_variants = {code: name for code, name in TARGET_LANGUAGES.items()
if '中文' in name}
for code, name in chinese_variants.items():
print(f"{code}: {name}")
# zh-CN: 中文(简体)
# zh-TW: 中文(繁体)
# Validate target language before translation
def is_valid_target(lang_code):
return lang_code in TARGET_LANGUAGES
if is_valid_target('fr'):
print("French is a valid target language")Helper functions and patterns for working with language codes and names.
Common Language Validation Patterns:
from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGES, Translate
def validate_language_pair(source, target):
"""Validate source and target language codes."""
if source != 'auto' and source not in SOURCE_LANGUAGES:
return False, f"Invalid source language: {source}"
if target not in TARGET_LANGUAGES:
return False, f"Invalid target language: {target}"
return True, "Valid language pair"
# Usage
valid, message = validate_language_pair('en', 'zh-CN')
if valid:
client = Translate()
result = client.translate('Hello', source='en', target='zh-CN')
else:
print(f"Error: {message}")Language Code Normalization:
def normalize_language_code(code):
"""Normalize language code to match pygtrans format."""
code = code.lower().strip()
# Common mappings
mappings = {
'chinese': 'zh-CN',
'chinese-simplified': 'zh-CN',
'chinese-traditional': 'zh-TW',
'english': 'en',
'french': 'fr',
'spanish': 'es',
'german': 'de',
'japanese': 'ja',
'korean': 'ko'
}
return mappings.get(code, code)
# Usage
normalized = normalize_language_code('Chinese')
print(normalized) # 'zh-CN'Working with automatic language detection and the "auto" source language option.
Usage Examples:
from pygtrans import Translate, SOURCE_LANGUAGES
client = Translate(proxies={'https': 'http://localhost:10809'})
# Using auto-detection (default behavior)
result = client.translate('Hello, world!', target='zh-CN')
print(f"Detected: {result.detectedSourceLanguage}") # 'en'
# Verify auto is available in source languages
if 'auto' in SOURCE_LANGUAGES:
print(f"Auto-detection: {SOURCE_LANGUAGES['auto']}") # '检测语言'
# Function to get language name from code
def get_language_name(code, is_source=True):
"""Get language name from code."""
languages = SOURCE_LANGUAGES if is_source else TARGET_LANGUAGES
return languages.get(code, f"Unknown language: {code}")
# Usage
source_name = get_language_name('en', is_source=True)
target_name = get_language_name('zh-CN', is_source=False)
print(f"Translating from {source_name} to {target_name}")
# Translating from 英语 to 中文(简体)Language Set Analysis:
from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGES
# Compare source and target language support
source_codes = set(SOURCE_LANGUAGES.keys())
target_codes = set(TARGET_LANGUAGES.keys())
print(f"Source languages: {len(source_codes)}") # 242
print(f"Target languages: {len(target_codes)}") # 243
# Languages only available as source (should be just "auto")
source_only = source_codes - target_codes
print(f"Source-only languages: {source_only}") # {'auto'}
# Languages only available as target (should be none for pygtrans)
target_only = target_codes - source_codes
print(f"Target-only languages: {target_only}") # set() (empty)
# Common languages (available for both source and target)
common_languages = source_codes & target_codes
print(f"Bidirectional languages: {len(common_languages)}") # 242Popular Language Codes:
# Common language codes for reference
POPULAR_LANGUAGES = {
'en': 'English',
'zh-CN': 'Chinese (Simplified)',
'zh-TW': 'Chinese (Traditional)',
'es': 'Spanish',
'fr': 'French',
'de': 'German',
'ja': 'Japanese',
'ko': 'Korean',
'pt': 'Portuguese (Brazil)',
'pt-PT': 'Portuguese (Portugal)',
'ru': 'Russian',
'it': 'Italian',
'ar': 'Arabic',
'hi': 'Hindi',
'th': 'Thai',
'vi': 'Vietnamese',
'tr': 'Turkish'
}
# Verify all popular languages are supported
for code in POPULAR_LANGUAGES:
if code in TARGET_LANGUAGES:
print(f"✓ {code} ({POPULAR_LANGUAGES[code]}) is supported")
else:
print(f"✗ {code} ({POPULAR_LANGUAGES[code]}) is NOT supported")pygtrans uses standard language codes with some specific formats:
en, fr, es, de, ja, kozh-CN (Simplified Chinese), zh-TW (Traditional Chinese)pt (Portuguese Brazil), pt-PT (Portuguese Portugal)auto (automatic detection, source only)Key Differences:
# Chinese language variants
'zh-CN' # Simplified Chinese (mainland China)
'zh-TW' # Traditional Chinese (Taiwan)
'yue' # Cantonese
# Portuguese variants
'pt' # Portuguese (Brazil)
'pt-PT' # Portuguese (Portugal)
# Arabic variants
'ar' # Standard Arabic
'fa' # Persian/Farsi
'fa-AF' # Dari (Afghanistan)Install with Tessl CLI
npx tessl i tessl/pypi-pygtrans