The Languine SDK provides a simple and powerful way to integrate Languine's translation capabilities into your applications.
npm install @languine/sdk
import { Languine } from "@languine/sdk"; async function run() { const languine = new Languine({ apiKey: process.env.LANGUINE_API_KEY, }); const result = await languine.translate({ sourceText: "Hello, world!", sourceLocale: "en", targetLocale: "sv", projectId: process.env.LANGUINE_PROJECT_ID, }); console.log(result.translatedText); } run();
The SDK requires an API key to authenticate requests. Your API key should start with org_. You can obtain an API key from your Languine dashboard.
const languine = new Languine({ apiKey: process.env.LANGUINE_API_KEY, });
Parameters:
The translate method accepts an object with the following properties:
projectId (string, required): The ID of your Languine projectsourceLocale (string, required): The locale code of the source texttargetLocale (string, required): The locale code to translate intosourceText (string, required): The text to translateformat (string, optional): The format of the source text. Defaults to "string"cache (boolean, optional): Whether to use cached translations. Defaults to trueSupported Formats:
string: Plain textjson: JSON filesyaml: YAML filesproperties: Java properties filesandroid: Android resource filesxcode-strings: iOS Strings filesxcode-stringsdict: iOS Stringsdict filesxcode-xcstrings: iOS XCStrings filesmd: Markdown filesmdx: MDX fileshtml: HTML filesjs: JavaScript filests: TypeScript filespo: Gettext PO filesxliff: XLIFF filescsv: CSV filesxml: XML filesarb: ARB (Application Resource Bundle) filesftl: Fluent FTL filesphp: PHP filesCache Management: By default, translations are cached. You can disable caching for specific requests by setting cache: false in the translate parameters.
Error Handling: Always implement proper error handling in your application to gracefully handle API errors.
API Key Security: Never expose your API key in client-side code. Always keep it secure on your server.
Format Selection: Choose the appropriate format for your content type to ensure accurate translations.
const languine = new Languine({ apiKey: process.env.LANGUINE_API_KEY, }); const jsonContent = { greeting: "Hello", farewell: "Goodbye" }; const translation = await languine.translate({ projectId: 'your_project_id', sourceLocale: 'en', targetLocale: 'fr', sourceText: JSON.stringify(jsonContent), format: 'json' }); const translatedJson = JSON.parse(translation.translatedText);
const languine = new Languine({ apiKey: process.env.LANGUINE_API_KEY, }); const markdownContent = "# Welcome\n\nThis is a **markdown** document."; const translation = await languine.translate({ projectId: 'your_project_id', sourceLocale: 'en', targetLocale: 'de', sourceText: markdownContent, format: 'md' }); console.log(translation.translatedText);