Skip to content

Commit 032007b

Browse files
authored
fix: Auto translation triggering on Single Step Import (tolgee#2447)
1 parent 76fa5f0 commit 032007b

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

backend/api/src/main/kotlin/io/tolgee/api/v2/controllers/dataImport/SingleStepImportController.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import io.swagger.v3.oas.annotations.Operation
88
import io.swagger.v3.oas.annotations.media.Content
99
import io.swagger.v3.oas.annotations.media.Encoding
1010
import io.swagger.v3.oas.annotations.parameters.RequestBody
11+
import io.tolgee.activity.RequestActivity
12+
import io.tolgee.activity.data.ActivityType
1113
import io.tolgee.dtos.dataImport.ImportFileDto
1214
import io.tolgee.dtos.request.SingleStepImportRequest
1315
import io.tolgee.model.enums.Scope
@@ -61,6 +63,7 @@ class SingleStepImportController(
6163
)
6264
@AllowApiAccess
6365
@OpenApiOrderExtension(1)
66+
@RequestActivity(ActivityType.IMPORT)
6467
fun doImport(
6568
@RequestPart("files")
6669
files: Array<MultipartFile>,

backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/SingleStepImportControllerTest.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.tolgee.api.v2.controllers.v2ImportController
22

33
import io.tolgee.ProjectAuthControllerTest
4+
import io.tolgee.batch.data.BatchJobType
45
import io.tolgee.constants.Message
56
import io.tolgee.development.testDataBuilder.data.SingleStepImportTestData
67
import io.tolgee.fixtures.andHasErrorMessage
78
import io.tolgee.fixtures.andIsBadRequest
89
import io.tolgee.fixtures.andIsForbidden
910
import io.tolgee.fixtures.andIsOk
11+
import io.tolgee.model.batch.BatchJob
1012
import io.tolgee.model.enums.Scope
1113
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
1214
import io.tolgee.testing.assert
@@ -216,12 +218,21 @@ class SingleStepImportControllerTest : ProjectAuthControllerTest("/v2/projects/"
216218
@ProjectJWTAuthTestMethod
217219
fun `imports xliff file`() {
218220
saveAndPrepare()
219-
val fileName = "en.xliff"
220-
performImport(
221-
projectId = testData.project.id,
222-
listOf(Pair(fileName, appleXliffFile)),
223-
getFileMappings(fileName, format = "APPLE_XLIFF", languageTag = "en"),
224-
).andIsOk
221+
importXliffFile()
222+
}
223+
224+
@Test
225+
@ProjectJWTAuthTestMethod
226+
fun `triggers auto translation`() {
227+
testData.projectBuilder.addAutoTranslationConfig {
228+
enableForImport = true
229+
usingPrimaryMtService = true
230+
}
231+
232+
saveAndPrepare()
233+
importXliffFile()
234+
235+
assertAutoTranslationTriggered()
225236
}
226237

227238
@Test
@@ -280,6 +291,20 @@ class SingleStepImportControllerTest : ProjectAuthControllerTest("/v2/projects/"
280291
}
281292
}
282293

294+
private fun importXliffFile() {
295+
val fileName = "en.xliff"
296+
performImport(
297+
projectId = testData.project.id,
298+
listOf(Pair(fileName, appleXliffFile)),
299+
getFileMappings(fileName, format = "APPLE_XLIFF", languageTag = "en"),
300+
).andIsOk
301+
}
302+
303+
private fun assertAutoTranslationTriggered() {
304+
val job = entityManager.createQuery("from BatchJob bj", BatchJob::class.java).singleResult
305+
job.type.assert.isEqualTo(BatchJobType.AUTO_TRANSLATE)
306+
}
307+
283308
private fun assertXliffDataImported() {
284309
getTestKeyTranslations().find { it.language.tag == "de" }!!.text.assert.isEqualTo("Test cs")
285310
getTestKeyTranslations().find { it.language.tag == "en" }!!.text.assert.isEqualTo("Test en")

backend/data/src/main/kotlin/io/tolgee/component/autoTranslation/AutoTranslationEventHandler.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ class AutoTranslationEventHandler(
3838
return
3939
}
4040

41-
if (event.activityRevision.modifiedEntities.any { it.entityClass == Translation::class.simpleName }) {
42-
autoTranslationService.autoTranslateViaBatchJob(
43-
projectId = projectId,
44-
keyIds = keyIds,
45-
isBatch = true,
46-
baseLanguageId = baseLanguageId ?: return,
47-
isHiddenJob = event.isLowVolumeActivity(),
48-
)
49-
}
41+
autoTranslationService.autoTranslateViaBatchJob(
42+
projectId = projectId,
43+
keyIds = keyIds,
44+
isBatch = true,
45+
baseLanguageId = baseLanguageId ?: return,
46+
isHiddenJob = event.isLowVolumeActivity(),
47+
)
5048
}
5149

5250
private fun shouldRunTheOperation(): Boolean {
51+
if (event.activityRevision.modifiedEntities.none { it.entityClass == Translation::class.simpleName }) {
52+
return false
53+
}
54+
5355
val configs =
5456
autoTranslationService.getConfigs(
5557
entityManager.getReference(Project::class.java, projectId),

0 commit comments

Comments
 (0)