Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from manGoweb/pr/bulk-translation
Browse files Browse the repository at this point in the history
Add bulk translations
  • Loading branch information
jonasnobile authored May 18, 2022
2 parents c216d4b + 45388e5 commit a433927
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/translate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch, { } from "node-fetch"

import { generateMutation } from './utils.js'
import {
CONTEMBER_CONTENT_URL,
CONTEMBER_TOKEN,
Expand All @@ -16,8 +16,13 @@ export async function translate() {
query: `
query {
listOfferParameterValue(
filter: { specification: { isNull: false } and: { specificationUK: { isNull: true } } },
limit: 1
filter: {
and: {
specification: { isNull: false }
specificationUK: { isNull: true }
}
}
limit: 10
) {
id
specification
Expand All @@ -31,29 +36,40 @@ export async function translate() {

if (!thingsToTranslate.length) return

console.log('Sending translation requests...')
const translations = []
for (const { id, specification } of thingsToTranslate) {
const params = new URLSearchParams()
params.append('input_text', specification)

const result = await fetch('https://lindat.cz/translation/api/v2/languages/?src=cs&tgt=uk&logInput=true&author=PomahejUkrajine', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
body: params
})
if (!specification.trim().length) {
console.log('Skipping empty translation request.', specification)
translations.push({ text: specification, id })
} else {
const params = new URLSearchParams()
params.append('input_text', specification)

if (!result.ok) console.error('Failed to translate: ', id, result)
console.log('Sending translation request for ', id)
const result = await fetch('https://lindat.cz/translation/api/v2/languages/?src=cs&tgt=uk&logInput=true&author=PomahejUkrajine', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
body: params
})

const json = await result.json()
if (!result.ok) {
console.error('Failed to translate: ', id, result)
return
}

saveTranslation(json[0], id)
const json = await result.json()
translations.push({ text: json[0], id })
}
}

saveTranslations(translations)
}

const saveTranslation = async (translation: String, translationID: String) => {
const saveTranslations = async (translations: { text: string, id: string }[]) => {
const mutation = generateMutation(translations)
const response = await fetch(
CONTEMBER_CONTENT_URL,
{
Expand All @@ -63,32 +79,18 @@ const saveTranslation = async (translation: String, translationID: String) => {
'Authorization': `Bearer ${CONTEMBER_TOKEN}`,
},
body: JSON.stringify({
query: `
mutation($data: String, $id: UUID!) {
updateOfferParameterValue(
by: { id: $id }
data: {
specificationUK: $data
}
) {
ok
errorMessage
}
}
`,
variables: {
data: translation,
id: translationID,
},
query: mutation,
}),
}
)

if (response.ok) {
console.log('Saved translation.')
console.log('Translations saved.')
} else {
console.error('Failed to save translation: ', response)
console.error('Translations failed to save: ', response)
}

return
}


17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ import crypto from "crypto";
export const generateSecretCode = (): string => {
return crypto.randomBytes(16).toString('hex')
}

export function generateMutation(translations: { text: string, id: string }[]): string {
let mutation = `mutation {`
for (let index = 0; index < translations.length; index++) {
mutation += `
update_${index}: updateOfferParameterValue(by: {
id: "${translations[index].id}" },
data: { specificationUK: ${JSON.stringify(translations[index].text)} })
{
ok
errorMessage
}
`
}
mutation += `}`
return mutation
}

0 comments on commit a433927

Please sign in to comment.