Library Usage
此内容尚不支持你的语言。
Simple text translation
Section titled “Simple text translation”from pypolyglot import translate
result = await translate("Hello world", "en", "ja")print(result.translation) # こんにちは世界The translate function handles chunking, batching, and retry automatically. For long text, it splits into segments, translates each, and reassembles the result.
Markdown translation
Section titled “Markdown translation”from pypolyglot import translate_markdown
md = """## Features
Local GPU translation with **zero cloud dependency**.
```pythonresult = await translate("Hello", "en", "ja")"""
result = await translate_markdown(md, “en”, “fr”) print(result.markdown)
Markdown translation preserves:- Code blocks (fenced and inline)- Tables- HTML tags and attributes- URLs, badges, and image references- Heading structure
## Multi-language translation
Translate into 7 languages at once:
```pythonfrom pypolyglot import translate_all
result = await translate_all(md, source_lang="en")for r in result.results: print(f"{r.name}: {r.status} ({r.duration_ms:.0f}ms)")Default languages: Japanese, Chinese (Simplified), Spanish, French, Hindi, Italian, Portuguese (BR).
Target specific languages:
result = await translate_all(md, target_langs=["ja", "es", "fr"])Translation options
Section titled “Translation options”Custom model
Section titled “Custom model”from pypolyglot import translate, TranslateOptions
result = await translate("Hello", "en", "ja", TranslateOptions(model="translategemma:4b"))Custom glossary
Section titled “Custom glossary”Override translations for specific terms:
from pypolyglot import translate, TranslateOptions, GlossaryEntry
result = await translate("Deploy the Widget", "en", "ja", TranslateOptions(glossary=[ GlossaryEntry("Widget", {"ja": "ウィジェット"}) ]))polyglot-gpu includes a built-in software glossary with 12 technical terms (Architecture, Pipeline, Deploy, Library, Framework, Build, Release, Branch, Repository, Merge, Token, Adoption). These are injected automatically when the terms appear in the source text.
Streaming
Section titled “Streaming”result = await translate("Hello world", "en", "ja", TranslateOptions(on_token=lambda t: print(t, end="")))Caching
Section titled “Caching”Segment-level caching is enabled by default. Cache uses Levenshtein fuzzy matching — if a segment is similar enough to a previously translated one, the cached translation is reused.
from pypolyglot import translate_markdown, TranslateMarkdownOptions
# Disable cachingresult = await translate_markdown(md, "en", "ja", TranslateMarkdownOptions(cache=False))Cache files are stored as .polyglot-cache.json in the working directory.