Skip to content

Commit 86d2dcc

Browse files
authored
Feature: added examples folder
1 parent 68a62dc commit 86d2dcc

5 files changed

Lines changed: 927 additions & 2 deletions

File tree

README.md

Lines changed: 390 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,395 @@
1-
# Lara Python Library
1+
# Lara Python SDK
2+
3+
[![Python Version](https://img.shields.io/badge/python-3.8+-blue.svg)](https://python.org)
4+
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
25

36
This SDK empowers you to build your own branded translation AI leveraging our translation fine-tuned language model.
47

58
All major translation features are accessible, making it easy to integrate and customize for your needs.
69

7-
Lara’s SDK full documentation is available at https://developers.laratranslate.com/
10+
## 🌍 **Features:**
11+
- **Text Translation**: Single strings, multiple strings, and complex text blocks
12+
- **Document Translation**: Word, PDF, and other document formats with status monitoring
13+
- **Translation Memory**: Store and reuse translations for consistency
14+
- **Glossaries**: Enforce terminology standards across translations
15+
- **Language Detection**: Automatic source language identification
16+
- **Advanced Options**: Translation instructions and more
17+
18+
## 📚 Documentation
19+
20+
Lara's SDK full documentation is available at [https://developers.laratranslate.com/](https://developers.laratranslate.com/)
21+
22+
## 🚀 Quick Start
23+
24+
### Installation
25+
26+
```bash
27+
pip install lara-sdk
28+
```
29+
30+
### Basic Usage
31+
32+
```python
33+
import os
34+
from lara_sdk import Credentials, Translator
35+
36+
# Set your credentials using environment variables (recommended)
37+
credentials = Credentials(
38+
os.environ.get('LARA_ACCESS_KEY_ID'),
39+
os.environ.get('LARA_ACCESS_KEY_SECRET')
40+
)
41+
42+
# Create translator instance
43+
lara = Translator(credentials)
44+
45+
# Simple text translation
46+
try:
47+
result = lara.translate("Hello, world!", target="fr-FR", source="en-US")
48+
print(f"Translation: {result.translation}")
49+
# Output: Translation: Bonjour, le monde !
50+
except Exception as error:
51+
print(f"Translation error: {error}")
52+
```
53+
54+
## 📖 Examples
55+
56+
The `examples/` directory contains comprehensive examples for all SDK features.
57+
58+
**All examples use environment variables for credentials, so set them first:**
59+
```bash
60+
export LARA_ACCESS_KEY_ID="your-access-key-id"
61+
export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
62+
```
63+
64+
### Text Translation
65+
- **[text_translation.py](examples/text_translation.py)** - Complete text translation examples
66+
- Single string translation
67+
- Multiple strings translation
68+
- Translation with instructions
69+
- TextBlocks translation (mixed translatable/non-translatable content)
70+
- Auto-detect source language
71+
- Advanced translation options
72+
- Get available languages
73+
74+
```bash
75+
cd examples
76+
python text_translation.py
77+
```
78+
79+
### Document Translation
80+
- **[document_translation.py](examples/document_translation.py)** - Document translation examples
81+
- Basic document translation
82+
- Advanced options with memories and glossaries
83+
- Step-by-step translation with status monitoring
84+
85+
```bash
86+
cd examples
87+
python document_translation.py
88+
```
89+
90+
### Translation Memory Management
91+
- **[memories_management.py](examples/memories_management.py)** - Memory management examples
92+
- Create, list, update, delete memories
93+
- Add individual translations
94+
- Multiple memory operations
95+
- TMX file import with progress monitoring
96+
- Translation deletion
97+
- Translation with TUID and context
98+
99+
```bash
100+
cd examples
101+
python memories_management.py
102+
```
103+
104+
### Glossary Management
105+
- **[glossaries_management.py](examples/glossaries_management.py)** - Glossary management examples
106+
- Create, list, update, delete glossaries
107+
- CSV import with status monitoring
108+
- Glossary export
109+
- Glossary terms count
110+
- Import status checking
111+
112+
```bash
113+
cd examples
114+
python glossaries_management.py
115+
```
116+
117+
## 🔧 API Reference
118+
119+
### Core Components
120+
121+
### 🔐 Authentication
122+
123+
The SDK supports authentication via access key and secret:
124+
125+
```python
126+
from lara_sdk import Credentials, Translator
127+
128+
credentials = Credentials("your-access-key-id", "your-access-key-secret")
129+
lara = Translator(credentials)
130+
```
131+
132+
**Environment Variables (Recommended):**
133+
```bash
134+
export LARA_ACCESS_KEY_ID="your-access-key-id"
135+
export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
136+
```
137+
138+
```python
139+
import os
140+
from lara_sdk import Credentials
141+
142+
credentials = Credentials(
143+
os.environ['LARA_ACCESS_KEY_ID'],
144+
os.environ['LARA_ACCESS_KEY_SECRET']
145+
)
146+
```
147+
148+
**Alternative Constructor:**
149+
```python
150+
# You can also pass credentials directly to Translator
151+
lara = Translator(
152+
access_key_id="your-access-key-id",
153+
access_key_secret="your-access-key-secret"
154+
)
155+
```
156+
157+
158+
### 🌍 Translator
159+
160+
```python
161+
# Create translator with credentials
162+
lara = Translator(credentials)
163+
```
164+
165+
#### Text Translation
166+
167+
```python
168+
# Basic translation
169+
result = lara.translate("Hello", target="fr-FR", source="en-US")
170+
171+
# Multiple strings
172+
result = lara.translate(["Hello", "World"], target="fr-FR", source="en-US")
173+
174+
# TextBlocks (mixed translatable/non-translatable content)
175+
from lara_sdk import TextBlock
176+
177+
text_blocks = [
178+
TextBlock(text="Translatable text", translatable=True),
179+
TextBlock(text="<br>", translatable=False), # Non-translatable HTML
180+
TextBlock(text="More translatable text", translatable=True)
181+
]
182+
result = lara.translate(text_blocks, target="fr-FR", source="en-US")
183+
184+
# With advanced options
185+
186+
result = lara.translate(
187+
"Hello",
188+
target="fr-FR",
189+
source="en-US",
190+
instructions=["Formal tone"],
191+
adapt_to=["memory-id"], # Replace with actual memory IDs
192+
glossaries=["glossary-id"], # Replace with actual glossary IDs
193+
style="fluid",
194+
timeout_ms=10000
195+
)
196+
```
197+
198+
### 📖 Document Translation
199+
#### Simple document translation
200+
```python
201+
translated_content = lara.documents.translate(
202+
file_path="/path/to/your/document.txt", # Replace with actual file path
203+
filename="document.txt",
204+
source="en-US",
205+
target="fr-FR"
206+
)
207+
208+
# With options
209+
translated_content = lara.documents.translate(
210+
file_path="/path/to/your/document.txt", # Replace with actual file path
211+
filename="document.txt",
212+
source="en-US",
213+
target="fr-FR",
214+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
215+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual glossary IDs
216+
style="fluid"
217+
)
218+
```
219+
### Document translation with status monitoring
220+
#### Document upload
221+
```python
222+
#Optional: upload options
223+
document = lara.documents.upload(
224+
file_path="/path/to/your/document.txt", # Replace with actual file path
225+
filename="document.txt",
226+
source="en-US",
227+
target="fr-FR",
228+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
229+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"] # Replace with actual glossary IDs
230+
)
231+
```
232+
#### Document translation status monitoring
233+
```python
234+
status = lara.documents.status(document.id)
235+
```
236+
#### Download translated document
237+
```python
238+
translated_content = lara.documents.download(document.id)
239+
```
240+
241+
### 🧠 Memory Management
242+
243+
```python
244+
# Create memory
245+
memory = lara.memories.create("MyMemory")
246+
247+
# Create memory with external ID (MyMemory integration)
248+
memory = lara.memories.create("Memory from MyMemory", external_id="aabb1122") # Replace with actual external ID
249+
250+
# Important: To update/overwrite a translation unit you must provide a tuid. Calls without a tuid always create a new unit and will not update existing entries.
251+
# Add translation to single memory
252+
memory_import = lara.memories.add_translation("mem_1A2b3C4d5E6f7G8h9I0jKl", "en-US", "fr-FR", "Hello", "Bonjour", tuid="greeting_001")
253+
254+
# Add translation to multiple memories
255+
memory_import = lara.memories.add_translation(["mem_1A2b3C4d5E6f7G8h9I0jKl", "mem_2XyZ9AbC8dEf7GhI6jKlMn"], "en-US", "fr-FR", "Hello", "Bonjour", tuid="greeting_002")
256+
257+
# Add with context
258+
memory_import = lara.memories.add_translation(
259+
"mem_1A2b3C4d5E6f7G8h9I0jKl", "en-US", "fr-FR", "Hello", "Bonjour",
260+
tuid="tuid", sentence_before="sentenceBefore", sentence_after="sentenceAfter"
261+
)
262+
263+
# TMX import from file
264+
memory_import = lara.memories.import_tmx("mem_1A2b3C4d5E6f7G8h9I0jKl", "/path/to/your/memory.tmx") # Replace with actual TMX file path
265+
266+
# Delete translation
267+
# Important: if you omit tuid, all entries that match the provided fields will be removed
268+
delete_job = lara.memories.delete_translation(
269+
"mem_1A2b3C4d5E6f7G8h9I0jKl", "en-US", "fr-FR", "Hello", "Bonjour", tuid="greeting_001"
270+
)
271+
272+
# Wait for import completion
273+
completed_import = lara.memories.wait_for_import(memory_import, max_wait_time=300) # 5 minutes
274+
```
275+
276+
### 📚 Glossary Management
277+
278+
```python
279+
# Create glossary
280+
glossary = lara.glossaries.create("MyGlossary")
281+
282+
# Import CSV from file
283+
glossary_import = lara.glossaries.import_csv("gls_1A2b3C4d5E6f7G8h9I0jKl", "/path/to/your/glossary.csv") # Replace with actual CSV file path
284+
285+
# Check import status
286+
import_status = lara.glossaries.get_import_status(import_id)
287+
288+
# Wait for import completion
289+
completed_import = lara.glossaries.wait_for_import(glossary_import, max_wait_time=300) # 5 minutes
290+
291+
# Export glossary
292+
csv_data = lara.glossaries.export("gls_1A2b3C4d5E6f7G8h9I0jKl", "csv/table-uni", "en-US")
293+
294+
# Get glossary terms count
295+
counts = lara.glossaries.counts("gls_1A2b3C4d5E6f7G8h9I0jKl")
296+
```
297+
298+
### Translation Options
299+
300+
```python
301+
result = lara.translate(
302+
text,
303+
target="fr-FR", # Target language (required)
304+
source="en-US", # Source language (optional, auto-detect if None)
305+
source_hint="en", # Hint for source language detection
306+
adapt_to=["memory-id"], # Memory IDs to adapt to
307+
glossaries=["glossary-id"], # Glossary IDs to use
308+
instructions=["instruction"], # Translation instructions
309+
style="fluid", # Translation style (fluid, faithful, creative)
310+
content_type="text/plain", # Content type (text/plain, text/html, etc.)
311+
multiline=True, # Enable multiline translation
312+
timeout_ms=10000, # Request timeout in milliseconds
313+
no_trace=False, # Disable request tracing
314+
verbose=False, # Enable verbose response
315+
)
316+
```
317+
318+
### Language Codes
319+
320+
The SDK supports full language codes (e.g., `en-US`, `fr-FR`, `es-ES`) as well as simple codes (e.g., `en`, `fr`, `es`):
321+
322+
```python
323+
# Full language codes (recommended)
324+
result = lara.translate("Hello", target="fr-FR", source="en-US")
325+
326+
# Simple language codes
327+
result = lara.translate("Hello", target="fr", source="en")
328+
```
329+
330+
### 🌐 Supported Languages
331+
332+
The SDK supports all languages available in the Lara API. Use the `languages()` method to get the current list:
333+
334+
```python
335+
languages = lara.languages()
336+
print(f"Supported languages: {', '.join(languages)}")
337+
```
338+
339+
## ⚙️ Configuration
340+
### Error Handling
341+
342+
The SDK provides detailed error information:
343+
344+
```python
345+
from lara_sdk import LaraApiError, LaraError
346+
347+
try:
348+
result = lara.translate("Hello", target="fr-FR", source="en-US")
349+
print(f"Translation: {result.translation}")
350+
except LaraApiError as error:
351+
print(f"API Error [{error.status_code}]: {error.message}")
352+
print(f"Error type: {error.type}")
353+
except LaraError as error:
354+
print(f"SDK Error: {error}")
355+
except Exception as error:
356+
print(f"Unexpected error: {error}")
357+
```
358+
359+
## 📋 Requirements
360+
361+
- Python 3.8 or higher
362+
- pip
363+
- Valid Lara API credentials
364+
365+
## 🧪 Testing
366+
367+
Run the examples to test your setup.
368+
369+
```bash
370+
# All examples use environment variables for credentials, so set them first:
371+
export LARA_ACCESS_KEY_ID="your-access-key-id"
372+
export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
373+
```
374+
```bash
375+
# Run basic text translation example
376+
cd examples
377+
python text_translation.py
378+
```
379+
380+
## 🏗️ Building from Source
381+
382+
```bash
383+
# Clone the repository
384+
git clone https://github.com/translated/lara-python.git
385+
cd lara-python
386+
387+
# Install in development mode
388+
pip install -e .
389+
```
390+
391+
## 📄 License
392+
393+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
394+
395+
Happy translating! 🌍✨

0 commit comments

Comments
 (0)