Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Auto translation triggering on Single Step Import #2447

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading