@frenglish/sdk
v1.0.59
Published
SDK for Frenglish translation service
Downloads
1,127
Readme
Frenglish SDK
_____ _ _ _ _
| ___| __ ___ _ __ __ _| (_)___| |__ __ _(_)
| |_ | '__/ _ \ '_ \ / _` | | / __| '_ \ / _` | |
| _|| | | __/ | | | (_| | | \__ \ | | || (_| | |
|_| |_| \___|_| |_|\__, |_|_|___/_| |_(_)__,_|_|
|___/ A powerful SDK for integrating Frenglish.ai's translation services into your applications. This SDK provides a flexible and robust way to handle translations programmatically. By initializing the frenglish SDK with your private key, it will contain all the information about your translation configuration (rules, origin language, target languages, etc).
Installation
npm install @frenglish/sdkQuick Start
import { FrenglishSDK } from '@frenglish/sdk';
// Initialize the SDK with your API key
const sdk = FrenglishSDK('your_api_key');
// Example: Translate content
async function translateContent() {
try {
const result = await sdk.translate(
['Hello world', 'Welcome to our app'],
true, // isFullTranslation
['welcome.txt', 'greeting.txt']
);
console.log('Translation result:', result);
} catch (error) {
console.error('Translation failed:', error);
}
}Core Features
Translation
// Translate multiple strings
const result = await sdk.translate(
content: string[], // Array of content to translate
isFullTranslation?: boolean, // Whether to perform a full translation
filenames?: string[], // Optional filenames for the content
partialConfig?: PartialConfiguration // Optional configuration overrides
);
// Translate a single string
const translated = await sdk.translateString(
content: string | string[], // String or string[] to translate
lang: string, // Target language code
isFullTranslation?: boolean, // Whether to perform a full translation
partialConfig?: PartialConfiguration.
);Project Management
// Get project information
const project = await sdk.getProjectInformation();
// Update project name
await sdk.updateProjectName('New Project Name');
// Toggle project active status
await sdk.setProjectActiveStatus(true);
// Toggle test mode
await sdk.setTestMode(true);Configuration
// Get default configuration
const config = await sdk.getDefaultConfiguration();
// Update configuration
await sdk.updateConfiguration({
originLanguage: 'en',
languages: ['fr', 'es'],
rules: 'Use a casual tone'
});
// Get supported languages
const languages = await sdk.getSupportedLanguages();
// Get supported file types
const fileTypes = await sdk.getSupportedFileTypes();File Management
// Upload files for translation
const uploadResult = await sdk.upload([
{
content: 'Hello world',
language: 'en'
}
]);
// Get project's text map
const textMap = await sdk.getTextMap();SDK Methods Reference
Translation Methods
translate
- Description: Translates multiple strings or content blocks to all configured target languages
- Parameters:
content: string[] - Array of content to translateisFullTranslation: boolean (optional) - Whether to perform a full translationfilenames: string[] (optional) - Filenames for the contentpartialConfig: PartialConfiguration (optional) - Configuration overrides
- Returns: Promise<{ translationId: number, content: TranslationResponse[] }>
translateString
- Description: Translates a single string to a specific target language
- Parameters:
content: string | string [] - Single string to translate or an array of strings to translatelang: string - Target language codeisFullTranslation: boolean (optional) - Whether to perform a full translationpartialConfig: PartialConfiguration (optional) - Configuration overrides
- Returns: Promise<string | string[] | undefined>
getTranslationStatus
- Description: Checks the current status of a translation request (e.g., completed, in progress, cancelled)
- Parameters:
translationId: number - ID of the translation to check
- Returns: Promise
getTranslationContent
- Description: Retrieves the translated content for a completed translation
- Parameters:
translationId: number - ID of the translation to retrieve
- Returns: Promise<TranslationResponse[]>
Project Management Methods
getProjectInformation
- Description: Retrieves detailed information about the current project
- Returns: Promise
updateProjectName
- Description: Updates the name of the current project
- Parameters:
updatedProjectName: string - New project name
- Returns: Promise
setProjectActiveStatus
- Description: Enables or disables the project's active status
- Parameters:
isActive: boolean - New active status
- Returns: Promise
setTestMode
- Description: Toggles test mode for the project, useful for testing without consuming API credits
- Parameters:
isTestMode: boolean - New test mode status
- Returns: Promise
Configuration Methods
getDefaultConfiguration
- Description: Retrieves the default configuration settings for the project
- Returns: Promise
updateConfiguration
- Description: Updates the project's configuration settings
- Parameters:
partiallyUpdatedConfig: PartialConfiguration - Configuration updates
- Returns: Promise
getProjectSupportedLanguages
- Description: Gets the list of languages supported by the project and the origin language
- Returns: Promise<{ languages: string[], originLanguage: string }>
getSupportedLanguages
- Description: Gets all languages supported by the Frenglish service
- Returns: Promise<string[]>
getSupportedFileTypes
- Description: Gets all file types that can be processed for translation
- Returns: Promise<string[]>
File Management Methods
upload
- Description: Uploads files for translation, typically used as base files to compare against
- Parameters:
files: FileContentWithLanguage[] - Array of files to upload
- Returns: Promise<{ message: string, originFilesInfo: Array<{ fileId: string, originS3Version: string }> }>
getTextMap
- Description: Retrieves the project's text map, which contains mappings of text content for consistency
- Returns: Promise<{ content: FlatJSON[] } | null>
Domain Management Methods
getProjectDomain
- Description: Gets the domain URL associated with the current project
- Returns: Promise
getPublicAPIKeyFromDomain
- Description: Retrieves the public API key associated with a given domain
- Parameters:
domainURL: string - Domain URL to get API key for
- Returns: Promise
Configuration Types
interface PartialConfiguration {
originLanguage?: string;
languages?: string[];
rules?: string;
languageSpecificRules?: Record<string, string>;
// ... other configuration options
}Error Handling
The SDK throws errors for various scenarios. Always wrap SDK calls in try-catch blocks to help you debug the issues:
try {
const result = await sdk.translate(['Hello world']);
} catch (error) {
if (error.message.includes('cancelled')) {
// Handle cancellation
} else if (error.message.includes('unsupported')) {
// Handle unsupported language
} else {
// Handle other errors
}
}Best Practices
API Key Management
- Store your API key securely
- Use environment variables for sensitive data
- Never commit API keys to version control
Translation Strategy
- Use
isFullTranslation: falsefor incremental updates - Use
isFullTranslation: truewhen you want to overwrite existing translations - Consider using the text map for consistency
- Use
Error Handling
- Implement proper error handling for all SDK calls
- Log errors appropriately
- Implement retry logic for transient failures
Configuration
- Use partial configuration to override specific settings
- Keep configuration consistent across your application
- Use language-specific rules when needed
Examples
Basic Translation
const sdk = FrenglishSDK(process.env.FRENGLISH_API_KEY);
async function translateContent() {
try {
const result = await sdk.translate(
['Welcome to our app', 'Please sign in'],
false,
['welcome.txt', 'signin.txt']
);
console.log('Translation complete:', result);
} catch (error) {
console.error('Translation failed:', error);
}
}Support
For more information, visit https://www.frenglish.ai or email us at [email protected]
