Skip to content

Commit a58c89e

Browse files
docs: review examples (#584)
1 parent 019b1b1 commit a58c89e

File tree

1 file changed

+135
-120
lines changed

1 file changed

+135
-120
lines changed

EXAMPLES.md

Lines changed: 135 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ import * as fs from 'fs';
1717
import crowdin from '@crowdin/crowdin-api-client';
1818

1919
const { uploadStorageApi, sourceFilesApi } = new crowdin({
20-
token: 'token',
21-
organization: 'org'
20+
token: 'token',
21+
organization: 'org'
2222
});
2323

2424
async function createFile(crowdinProjectId: number, fileName: string, fileContent: any): Promise<number> {
25-
const storageResponse = await uploadStorageApi.addStorage(fileName, fileContent);
26-
const file = await sourceFilesApi.createFile(crowdinProjectId, {
27-
storageId: storageResponse.data.id,
28-
name: fileName,
29-
});
30-
return file.data.id;
25+
const storageResponse = await uploadStorageApi.addStorage(fileName, fileContent);
26+
const file = await sourceFilesApi.createFile(crowdinProjectId, {
27+
storageId: storageResponse.data.id,
28+
name: fileName,
29+
});
30+
return file.data.id;
3131
}
3232

3333
const file = 'test.json';
@@ -44,22 +44,21 @@ import * as fs from 'fs';
4444
import crowdin from '@crowdin/crowdin-api-client';
4545

4646
const { uploadStorageApi, sourceFilesApi } = new crowdin({
47-
token: 'token',
48-
organization: 'org'
47+
token: 'token',
48+
organization: 'org'
4949
});
5050

5151
async function updateFile(projectId: number, fileId: number, fileName: string, fileContent: any): Promise<void> {
52-
const storageResponse = await uploadStorageApi.addStorage(fileName, fileContent);
53-
await sourceFilesApi.updateOrRestoreFile(projectId, fileId, {
54-
storageId: storageResponse.data.id,
55-
});
52+
const storageResponse = await uploadStorageApi.addStorage(fileName, fileContent);
53+
await sourceFilesApi.updateOrRestoreFile(projectId, fileId, {
54+
storageId: storageResponse.data.id,
55+
});
5656
}
5757

5858
const file = 'test.json';
5959
const fileContent = fs.readFileSync(file);
6060

6161
updateFile(123, 456, file, fileContent);
62-
6362
```
6463

6564
## Create TM
@@ -69,34 +68,33 @@ import * as fs from 'fs';
6968
import crowdin, { TranslationMemoryModel } from '@crowdin/crowdin-api-client';
7069

7170
const { uploadStorageApi, translationMemoryApi } = new crowdin({
72-
token: 'token',
73-
organization: 'org'
71+
token: 'token',
72+
organization: 'org'
7473
});
7574

7675
async function createTm(languageId: string, name: string, fileName: string, fileContent: any, scheme: TranslationMemoryModel.Scheme): Promise<void> {
77-
const tm = await translationMemoryApi.addTm({ languageId, name });
78-
const storage = await uploadStorageApi.addStorage(fileName, fileContent);
79-
const importTm = await translationMemoryApi.importTm(tm.data.id, {
80-
storageId: storage.data.id,
81-
scheme
82-
});
83-
84-
let status = importTm.data.status;
85-
while (status !== 'finished') {
86-
const progress = await translationMemoryApi.checkImportStatus(tm.data.id, importTm.data.identifier);
87-
status = progress.data.status;
88-
}
76+
const tm = await translationMemoryApi.addTm({ languageId, name });
77+
const storage = await uploadStorageApi.addStorage(fileName, fileContent);
78+
const importTm = await translationMemoryApi.importTm(tm.data.id, {
79+
storageId: storage.data.id,
80+
scheme
81+
});
82+
83+
let status = importTm.data.status;
84+
while (status !== 'finished') {
85+
const progress = await translationMemoryApi.checkImportStatus(tm.data.id, importTm.data.identifier);
86+
status = progress.data.status;
87+
}
8988
}
9089

9190
const file = 'test.csv';
9291
const fileContent = fs.readFileSync(file);
9392
const schema = {
94-
en: 0,
95-
uk: 1
93+
en: 0,
94+
uk: 1
9695
};
9796

9897
createTm('uk', 'test', file, fileContent, schema);
99-
10098
```
10199

102100
## Create Glossary
@@ -106,34 +104,33 @@ import * as fs from 'fs';
106104
import crowdin, { GlossariesModel } from '@crowdin/crowdin-api-client';
107105

108106
const { uploadStorageApi, glossariesApi } = new crowdin({
109-
token: 'token',
110-
organization: 'org'
107+
token: 'token',
108+
organization: 'org'
111109
});
112110

113111
async function createGlossary(languageId: string, name: string, fileName: string, fileContent: any, scheme: GlossariesModel.GlossaryFileScheme): Promise<void> {
114-
const glossary = await glossariesApi.addGlossary({ languageId, name });
115-
const storage = await uploadStorageApi.addStorage(fileName, fileContent);
116-
const importGlossary = await glossariesApi.importGlossaryFile(glossary.data.id, {
117-
storageId: storage.data.id,
118-
scheme
119-
});
120-
121-
let status = importGlossary.data.status;
122-
while (status !== 'finished') {
123-
const progress = await glossariesApi.checkGlossaryImportStatus(glossary.data.id, importGlossary.data.identifier);
124-
status = progress.data.status;
125-
}
112+
const glossary = await glossariesApi.addGlossary({ languageId, name });
113+
const storage = await uploadStorageApi.addStorage(fileName, fileContent);
114+
const importGlossary = await glossariesApi.importGlossaryFile(glossary.data.id, {
115+
storageId: storage.data.id,
116+
scheme
117+
});
118+
119+
let status = importGlossary.data.status;
120+
while (status !== 'finished') {
121+
const progress = await glossariesApi.checkGlossaryImportStatus(glossary.data.id, importGlossary.data.identifier);
122+
status = progress.data.status;
123+
}
126124
}
127125

128126
const file = 'test.csv';
129127
const fileContent = fs.readFileSync(file);
130128
const schema = {
131-
term_en: 0,
132-
description_en: 1
129+
term_en: 0,
130+
description_en: 1
133131
};
134132

135133
createGlossary('uk', 'test', file, fileContent, schema);
136-
137134
```
138135

139136
## Pre-Translate project
@@ -142,33 +139,32 @@ createGlossary('uk', 'test', file, fileContent, schema);
142139
import crowdin from '@crowdin/crowdin-api-client';
143140

144141
const { translationsApi } = new crowdin({
145-
token: 'token',
146-
organization: 'org'
142+
token: 'token',
143+
organization: 'org'
147144
});
148145

149146
async function preTranslateProject(projectId: number, languageIds: string[], fileIds: number[]): Promise<string> {
150-
const result = await translationsApi.applyPreTranslation(projectId, {
151-
languageIds,
152-
fileIds,
153-
});
154-
155-
let status = result.data.status;
156-
while (status !== 'finished') {
157-
const progress = await translationsApi.preTranslationStatus(projectId, result.data.identifier);
158-
status = progress.data.status;
159-
}
160-
161-
//as an example we export translations for 1 file and 1 language
162-
const translations = await translationsApi.exportProjectTranslation(projectId, {
163-
targetLanguageId: languageIds[0],
164-
fileIds: [fileIds[0]],
165-
});
166-
167-
return translations.data.url;
147+
const result = await translationsApi.applyPreTranslation(projectId, {
148+
languageIds,
149+
fileIds,
150+
});
151+
152+
let status = result.data.status;
153+
while (status !== 'finished') {
154+
const progress = await translationsApi.preTranslationStatus(projectId, result.data.identifier);
155+
status = progress.data.status;
156+
}
157+
158+
//as an example we export translations for 1 file and 1 language
159+
const translations = await translationsApi.exportProjectTranslation(projectId, {
160+
targetLanguageId: languageIds[0],
161+
fileIds: [fileIds[0]],
162+
});
163+
164+
return translations.data.url;
168165
}
169166

170167
preTranslateProject(123, ['uk'], [456]);
171-
172168
```
173169

174170
## Download translations
@@ -177,26 +173,25 @@ preTranslateProject(123, ['uk'], [456]);
177173
import crowdin from '@crowdin/crowdin-api-client';
178174

179175
const { translationsApi } = new crowdin({
180-
token: 'token',
181-
organization: 'org'
176+
token: 'token',
177+
organization: 'org'
182178
});
183179

184180
async function downloadTranslations(projectId: number): Promise<string> {
185-
const result = await translationsApi.buildProject(projectId);
181+
const result = await translationsApi.buildProject(projectId);
186182

187-
let status = result.data.status;
188-
while (status !== 'finished') {
189-
const progress = await translationsApi.checkBuildStatus(projectId, result.data.id);
190-
status = progress.data.status;
191-
}
183+
let status = result.data.status;
184+
while (status !== 'finished') {
185+
const progress = await translationsApi.checkBuildStatus(projectId, result.data.id);
186+
status = progress.data.status;
187+
}
192188

193-
const translations = await translationsApi.downloadTranslations(projectId, result.data.id);
189+
const translations = await translationsApi.downloadTranslations(projectId, result.data.id);
194190

195-
return translations.data.url;
191+
return translations.data.url;
196192
}
197193

198194
downloadTranslations(123);
199-
200195
```
201196

202197
## Generate report
@@ -205,55 +200,75 @@ downloadTranslations(123);
205200
import crowdin from '@crowdin/crowdin-api-client';
206201

207202
const { reportsApi } = new crowdin({
208-
token: 'token',
209-
organization: 'org'
203+
token: 'token',
204+
organization: 'org'
210205
});
211206

207+
function wait(ms: number): Promise<void> {
208+
return new Promise(resolve => setTimeout(resolve, ms));
209+
}
210+
212211
async function costsEstimationPostEditingReport(projectId: number): Promise<string> {
212+
try {
213213
const result = await reportsApi.generateReport(projectId, {
214-
name: 'costs-estimation-pe',
215-
schema: {
216-
languageId: 'uk',
217-
currency: 'USD',
218-
unit: 'words',
219-
format: 'json',
220-
calculateInternalMatches: true,
221-
includePreTranslatedStrings: false,
222-
baseRates: {
223-
fullTranslation: 0.6,
224-
proofread: 0.05,
225-
},
226-
individualRates: [{
227-
languageIds: [
228-
"ee"
229-
],
230-
fullTranslation: 0.1,
231-
proofread: 0.12
232-
}],
233-
netRateSchemes: {
234-
tmMatch: [{
235-
matchType: "perfect",
236-
price: 0.01
237-
}, {
238-
matchType: "100",
239-
price: 0.5
240-
}, {
241-
matchType: "99-82",
242-
price: 0.7
243-
}]
244-
},
214+
name: 'costs-estimation-pe',
215+
schema: {
216+
languageId: 'uk',
217+
currency: 'USD',
218+
unit: 'words',
219+
format: 'json',
220+
calculateInternalMatches: true,
221+
includePreTranslatedStrings: false,
222+
baseRates: {
223+
fullTranslation: 0.6,
224+
proofread: 0.1,
225+
},
226+
individualRates: [{
227+
languageIds: [
228+
"ee"
229+
],
230+
fullTranslation: 0.12,
231+
proofread: 0.1
232+
}],
233+
netRateSchemes: {
234+
tmMatch: [{
235+
matchType: "perfect",
236+
price: 0.1
237+
}, {
238+
matchType: "100",
239+
price: 0.1
240+
}, {
241+
matchType: "99-94",
242+
price: 0.33
243+
}]
245244
},
245+
},
246246
});
247247

248-
let status = result.data.status;
249-
while (status !== 'finished') {
250-
const progress = await reportsApi.checkReportStatus(projectId, result.data.identifier);
251-
status = progress.data.status;
248+
const identifier = result.data.identifier;
249+
let status = 'created';
250+
251+
while (status === 'created' || status === 'in_progress') {
252+
const statusData = await reportsApi.checkReportStatus(projectId, identifier);
253+
status = statusData.data.status;
254+
255+
if (status === 'canceled' || status === 'failed') {
256+
throw new Error(`Report generation failed with status: ${status}`);
257+
}
258+
259+
await wait(2000);
252260
}
253261

254-
const report = await reportsApi.downloadReport(projectId, result.data.identifier);
262+
if (status === 'finished') {
263+
const report = await reportsApi.downloadReport(projectId, identifier);
264+
return report.data.url;
265+
}
255266

256-
return report.data.url;
267+
throw new Error(`Unexpected report status: ${status}`);
268+
} catch (error) {
269+
console.error('Error generating report:', error);
270+
throw error;
271+
}
257272
}
258273

259274
costsEstimationPostEditingReport(123);

0 commit comments

Comments
 (0)