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);
import (
	lara_sdk "github.com/translated/lara-go/lara"  // alias "lara_sdk", can be changed or removed
)

const (
	LARA_ACCESS_KEY_ID     = "your-access-key-id"
	LARA_ACCESS_KEY_SECRET = "your-access-key-secret"
)

credentials := lara_sdk.NewCredentials(LARA_ACCESS_KEY_ID, LARA_ACCESS_KEY_SECRET)
lara := lara_sdk.NewTranslator(credentials, nil)

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'],
                     glossaries=['gls_1_id', 'gls_2_id'],
                     instructions=['Be formal'],
                     style="fluid",
                     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'],
        glossaries: ['gls_1_id', 'gls_2_id'],
        instructions: ['Be formal'],
        style: 'fluid',
        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.setGlossaries("gls_1_id", "gls_2_id");
options.setInstructions("Be formal");
options.setStyle(TranslationStyle.FLUID);
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'),
     'glossaries' => array('gls_1_id', 'gls_2_id'),
    'instructions' => array('Be formal'),
	  'style' => 'fluid',
    'contentType' => 'text/plain',
    'timeoutInMillis' => 2000,
    'priority' => 'normal',
    'useCache' => true,
    'cacheTTLSeconds' => 86400
]);

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

res, _ := lara.Translate("Hello, how are you?", "en-US", "it-IT",
	lara_sdk.TranslateOptions{
		Instructions: []string{"Be formal"},
		Style:        "fluid",
		ContentType:  "text/plain",
		TimeoutMs:    2000,
		Priority:     "normal",
		UseCache:     &useCache,
    CacheTTL:     &cacheTTL
  })

fmt.Println(*res.Translation.String)

Here follows the basic fields for the translate method:

Field

Type     

Required     

Default     

Description

text        

String |
String[] |
TextBlock[]

Yes


The text to be translated. This can be a single string or a list (up to 128 elements) of strings or 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.

glossaries

String[]

No


A list of glossary IDs.

instructions

String[]

No

[ ]

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

style

String

No

faithful

The style to apply to the translation. Available values:
faithful fluid creative

content_type

String

No

Autodetected

Specifies the Content-Type of the text.
Available values: text/plain 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.
Note: Caching is an advanced feature and is not enabled by default. To enable caching for your account, please contact our sales team.

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.

no_trace

Boolean

No

False

If set to True, source content and its translation will not be saved on our system. (AKA Incognito mode)

verbose

Boolean

No

False

When enabled, this parameter returns additional debugging and metadata information from the engine, such as memory and glossary matches. Not recommended for production use, as it can significantly impact response times.

headers

List[String, String]

No

None

An optional object containing additional HTTP headers to include in the request. This field is primarily intended for debugging purposes and future extensibility. Use with caution. Custom headers are not required for standard usage and may be ignored or restricted by the server.


❗️

XLIFF support

Support for the application/xliff+xml content type is limited to text and a specific set of inline tags within the <source> element:

XLIFF 1.2:<g>, <x>, <ex>, <bx>, <ph>, <it>, and <mrk>.

XLIFF 2.0: <cp>, <ph>, <pc>, <sc>, <ec>, <sm>, <em> and <mrk>.

For all other tags or advanced XLIFF features, please use the Document Translation service.

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']
}
// Single string
TextResult{
    ContentType:    "text/plain",
    SourceLanguage: "en-US",
    Translation: Translation{
        String: &"Salve, come sta?",
    },
    AdaptedTo: []string{"mem_1_id", "mem_2_id"},
}

// String array
TextResult{
    ContentType:    "text/plain",
    SourceLanguage: "en-US",
    Translation: Translation{
        Strings: []string{"Salve, come sta?", "Che bella giornata."},
    },
    AdaptedTo: []string{"mem_1_id", "mem_2_id"},
}

// TextBlock array
TextResult{
    ContentType:    "text/plain",
    SourceLanguage: "en-US",
    Translation: Translation{
        TextBlocks: []TextBlock{
            {Text: "Salve, come sta?", Translatable: true},
            {Text: "Che bella giornata.", Translatable: true},
        },
    },
    AdaptedTo: []string{"mem_1_id", "mem_2_id"},
}