Crowdin API client usage examples
Create a file in a Crowdin project
import * as fs from 'fs' ;
import crowdin from '@crowdin/crowdin-api-client' ;
const { uploadStorageApi, sourceFilesApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function createFile ( crowdinProjectId : number , fileName : string , fileContent : any ) : Promise < number > {
const storageResponse = await uploadStorageApi . addStorage ( fileName , fileContent ) ;
const file = await sourceFilesApi . createFile ( crowdinProjectId , {
storageId : storageResponse . data . id ,
name : fileName ,
} ) ;
return file . data . id ;
}
const file = 'test.json' ;
const fileContent = fs . readFileSync ( file ) ;
createFile ( 123 , file , fileContent ) ;
import * as fs from 'fs' ;
import crowdin from '@crowdin/crowdin-api-client' ;
const { uploadStorageApi, sourceFilesApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function updateFile ( projectId : number , fileId : number , fileName : string , fileContent : any ) : Promise < void > {
const storageResponse = await uploadStorageApi . addStorage ( fileName , fileContent ) ;
await sourceFilesApi . updateOrRestoreFile ( projectId , fileId , {
storageId : storageResponse . data . id ,
} ) ;
}
const file = 'test.json' ;
const fileContent = fs . readFileSync ( file ) ;
updateFile ( 123 , 456 , file , fileContent ) ;
import * as fs from 'fs' ;
import crowdin , { TranslationMemoryModel } from '@crowdin/crowdin-api-client' ;
const { uploadStorageApi, translationMemoryApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function createTm ( languageId : string , name : string , fileName : string , fileContent : any , scheme : TranslationMemoryModel . Scheme ) : Promise < void > {
const tm = await translationMemoryApi . addTm ( { languageId, name } ) ;
const storage = await uploadStorageApi . addStorage ( fileName , fileContent ) ;
const importTm = await translationMemoryApi . importTm ( tm . data . id , {
storageId : storage . data . id ,
scheme
} ) ;
let status = importTm . data . status ;
while ( status !== 'finished' ) {
const progress = await translationMemoryApi . checkImportStatus ( tm . data . id , importTm . data . identifier ) ;
status = progress . data . status ;
}
}
const file = 'test.csv' ;
const fileContent = fs . readFileSync ( file ) ;
const schema = {
en : 0 ,
uk : 1
} ;
createTm ( 'uk' , 'test' , file , fileContent , schema ) ;
import * as fs from 'fs' ;
import crowdin , { GlossariesModel } from '@crowdin/crowdin-api-client' ;
const { uploadStorageApi, glossariesApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function createGlossary ( languageId : string , name : string , fileName : string , fileContent : any , scheme : GlossariesModel . GlossaryFileScheme ) : Promise < void > {
const glossary = await glossariesApi . addGlossary ( { languageId, name } ) ;
const storage = await uploadStorageApi . addStorage ( fileName , fileContent ) ;
const importGlossary = await glossariesApi . importGlossaryFile ( glossary . data . id , {
storageId : storage . data . id ,
scheme
} ) ;
let status = importGlossary . data . status ;
while ( status !== 'finished' ) {
const progress = await glossariesApi . checkGlossaryImportStatus ( glossary . data . id , importGlossary . data . identifier ) ;
status = progress . data . status ;
}
}
const file = 'test.csv' ;
const fileContent = fs . readFileSync ( file ) ;
const schema = {
term_en : 0 ,
description_en : 1
} ;
createGlossary ( 'uk' , 'test' , file , fileContent , schema ) ;
import crowdin from '@crowdin/crowdin-api-client' ;
const { translationsApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function preTranslateProject ( projectId : number , languageIds : string [ ] , fileIds : number [ ] ) : Promise < string > {
const result = await translationsApi . applyPreTranslation ( projectId , {
languageIds,
fileIds,
} ) ;
let status = result . data . status ;
while ( status !== 'finished' ) {
const progress = await translationsApi . preTranslationStatus ( projectId , result . data . identifier ) ;
status = progress . data . status ;
}
//as an example we export translations for 1 file and 1 language
const translations = await translationsApi . exportProjectTranslation ( projectId , {
targetLanguageId : languageIds [ 0 ] ,
fileIds : [ fileIds [ 0 ] ] ,
} ) ;
return translations . data . url ;
}
preTranslateProject ( 123 , [ 'uk' ] , [ 456 ] ) ;
import crowdin from '@crowdin/crowdin-api-client' ;
const { translationsApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
async function downloadTranslations ( projectId : number ) : Promise < string > {
const result = await translationsApi . buildProject ( projectId ) ;
let status = result . data . status ;
while ( status !== 'finished' ) {
const progress = await translationsApi . checkBuildStatus ( projectId , result . data . id ) ;
status = progress . data . status ;
}
const translations = await translationsApi . downloadTranslations ( projectId , result . data . id ) ;
return translations . data . url ;
}
downloadTranslations ( 123 ) ;
import crowdin from '@crowdin/crowdin-api-client' ;
const { reportsApi } = new crowdin ( {
token : 'token' ,
organization : 'org'
} ) ;
function wait ( ms : number ) : Promise < void > {
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
}
async function costsEstimationPostEditingReport ( projectId : number ) : Promise < string > {
try {
const result = await reportsApi . generateReport ( projectId , {
name : 'costs-estimation-pe' ,
schema : {
languageId : 'uk' ,
currency : 'USD' ,
unit : 'words' ,
format : 'json' ,
calculateInternalMatches : true ,
includePreTranslatedStrings : false ,
baseRates : {
fullTranslation : 0.6 ,
proofread : 0.1 ,
} ,
individualRates : [ {
languageIds : [
"ee"
] ,
fullTranslation : 0.12 ,
proofread : 0.1
} ] ,
netRateSchemes : {
tmMatch : [ {
matchType : "perfect" ,
price : 0.1
} , {
matchType : "100" ,
price : 0.1
} , {
matchType : "99-94" ,
price : 0.33
} ]
} ,
} ,
} ) ;
const identifier = result . data . identifier ;
let status = 'created' ;
while ( status === 'created' || status === 'in_progress' ) {
const statusData = await reportsApi . checkReportStatus ( projectId , identifier ) ;
status = statusData . data . status ;
if ( status === 'canceled' || status === 'failed' ) {
throw new Error ( `Report generation failed with status: ${ status } ` ) ;
}
await wait ( 2000 ) ;
}
if ( status === 'finished' ) {
const report = await reportsApi . downloadReport ( projectId , identifier ) ;
return report . data . url ;
}
throw new Error ( `Unexpected report status: ${ status } ` ) ;
} catch ( error ) {
console . error ( 'Error generating report:' , error ) ;
throw error ;
}
}
costsEstimationPostEditingReport ( 123 ) ;