Installation
Learn how to download and install JSON translation files from Lengrise API into your project.
This guide shows you how to download and integrate your translation JSON files from Lengrise into your project. We provide code examples in multiple programming languages to help you quickly set up localization in your application.
API Key Required:
You'll need an API key from your Lengrise project to download translation files. Find it in your project settings under the API section.
Getting Started
Follow these steps to install translation files from Lengrise API into your project. Choose the programming language you're using and follow the instructions.
Step 1: Copy the code for your project
// utils/initialize-translations.js
import fs from "fs/promises";
import path from "path";
/**
* Initialize translations by fetching from Lengrise API and saving them as JSON files.
* @param {string} apiUrl - The API URL to fetch translations from.
* @param {string} outputDir - The local directory to save translation files.
*/
async function initializeTranslations(
apiUrl = "https://lengrise.com/api/translations",
outputDir = "./public/locales"
) {
try {
const apiKey = process.env.LENGRISE_API_KEY;
if (!apiKey) {
throw new Error("Missing LENGRISE_API_KEY environment variable.");
}
console.log("⏳ Fetching translations from Lengrise...");
const response = await fetch(apiUrl, {
headers: {
"x-api-key": apiKey,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch translations: ${response.status} ${response.statusText}`);
}
const translations = await response.json();
const languages = Object.keys(translations);
if (languages.length === 0) {
console.warn("⚠️ No translations found.");
return;
}
// Ensure output directory exists
await fs.mkdir(outputDir, { recursive: true });
// Write each language JSON file
for (const lang of languages) {
const filePath = path.join(outputDir, `${lang}.json`);
const fileContent = JSON.stringify(translations[lang], null, 2);
try {
await fs.writeFile(filePath, fileContent, "utf-8");
console.log(`✅ Created ${filePath}`);
} catch (writeError) {
console.error(`❌ Failed to create ${filePath}: ${writeError.message}`);
}
}
console.log(`🎉 Translations successfully initialized in '${outputDir}'`);
console.log(`🌎 Total languages: ${languages.length}`);
} catch (error) {
console.error(`🚨 Error initializing translations: ${error.message}`);
process.exit(1);
}
}
initializeTranslations();
Select the tab for your programming language above and copy the code into your project. Save it to the appropriate location based on your project structure.
Step 2: Run the code to fetch translations
Execute the code using the appropriate command for your language to fetch translations from Lengrise and save them as JSON files:
Language | File Extension | Command to Run |
---|---|---|
JavaScript | .js | node utils/initialize-translations.js |
Python | .py | python utils/initialize_translations.py |
PHP | .php | php utils/initialize_translations.php |
C# (.NET Core) | .cs | dotnet run (in project directory) |
Java | .java | javac InitializeTranslations.java && java InitializeTranslations |
Go | .go | go run utils/initialize_translations.go |
Dart | .dart | dart lib/utils/initialize_translations.dart |
Step 3: Integrate the JSON files with your app
Once the JSON files are generated in your locales directory, you can integrate them with your application's localization system. For framework-specific integration guides, see our Integration Guides.