or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

api-client.mdfree-client.mdindex.mdlanguages.mdresponses.md

languages.mddocs/

0

# Language Support

1

2

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.

3

4

## Capabilities

5

6

### Source Languages Dictionary

7

8

Contains all supported source languages including the special "auto" option for automatic language detection.

9

10

```python { .api }

11

SOURCE_LANGUAGES: dict[str, str] = {

12

"auto": "检测语言",

13

"ab": "阿布哈兹语",

14

"sq": "阿尔巴尼亚语",

15

"aa": "阿法尔语",

16

"ar": "阿拉伯语",

17

"am": "阿姆哈拉语",

18

"en": "英语",

19

"zh-CN": "中文",

20

"fr": "法语",

21

"es": "西班牙语",

22

"de": "德语",

23

"ja": "日语",

24

"ko": "韩语",

25

"pt": "葡萄牙语(巴西)",

26

"pt-PT": "葡萄牙语(葡萄牙)",

27

"ru": "俄语",

28

"it": "意大利语",

29

"hi": "印地语",

30

"th": "泰语",

31

"vi": "越南语",

32

"tr": "土耳其语",

33

# ... and 222 more languages

34

}

35

```

36

37

**Usage Examples:**

38

39

```python

40

from pygtrans import SOURCE_LANGUAGES

41

42

# Check if a language is supported as source

43

if 'en' in SOURCE_LANGUAGES:

44

print(f"English is supported: {SOURCE_LANGUAGES['en']}") # '英语'

45

46

# List all supported source languages

47

print(f"Total source languages: {len(SOURCE_LANGUAGES)}") # 242

48

49

# Display first 10 source languages

50

for i, (code, name) in enumerate(SOURCE_LANGUAGES.items()):

51

if i < 10:

52

print(f"{code}: {name}")

53

# auto: 检测语言

54

# ab: 阿布哈兹语

55

# sq: 阿尔巴尼亚语

56

# ...

57

58

# Find language code by name (reverse lookup)

59

def find_language_code(language_name):

60

for code, name in SOURCE_LANGUAGES.items():

61

if name == language_name:

62

return code

63

return None

64

65

code = find_language_code('英语')

66

print(code) # 'en'

67

```

68

69

### Target Languages Dictionary

70

71

Contains all supported target languages (excludes "auto" since you cannot translate TO auto-detection).

72

73

```python { .api }

74

TARGET_LANGUAGES: dict[str, str] = {

75

"ab": "阿布哈兹语",

76

"sq": "阿尔巴尼亚语",

77

"aa": "阿法尔语",

78

"ar": "阿拉伯语",

79

"am": "阿姆哈拉语",

80

"en": "英语",

81

"zh-CN": "中文(简体)",

82

"zh-TW": "中文(繁体)",

83

"fr": "法语",

84

"es": "西班牙语",

85

"de": "德语",

86

"ja": "日语",

87

"ko": "韩语",

88

"pt": "葡萄牙语(巴西)",

89

"pt-PT": "葡萄牙语(葡萄牙)",

90

"ru": "俄语",

91

"it": "意大利语",

92

"hi": "印地语",

93

"th": "泰语",

94

"vi": "越南语",

95

"tr": "土耳其语",

96

# ... and 222 more languages

97

}

98

```

99

100

**Usage Examples:**

101

102

```python

103

from pygtrans import TARGET_LANGUAGES

104

105

# Check if a language is supported as target

106

if 'zh-CN' in TARGET_LANGUAGES:

107

print(f"Simplified Chinese: {TARGET_LANGUAGES['zh-CN']}") # '中文(简体)'

108

109

# List all Chinese language variants

110

chinese_variants = {code: name for code, name in TARGET_LANGUAGES.items()

111

if '中文' in name}

112

for code, name in chinese_variants.items():

113

print(f"{code}: {name}")

114

# zh-CN: 中文(简体)

115

# zh-TW: 中文(繁体)

116

117

# Validate target language before translation

118

def is_valid_target(lang_code):

119

return lang_code in TARGET_LANGUAGES

120

121

if is_valid_target('fr'):

122

print("French is a valid target language")

123

```

124

125

### Language Validation and Utilities

126

127

Helper functions and patterns for working with language codes and names.

128

129

**Common Language Validation Patterns:**

130

131

```python

132

from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGES, Translate

133

134

def validate_language_pair(source, target):

135

"""Validate source and target language codes."""

136

if source != 'auto' and source not in SOURCE_LANGUAGES:

137

return False, f"Invalid source language: {source}"

138

if target not in TARGET_LANGUAGES:

139

return False, f"Invalid target language: {target}"

140

return True, "Valid language pair"

141

142

# Usage

143

valid, message = validate_language_pair('en', 'zh-CN')

144

if valid:

145

client = Translate()

146

result = client.translate('Hello', source='en', target='zh-CN')

147

else:

148

print(f"Error: {message}")

149

```

150

151

**Language Code Normalization:**

152

153

```python

154

def normalize_language_code(code):

155

"""Normalize language code to match pygtrans format."""

156

code = code.lower().strip()

157

158

# Common mappings

159

mappings = {

160

'chinese': 'zh-CN',

161

'chinese-simplified': 'zh-CN',

162

'chinese-traditional': 'zh-TW',

163

'english': 'en',

164

'french': 'fr',

165

'spanish': 'es',

166

'german': 'de',

167

'japanese': 'ja',

168

'korean': 'ko'

169

}

170

171

return mappings.get(code, code)

172

173

# Usage

174

normalized = normalize_language_code('Chinese')

175

print(normalized) # 'zh-CN'

176

```

177

178

### Language Detection and Auto-Selection

179

180

Working with automatic language detection and the "auto" source language option.

181

182

**Usage Examples:**

183

184

```python

185

from pygtrans import Translate, SOURCE_LANGUAGES

186

187

client = Translate(proxies={'https': 'http://localhost:10809'})

188

189

# Using auto-detection (default behavior)

190

result = client.translate('Hello, world!', target='zh-CN')

191

print(f"Detected: {result.detectedSourceLanguage}") # 'en'

192

193

# Verify auto is available in source languages

194

if 'auto' in SOURCE_LANGUAGES:

195

print(f"Auto-detection: {SOURCE_LANGUAGES['auto']}") # '检测语言'

196

197

# Function to get language name from code

198

def get_language_name(code, is_source=True):

199

"""Get language name from code."""

200

languages = SOURCE_LANGUAGES if is_source else TARGET_LANGUAGES

201

return languages.get(code, f"Unknown language: {code}")

202

203

# Usage

204

source_name = get_language_name('en', is_source=True)

205

target_name = get_language_name('zh-CN', is_source=False)

206

print(f"Translating from {source_name} to {target_name}")

207

# Translating from 英语 to 中文(简体)

208

```

209

210

### Language Statistics and Information

211

212

**Language Set Analysis:**

213

214

```python

215

from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGES

216

217

# Compare source and target language support

218

source_codes = set(SOURCE_LANGUAGES.keys())

219

target_codes = set(TARGET_LANGUAGES.keys())

220

221

print(f"Source languages: {len(source_codes)}") # 242

222

print(f"Target languages: {len(target_codes)}") # 243

223

224

# Languages only available as source (should be just "auto")

225

source_only = source_codes - target_codes

226

print(f"Source-only languages: {source_only}") # {'auto'}

227

228

# Languages only available as target (should be none for pygtrans)

229

target_only = target_codes - source_codes

230

print(f"Target-only languages: {target_only}") # set() (empty)

231

232

# Common languages (available for both source and target)

233

common_languages = source_codes & target_codes

234

print(f"Bidirectional languages: {len(common_languages)}") # 242

235

```

236

237

**Popular Language Codes:**

238

239

```python

240

# Common language codes for reference

241

POPULAR_LANGUAGES = {

242

'en': 'English',

243

'zh-CN': 'Chinese (Simplified)',

244

'zh-TW': 'Chinese (Traditional)',

245

'es': 'Spanish',

246

'fr': 'French',

247

'de': 'German',

248

'ja': 'Japanese',

249

'ko': 'Korean',

250

'pt': 'Portuguese (Brazil)',

251

'pt-PT': 'Portuguese (Portugal)',

252

'ru': 'Russian',

253

'it': 'Italian',

254

'ar': 'Arabic',

255

'hi': 'Hindi',

256

'th': 'Thai',

257

'vi': 'Vietnamese',

258

'tr': 'Turkish'

259

}

260

261

# Verify all popular languages are supported

262

for code in POPULAR_LANGUAGES:

263

if code in TARGET_LANGUAGES:

264

print(f"✓ {code} ({POPULAR_LANGUAGES[code]}) is supported")

265

else:

266

print(f"✗ {code} ({POPULAR_LANGUAGES[code]}) is NOT supported")

267

```

268

269

## Language Code Format

270

271

pygtrans uses standard language codes with some specific formats:

272

273

- **ISO 639-1 codes**: `en`, `fr`, `es`, `de`, `ja`, `ko`

274

- **Locale-specific codes**: `zh-CN` (Simplified Chinese), `zh-TW` (Traditional Chinese)

275

- **Regional variants**: `pt` (Portuguese Brazil), `pt-PT` (Portuguese Portugal)

276

- **Special codes**: `auto` (automatic detection, source only)

277

- **Extended codes**: Various regional and minority languages with extended identifiers

278

279

**Key Differences:**

280

281

```python

282

# Chinese language variants

283

'zh-CN' # Simplified Chinese (mainland China)

284

'zh-TW' # Traditional Chinese (Taiwan)

285

'yue' # Cantonese

286

287

# Portuguese variants

288

'pt' # Portuguese (Brazil)

289

'pt-PT' # Portuguese (Portugal)

290

291

# Arabic variants

292

'ar' # Standard Arabic

293

'fa' # Persian/Farsi

294

'fa-AF' # Dari (Afghanistan)

295

```