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: single-step-import lack of auto-translation #2446

Closed
Closed
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 @@ -2,6 +2,8 @@ package io.tolgee.service.dataImport

import io.sentry.Sentry
import io.tolgee.api.IImportSettings
import io.tolgee.batch.data.BatchTranslationTargetItem
import io.tolgee.batch.request.AutoTranslationRequest
import io.tolgee.component.CurrentDateProvider
import io.tolgee.component.fileStorage.FileStorage
import io.tolgee.component.reporting.BusinessEventPublisher
Expand All @@ -26,9 +28,11 @@ import io.tolgee.model.dataImport.ImportLanguage
import io.tolgee.model.dataImport.ImportTranslation
import io.tolgee.model.dataImport.issues.ImportFileIssue
import io.tolgee.model.dataImport.issues.ImportFileIssueParam
import io.tolgee.model.key.Key
import io.tolgee.model.views.ImportFileIssueView
import io.tolgee.model.views.ImportLanguageView
import io.tolgee.model.views.ImportTranslationView
import io.tolgee.repository.LanguageRepository
import io.tolgee.repository.dataImport.ImportFileRepository
import io.tolgee.repository.dataImport.ImportKeyRepository
import io.tolgee.repository.dataImport.ImportLanguageRepository
Expand All @@ -37,6 +41,7 @@ import io.tolgee.repository.dataImport.ImportTranslationRepository
import io.tolgee.repository.dataImport.issues.ImportFileIssueParamRepository
import io.tolgee.repository.dataImport.issues.ImportFileIssueRepository
import io.tolgee.service.dataImport.status.ImportApplicationStatus
import io.tolgee.service.translation.AutoTranslationService
import io.tolgee.util.getSafeNamespace
import jakarta.persistence.EntityManager
import org.springframework.context.ApplicationContext
Expand Down Expand Up @@ -73,6 +78,10 @@ class ImportService(
private val jdbcTemplate: JdbcTemplate,
@Lazy
private val importSettingsService: ImportSettingsService,
@Lazy
private val autoTranslationService: AutoTranslationService,
@Lazy
private val languageRepository: LanguageRepository,
) {
@Transactional
fun addFiles(
Expand Down Expand Up @@ -153,7 +162,7 @@ class ImportService(

entityManager.clear()

StoredDataImporter(
val keyEntitiesToSave = StoredDataImporter(
applicationContext,
import,
params.forceMode,
Expand All @@ -162,6 +171,40 @@ class ImportService(
_importDataManager = fileProcessor.importDataManager,
isSingleStepImport = true,
).doImport()

autoTranslateSingleStepImport(project, keyEntitiesToSave);
}

fun autoTranslateSingleStepImport(project : Project, keyEntitiesToSave : Collection<Key> ) {
// this doesn't have any effect:
// -> publishImportBusinessEvent(import.project.id, import.author.id)

// Determine if we should auto-translate at all.
// See: AutoTranslationEventHandler.shouldRunTheOperation()
val configs = autoTranslationService.getConfigs(project)
if (!configs.any { it.enableForImport }) return
if (!configs.any { it.usingPrimaryMtService } && !configs.any { it.usingTm }) return

// Find the project languages
// -> val importLanguages = project.languages # exception: NoSession
// -> val importLanguages = this.findLanguages(import) # empty list
val importLanguages = languageRepository.findAllByProjectId(project.id);

// Produce all combinations of imported-keys and target-languages
val autoTranslationRequest = AutoTranslationRequest()
autoTranslationRequest.target = keyEntitiesToSave.stream().flatMap { key ->
importLanguages.stream()
.filter { lang -> lang.id != project.baseLanguage?.id }
.map { lang -> BatchTranslationTargetItem(key.id, lang.id) }
}.toList();

if(autoTranslationRequest.target.isNotEmpty()) {
autoTranslationService.autoTranslateViaBatchJob(
project,
autoTranslationRequest,
false // only hidden for small jobs
)
}
}

@Transactional(noRollbackFor = [ImportConflictNotResolvedException::class])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class StoredDataImporter(
result
}

fun doImport() {
fun doImport() : Collection<Key> {
reportStatus(ImportApplicationStatus.PREPARING_AND_VALIDATING)
importDataManager.storedLanguages.forEach {
it.prepareImport()
Expand Down Expand Up @@ -136,6 +136,8 @@ class StoredDataImporter(
deleteOtherKeys()

entityManager.flushAndClear()

return keyEntitiesToSave
}

private fun tagNewKeys() {
Expand Down