Python SDK
الترجمة بالذكاء الاصطناعي باستخدام Python وLingo.dev
مقدمة
يترجم Lingo.dev Python SDK النصوص وكائنات JSON ومحادثات الدردشة من خلال واجهة برمجة تطبيقات غير متزامنة. يتعامل مع الحمولات الكبيرة عن طريق تقسيم المحتوى تلقائيًا إلى أحجام دفعات مثالية، ويتتبع التقدم للترجمات طويلة المدى، ويدعم المسارد للحفاظ على اتساق المصطلحات.
التثبيت
pip
pip install lingodotdev
uv
uv add lingodotdev
ترجمة سريعة
تجاوز مدير السياق للترجمات لمرة واحدة. تتعامل هذه الطرق مع دورة حياة المحرك تلقائيًا وتستخدم الوضع السريع افتراضيًا، مما يجعلها مثالية لأدوات سطر الأوامر والبرامج النصية.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev.engine import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
# Translate text
text_result = await LingoDotDevEngine.quick_translate(
"Hello world",
api_key=api_key,
source_locale="en",
target_locale="es"
)
print(f"Text: {text_result}")
# Translate object
object_result = await LingoDotDevEngine.quick_translate(
{"greeting": "Hello", "farewell": "Goodbye"},
api_key=api_key,
source_locale="en",
target_locale="fr"
)
print(f"Object: {object_result}")
asyncio.run(main())
الاستخدام الأساسي
يتطلب SDK مفتاح API من Lingo.dev ويستخدم مديري السياق غير المتزامنين.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_text(
"Welcome! We missed you.",
{"source_locale": "en", "target_locale": "es"}
)
print(result)
asyncio.run(main())
ترجمة النص مع التقدم
تتبع تقدم الترجمة باستخدام دالة رد الاتصال. حتى النصوص المفردة تمر عبر المقسم، مما يتيح الإبلاغ عن التقدم للنصوص الطويلة ويوفر سلوكًا متسقًا عبر جميع أنواع المحتوى.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
def on_progress(percent: int):
print(f"Progress: {percent}%")
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_text(
"We sent a confirmation email.",
{"source_locale": "en", "target_locale": "es"},
progress_callback=on_progress
)
print(f"Result: {result}")
asyncio.run(main())
ترجمة الكائنات
ترجمة القواميس المتداخلة مع الحفاظ على البنية. يعالج SDK بشكل تكراري جميع قيم النصوص بغض النظر عن العمق.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
content = {
"title": "Welcome",
"cta": "Start your free trial",
"footer": {
"legal": "All rights reserved.",
"help": "Need help?"
}
}
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_object(
content,
{"source_locale": "en", "target_locale": "es"}
)
print(result)
asyncio.run(main())
الترجمة الدفعية إلى لغات متعددة
ترجمة المحتوى إلى لغات مستهدفة متعددة في استدعاء واحد. يُرجع قائمة بنتيجة واحدة لكل لغة مستهدفة، بنفس الترتيب المطلوب.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
# Translate text to multiple languages
text_results = await engine.batch_localize_text(
"Welcome to our application",
{
"source_locale": "en",
"target_locales": ["es", "fr", "de"],
"fast": True
}
)
print(f"Text results: {text_results}")
asyncio.run(main())
ترجمة الكائنات الدفعية
ترجم عدة كائنات في نفس الوقت ضمن مكالمة واحدة. تتم معالجة كل كائن بشكل مستقل، ما يجعل هذا الخيار مثالياً لتعريب قوائم السجلات أو كتالوجات المنتجات أو أي مجموعة من المحتوى المنظم.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
objects = [
{"title": "Getting Started", "description": "Learn the basics"},
{"title": "Advanced Usage", "description": "Deep dive into features"},
{"title": "API Reference", "description": "Complete method listing"},
]
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
results = await engine.batch_localize_objects(
objects,
{"source_locale": "en", "target_locale": "es"}
)
for result in results:
print(result)
asyncio.run(main())
الترجمة الدفعية للكائنات بطريقة سريعة
تتعامل طريقة الدفعة السريعة مع النصوص والكائنات معاً.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev.engine import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
results = await LingoDotDevEngine.quick_batch_translate(
{"greeting": "Hello", "bye": "Goodbye"},
api_key=api_key,
target_locales=["es", "fr", "de"],
source_locale="en",
fast=True,
)
for locale, result in zip(["es", "fr", "de"], results):
print(f"{locale}: {result}")
asyncio.run(main())
ترجمة المحادثات
ترجم رسائل الدردشة مع الحفاظ على أسماء المتحدثين. يجب أن تحتوي كل رسالة على حقلي "name" و "text". مناسب لتعريب محادثات الدعم أو سجلات الدردشة أو أنظمة الحوار دون فقدان هيكل المحادثة.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
chat = [
{"name": "Alice", "text": "Hello everyone!"},
{"name": "Bob", "text": "How are you doing?"},
{"name": "Alice", "text": "Great, thanks for asking!"},
]
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_chat(
chat,
{"source_locale": "en", "target_locale": "es"}
)
for message in result:
print(f"{message['name']}: {message['text']}")
asyncio.run(main())
المعالجة التسلسلية مع تتبع التقدم
يتيح الوضع التسلسلي إمكانية الحصول على ردود تقدم مفصلة. يتلقى رد الاستدعاء جزء النص الأصلي والجزء المترجم لكل دفعة معالجة، ما يساعد في تتبع أخطاء الترجمة أو عرض تقدم مفصل في الواجهات.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
def progress(percent: int, src_chunk: dict, out_chunk: dict):
print(f"{percent}% complete - processed {len(src_chunk)} keys")
async def main():
content = {
"welcome": "Hello",
"goodbye": "Farewell",
"help": "Need assistance?"
}
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_object(
content,
{"source_locale": "en", "target_locale": "es"},
progress_callback=progress
)
print(result)
asyncio.run(main())
المعالجة المتزامنة
عالج الأجزاء بالتوازي للحصول على ترجمة أسرع. هذا الوضع يضحي بتتبع التقدم لصالح السرعة، مما يجعله مثالياً عند الحاجة للأداء العالي في بيئات الإنتاج.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
content = {
"header": {"title": "Welcome", "subtitle": "Get started"},
"body": {"intro": "Learn more", "details": "Full description"},
"footer": {"copyright": "2024", "contact": "Email us"}
}
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_object(
content,
{"source_locale": "en", "target_locale": "es"},
concurrent=True
)
print(result)
asyncio.run(main())
بيانات مرجعية
قدّم القواميس أو الترجمات السابقة لضمان الاتساق. تُرسل البيانات المرجعية مع كل جزء إلى واجهة البرمجة، لذا يُفضل إبقاؤها بحجم مناسب. استخدمها للمصطلحات التجارية أو التقنية أو للحفاظ على ثبات الترجمة عبر تحديثات المنتجات.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
content = {"cta": "Start your free trial", "title": "Welcome"}
reference = {
"es": {"cta": "Comienza tu prueba gratuita"},
"fr": {"cta": "Commencez votre essai gratuit"},
}
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_object(
content,
{
"source_locale": "en",
"target_locale": "es",
"reference": reference,
},
concurrent=True
)
print(result)
asyncio.run(main())
الإعدادات
تحكّم في سلوك التجميع ونقاط نهاية واجهة البرمجة. يقوم SDK بتقسيم المحتوى الكبير حسب عدد المفاتيح والكلمات، ما يمنع انقطاع الاتصال ويحسن الاعتمادية في مشاريع الترجمة الضخمة.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
config = {
"api_key": os.environ["LINGODOTDEV_API_KEY"],
"api_url": "https://engine.lingo.dev",
"batch_size": 25, # Items per chunk (1-250)
"ideal_batch_item_size": 250, # Words per chunk (1-2500)
}
async with LingoDotDevEngine(config) as engine:
result = await engine.localize_text(
"Reset your password",
{"source_locale": "en", "target_locale": "es", "fast": True}
)
print(result)
asyncio.run(main())
اكتشاف اللغة
تعرّف على لغة النص تلقائياً. مفيد لتوجيه المحتوى تلقائياً إلى خط الترجمة الصحيح أو للتحقق من لغة إدخال المستخدم.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
locale = await engine.recognize_locale("Guten Morgen")
print(f"Detected language: {locale}")
asyncio.run(main())
التحقق من مفتاح API
تحقَّق من الحساب الذي يملك مفتاح API. يُرجِع None إذا فشلت المصادقة. مفيد لتصحيح مشاكل المصادقة أو لعرض معلومات الحساب في أدوات الإدارة.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
user = await engine.whoami()
if user:
print(f"Authenticated as: {user}")
else:
print("Authentication failed")
asyncio.run(main())
معالجة الأخطاء
تقوم حزمة SDK بإظهار خطأ من نوع ValueError عند وجود مشاكل في المتغيرات، وخطأ RuntimeError عند حدوث مشاكل في الشبكة أو الخادم. عليك التفريق بين أخطاء المستخدم (ValueError) ومشاكل البنية التحتية (RuntimeError) لوضع منطق تكرار مناسب.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lingodotdev==1.3.0",
# ]
# ///
import asyncio
import os
from lingodotdev import LingoDotDevEngine
async def main():
try:
api_key = os.environ["LINGODOTDEV_API_KEY"]
async with LingoDotDevEngine({"api_key": api_key}) as engine:
result = await engine.localize_text(
"Hello",
{"target_locale": "es"}
)
print(result)
except ValueError as e:
print(f"Invalid parameters or bad request: {e}")
except RuntimeError as e:
print(f"Server or network error: {e}")
asyncio.run(main())
مرجع API
المُنشئ
| المعامل | النوع | الافتراضي | الوصف |
|---|---|---|---|
api_key | str | مطلوب | مفتاح Lingo.dev API |
api_url | str | "https://engine.lingo.dev" | عنوان نقطة نهاية API |
batch_size | int | 25 | العناصر في كل جزء (1-250) |
ideal_batch_item_size | int | 250 | عدد الكلمات في كل جزء (1-2500) |
طرق الكائن
| الطريقة | المعاملات | الإرجاع | الوصف |
|---|---|---|---|
localize_text(text, params, progress_callback?) | text: str, params: dict | str | ترجمة نص فردي |
localize_object(obj, params, progress_callback?, concurrent?) | obj: dict, params: dict | dict | ترجمة قاموس مع الحفاظ على البنية |
localize_chat(chat, params, progress_callback?) | chat: list[dict], params: dict | list[dict] | ترجمة رسائل الدردشة مع الحفاظ على أسماء المتحدثين |
batch_localize_text(text, params) | text: str, params: dict مع target_locales | list[str] | ترجمة نص لعدة لغات مستهدفة دفعة واحدة |
batch_localize_objects(objects, params) | objects: list[dict], params: dict | list[dict] | ترجمة عدة كائنات بالتوازي |
recognize_locale(text) | text: str | str | الكشف عن لغة نص معين |
whoami() | — | dict | None | الحصول على معلومات الحساب لمفتاح API الحالي |
طرق الصف
| الطريقة | المعاملات | النتيجة | الوصف |
|---|---|---|---|
quick_translate(content, api_key, target_locale, source_locale?, api_url?, fast?) | content: str | dict | str | dict | ترجمة لمرة واحدة من دون مدير للسياق |
quick_batch_translate(content, api_key, target_locales, source_locale?, api_url?, fast?) | content: str | dict | list | ترجمة دفعة واحدة لعدة لغات |
معلمات التوطين
| المفتاح | النوع | مطلوب | الوصف |
|---|---|---|---|
source_locale | str | لا | رمز لغة المصدر (يُكتشف تلقائيًا إذا تُرك فارغًا) |
target_locale | str | نعم | رمز اللغة المستهدفة |
target_locales | list[str] | للدفعات | قائمة برموز اللغات المستهدفة |
fast | bool | لا | تفعيل الوضع السريع لتسريع الترجمة |
reference | dict | لا | مصطلحات أو ترجمات مرجعية لتحقيق التناسق |