Function Descriptions
Introduction
The Frenglish SDK is a powerful tool that enables developers to integrate automatic translation of content into their applications. This SDK handles the entire translation process, from submitting content for translation to retrieving the translated content. This document provides detailed information on how to use each method in the SDK.
Installation
Please refer to the Quickstart Guide for installation instructions.
SDK Methods
translate
translate(contents: string[], isFullTranslation: boolean, filenames: string[], partialConfig: PartialConfiguration): Promise<RequestTranslationResponse>
Sends content for translation. This method handles the polling process automatically and returns the translated content when completed.
Parameters:
- content: string[] - An array of text content to be translated. Each element represents a separate piece of content.
- fullTranslation: boolean (optional, default false) - Controls translation behavior:
- When false (default): Optimizes translation by checking previously translated content in the database. Only translates new or modified content, reducing translation time and costs.
- When true: Forces a complete retranslation of all content, ignoring any existing translations.
- filenames: string[] (optional) - An array of filenames corresponding to each content item. Used to track and identify translations within your project. If provided, must match the length of content array. The filenames should include file extensions (e.g., .json).
- partialConfig: PartialConfiguration (optional) - Override default configuration settings for this translation. Can include:
{
originLanguage?: string, // Source language code
languages?: string[], // Target language codes
rules?: string, // General translation rules
autoMergeToBaseBranch?: boolean, // Auto-merge setting
implicitRules?: ImplicitRule[], // Array of implicit translation rules
rulesPerLanguage?: Rule[], // Language-specific rules
useThisConfig?: boolean, // Whether to use this config
keyFilters?: { // Filters for translation keys
includeFilters: string[],
excludeFilters: string[]
} | null
}
Returns:
A Promise that resolves to a RequestTranslationResponse object containing:
- translationId: number - Unique identifier for the translation request.
- content?: TranslationResponse[] - Array of TranslationResponse objects, each representing translated content for a specific language.
Example:
const contents = [
'{"hello": "Hello, world!"}',
'{"goodbye": "Goodbye, world!"}'
];
const filenames = ['greetings.json', 'farewells.json'];
const partialConfig = {
languages: ['fr', 'es'],
rules: 'use an informal tone'
};
try {
const translation = await frenglish.translate(contents, false, filenames, partialConfig);
if (translation && translation.content) {
console.log('Translation completed:', translation.content);
} else {
console.log('Translation in progress or failed.');
}
} catch (error) {
console.error('Translation error:', error.message);
}
Errors:
- Throws an error if the translation request fails or if the polling exceeds the maximum allowed time.
- Throws an error if the translation is cancelled.
translateString
translateString(text: string, targetLanguage: string, partialConfig: PartialConfiguration): Promise<string>