Skip to content

Commit

Permalink
fix: Auto translation triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Aug 30, 2024
1 parent c0683cc commit 1ae3de3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Encoding
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.tolgee.activity.RequestActivity
import io.tolgee.activity.data.ActivityType
import io.tolgee.dtos.dataImport.ImportFileDto
import io.tolgee.dtos.request.SingleStepImportRequest
import io.tolgee.model.enums.Scope
Expand Down Expand Up @@ -61,6 +63,7 @@ class SingleStepImportController(
)
@AllowApiAccess
@OpenApiOrderExtension(1)
@RequestActivity(ActivityType.IMPORT)
fun doImport(
@RequestPart("files")
files: Array<MultipartFile>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.tolgee.api.v2.controllers.v2ImportController

import io.tolgee.ProjectAuthControllerTest
import io.tolgee.batch.data.BatchJobType
import io.tolgee.constants.Message
import io.tolgee.development.testDataBuilder.data.SingleStepImportTestData
import io.tolgee.fixtures.andHasErrorMessage
import io.tolgee.fixtures.andIsBadRequest
import io.tolgee.fixtures.andIsForbidden
import io.tolgee.fixtures.andIsOk
import io.tolgee.model.batch.BatchJob
import io.tolgee.model.enums.Scope
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assert
Expand Down Expand Up @@ -216,12 +218,21 @@ class SingleStepImportControllerTest : ProjectAuthControllerTest("/v2/projects/"
@ProjectJWTAuthTestMethod
fun `imports xliff file`() {
saveAndPrepare()
val fileName = "en.xliff"
performImport(
projectId = testData.project.id,
listOf(Pair(fileName, appleXliffFile)),
getFileMappings(fileName, format = "APPLE_XLIFF", languageTag = "en"),
).andIsOk
importXliffFile()
}

@Test
@ProjectJWTAuthTestMethod
fun `triggers auto translation`() {
testData.projectBuilder.addAutoTranslationConfig {
enableForImport = true
usingPrimaryMtService = true
}

saveAndPrepare()
importXliffFile()

assertAutoTranslationTriggered()
}

@Test
Expand Down Expand Up @@ -280,6 +291,20 @@ class SingleStepImportControllerTest : ProjectAuthControllerTest("/v2/projects/"
}
}

private fun importXliffFile() {
val fileName = "en.xliff"
performImport(
projectId = testData.project.id,
listOf(Pair(fileName, appleXliffFile)),
getFileMappings(fileName, format = "APPLE_XLIFF", languageTag = "en"),
).andIsOk
}

private fun assertAutoTranslationTriggered() {
val job = entityManager.createQuery("from BatchJob bj", BatchJob::class.java).singleResult
job.type.assert.isEqualTo(BatchJobType.AUTO_TRANSLATE)
}

private fun assertXliffDataImported() {
getTestKeyTranslations().find { it.language.tag == "de" }!!.text.assert.isEqualTo("Test cs")
getTestKeyTranslations().find { it.language.tag == "en" }!!.text.assert.isEqualTo("Test en")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ class AutoTranslationEventHandler(
return
}

if (event.activityRevision.modifiedEntities.any { it.entityClass == Translation::class.simpleName }) {
autoTranslationService.autoTranslateViaBatchJob(
projectId = projectId,
keyIds = keyIds,
isBatch = true,
baseLanguageId = baseLanguageId ?: return,
isHiddenJob = event.isLowVolumeActivity(),
)
}
autoTranslationService.autoTranslateViaBatchJob(
projectId = projectId,
keyIds = keyIds,
isBatch = true,
baseLanguageId = baseLanguageId ?: return,
isHiddenJob = event.isLowVolumeActivity(),
)
}

private fun shouldRunTheOperation(): Boolean {
if (event.activityRevision.modifiedEntities.none { it.entityClass == Translation::class.simpleName }) {
return false
}

val configs =
autoTranslationService.getConfigs(
entityManager.getReference(Project::class.java, projectId),
Expand Down

0 comments on commit 1ae3de3

Please sign in to comment.