forked from rango-exchange/rango-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/feat/rf-1267-update-crowdin-wo…
…rkflow-with-pre-translate' into next
- Loading branch information
Showing
4 changed files
with
127 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const PROJECT_ID = process.env.CROWDIN_PROJECT_ID; | ||
export const TOKEN = process.env.CROWDIN_PERSONAL_TOKEN; | ||
|
||
export const BASE_URL = `https://api.crowdin.com/api/v2`; | ||
export const PROJECT_API = `${BASE_URL}/projects/${PROJECT_ID}`; | ||
export const PRETRANSLATE_API = `${PROJECT_API}/pre-translations` | ||
export const FILE_API = `${PROJECT_API}/files` | ||
export const MACHINE_TRANSLATE_API = `${BASE_URL}/mts`; | ||
|
||
export const REQUEST_INTERVAL_TIMEOUT = 10_000; | ||
export const MAXIMUM_PRETRANSLATION_STATUS_CHECK = 20; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { CrowdinError } from "../common/errors.mjs"; | ||
import { fetchDataWithAuthorization } from "./utils.mjs"; | ||
|
||
import { | ||
MAXIMUM_PRETRANSLATION_STATUS_CHECK, | ||
PRETRANSLATE_API, | ||
REQUEST_INTERVAL_TIMEOUT, | ||
FILE_API, | ||
MACHINE_TRANSLATE_API, | ||
PROJECT_API, | ||
} from "./constants.mjs"; | ||
|
||
|
||
export const getMachineTranslationEngineID = async () =>{ | ||
const responseData = await fetchDataWithAuthorization(MACHINE_TRANSLATE_API); | ||
if(!responseData.data || !responseData.data.length){ | ||
throw new CrowdinError('No data received for machine translation'); | ||
} | ||
return responseData.data[0].data.id; | ||
} | ||
|
||
export const getLanguageIds = async () => { | ||
const responseData = await fetchDataWithAuthorization(PROJECT_API); | ||
return responseData.data.targetLanguageIds; | ||
}; | ||
|
||
export const getSourceFileId = async () => { | ||
const responseData = await fetchDataWithAuthorization(FILE_API); | ||
if (!responseData.data || !responseData.data.length) { | ||
throw new CrowdinError('No data received for source file id'); | ||
} | ||
return responseData.data[0].data.id; | ||
}; | ||
|
||
export const sendPreTranslateRequest = async ({ sourceFileId, languageIds, preTranslationOption }) => { | ||
const responseData = await fetchDataWithAuthorization(PRETRANSLATE_API, 'POST', { | ||
...preTranslationOption, | ||
fileIds: [sourceFileId], | ||
languageIds, | ||
}); | ||
return responseData.data.identifier; | ||
}; | ||
|
||
export const checkPreTranslateStatus = async (preTranslationId) => { | ||
const maxAttempts = MAXIMUM_PRETRANSLATION_STATUS_CHECK; | ||
let attempt = 0; | ||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
while (attempt < maxAttempts) { | ||
const responseData = await fetchDataWithAuthorization(`${PRETRANSLATE_API}/${preTranslationId}`); | ||
const status = responseData.data.status; | ||
if (status === 'finished') { | ||
return status; | ||
} else { | ||
console.log(`Pre-translation status: ${status}. Retrying in 10 seconds...`); | ||
await delay(REQUEST_INTERVAL_TIMEOUT); | ||
attempt++; | ||
} | ||
} | ||
throw new CrowdinError('Timeout: Pre-translation did not succeed within the specified time.'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {TOKEN } from "./constants.mjs"; | ||
|
||
// Reusable function to handle fetch requests with authorization headers | ||
export const fetchDataWithAuthorization = async (url, method = 'GET', body = null) => { | ||
const options = { | ||
method, | ||
headers: { | ||
Authorization: `Bearer ${TOKEN}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
|
||
if (body) { | ||
options.body = JSON.stringify(body); | ||
} | ||
|
||
const response = await fetch(url, options); | ||
|
||
if (!response.ok) { | ||
throw new CrowdinError(`Failed to fetch data from ${url}. Status: ${response.status}`); | ||
} | ||
|
||
return await response.json(); | ||
}; |