Docs

Translate Text

Translator

The Translator class is the core component of the lara-sdk, designed for translating text. It must be initialized with authentication credentials.

from lara_sdk import Translator, Credentials

LARA_ACCESS_KEY_ID = "your-access-key-id"
LARA_ACCESS_KEY_SECRET = "your-access-key-secret"

# Initialization of the Translator class
credentials = Credentials(access_key_id=LARA_ACCESS_KEY_ID, access_key_secret=LARA_ACCESS_KEY_SECRET)
lara = Translator(credentials)
import {Credentials, Translator} from "@translated/lara";

const LARA_ACCESS_KEY_ID: string = "your-access-key-id";
const LARA_ACCESS_KEY_SECRET: string = "your-access-key-secret";

// Initialization of the Translator class
const credentials = new Credentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET)
const lara: Translator = new Translator(credentials);
import com.translated.lara.Credentials;
import com.translated.lara.translator.Translator;

private static final String LARA_ACCESS_KEY_ID = "your-access-key-id";
private static final String LARA_ACCESS_KEY_SECRET = "your-access-key-secret";

// Initialization of the Translator class
Credentials credentials = new Credentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET);
Translator lara = new Translator(credentials);
<?php

require 'vendor/autoload.php';

use Lara\Translator;
use Lara\LaraCredentials;

$LARA_ACCESS_KEY_ID = "your-access-key-id";
$LARA_ACCESS_KEY_SECRET = "your-access-key-secret";

// Initialization of the Translator class
$credentials = new LaraCredentials($LARA_ACCESS_KEY_ID, $LARA_ACCESS_KEY_SECRET);
$lara = new Translator($credentials);

Translate

Translates text from a source language to a target language. It supports single-sentence translations, context-aware translations and adaptation to specific translation memories.

Request

res = lara.translate('Hello, how are you?',
                     source='en-US',
                     target='it-IT',
                     adapt_to=['mem_1_id', 'mem_2_id'],
                     instructions=['Be formal'],
                     content_type='text/plain',
                     timeout_ms=2000,
                     priority=TranslatePriority.NORMAL,
                     use_cache=True,
                     cache_ttl_s=86400
                     )

print(res.translation)
const res  = await lara.translate('Hello, how are you?', 'en-US', 'it-IT', {
        adaptTo: ['mem_1_id', 'mem_2_id'],
        instructions: ['Be formal'],
        contentType: 'text/plain',
        timeoutInMillis: 2000,
        priority: 'normal',
        useCache: true,
        cacheTTLSeconds: 86400
});
// All options are optional and can be omitted
TranslateOptions options = new TranslateOptions();
options.setAdaptTo("mem_1_id", "mem_2_id");
options.setInstructions("Be formal");
options.setContentType("text/plain");
options.setTimeoutMs(2000);
options.setPriority(TranslateOptions.Priority.NORMAL);
options.setUseCache(TranslateOptions.UseCache.YES);
options.setCacheTTL(86400);

TextResult res = lara.translate("Hello, how are you?", "en-US", "it-IT", options);
$options = new TranslateOptions([
    'adaptTo' => array('mem_1_id', 'mem_2_id'),
    'instructions' => array('Be formal'),
    'contentType' => 'text/plain',
    'timeoutInMillis' => 2000,
    'priority' => 'normal',
    'useCache' => true,
    'cacheTTLSeconds' => 86400
]);

$res = $lara->translate('Hello, how are you?', 'en-US', 'it-IT', $options);

Here follows the basic fields for the translate method:

Field

Type     

Required     

Default     

Description

text        

String |
String[] |
TextBlock[]

Yes

-

The text to be translated. It can be a single string, a list of strings, or a list of TextBlock objects for incremental translation.

source

String

No

Autodetected

The source language code (e.g., "en-EN" for English). If not specified, the system will attempt to detect it automatically.

target

String

Yes

-

The target language code (e.g., "it-IT" for Italian). This specifies the language you want the text translated into.

Several options are available to customize the behavior of the translate method:

Field

Type

Required

Default

Description

source_hint

String

No

-

Used to guide language detection. Specify this when the source language is uncertain to improve detection accuracy.

adapt_to

String[]

No

Default is all Memories on your account

A list of translation memory IDs for adapting the translation.

instructions

String[]

No

[ ]

A list of instructions to adjust the network’s behavior regarding the output (e.g., "Use a formal tone").

content_type

String

No

Autodetected

Specifies the Content-Type of the text. Available values: text/plain text/xml text/html application/xliff+xml

timeout_ms

Integer

No

None

Specifies the maximum allowable time (in milliseconds) to perform the translation. If the timeout is exceeded, a TimeoutException is raised.

priority

String

No

Normal

Specifies whether the translation should be executed as a normal priority task or can wait for other tasks to complete.

use_cache

Boolean

No

False

Specifies whether the translation should be saved in the cache for future retrieval. Cache functionality must be enabled for the current user.

cache_ttl

Integer

No

2 Years

The time-to-live (TTL) for the cache entry, specifying how long the translation will remain in the cache.


Response

# Single string
TextResult(
    content_type="text/plain",
    source_language="en",
    adapted_to=["mem_1_id", "mem_2_id"],
    translation="Ciao, come stai?"
)

# String array
TextResult(
    content_type="text/plain",
    source_language="en",
    adapted_to=["mem_1_id", "mem_2_id"],
    translation=['Ciao, come stai?', 'Che giornata meravigliosa.']
)

# TextBlock array
TextResult(
    content_type="text/plain",
    source_language="en",
    adapted_to=["mem_1_id", "mem_2_id"],
    translation=[
        TextBlock(text="Ciao, come stai?", translatable=True),
        TextBlock(text="Che giornata meravigliosa.", translatable=True)
    ]
)
// Single string
{
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: 'Salve, come sta?',
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}

// String array
{
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: ['Salve, come sta?', 'Che bella giornata.'],
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}

// TextBlock array
{
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: [
      { text: 'Salve, come sta?', translatable: true },
      { text: 'Che bella giornata.', translatable: true }
    ],
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}
// Single string
TextResult(
    contentType="text/plain",
    sourceLanguage="en",
    adaptedTo=["mem_1_id", "mem_2_id"],
    translation="Ciao, come stai?"
)

// String array
TextResult(
    contentType="text/plain",
    sourceLanguage="en",
    adaptedTo=["mem_1_id", "mem_2_id"],
    translation=['Ciao, come stai?', 'Che giornata meravigliosa.']
)

// TextBlock array
TextResult(
    contentType="text/plain",
    sourceLanguage="en",
    adaptedTo=["mem_1_id", "mem_2_id"],
    translation=[
        TextBlock(text="Ciao, come stai?", translatable=true),
        TextBlock(text="Che giornata meravigliosa.", translatable=true)
    ]
)
// Single string
Lara\TextResult {
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: 'Salve, come sta?',
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}

// String array
Lara\TextResult {
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: ['Salve, come sta?', 'Che bella giornata.'],
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}

// TextBlock array
Lara\TextResult {
    contentType: 'text/plain',
    sourceLanguage: 'en-US',
    translation: [
        Lara\TextBlock { text: 'Salve, come sta?', translatable: true },
        Lara\TextBlock { text: 'Che bella giornata.', translatable: true }
    ],
    adaptedTo: [ 'mem_1_id', 'mem_2_id']
}