feat(content): 상세 조회에서 번역을 동기로 완료해 반환한다
This commit is contained in:
+144
@@ -0,0 +1,144 @@
|
||||
package kr.co.vividnext.sodalive.content
|
||||
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationPayload
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.translation.TranslatedContent
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationMemoryService
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationReadModelMaterializer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationSourceField
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
data class AudioContentDetailTranslationResult(
|
||||
val languageCode: String?,
|
||||
val translated: TranslatedContent? = null,
|
||||
val detectionQuery: String? = null
|
||||
)
|
||||
|
||||
@Service
|
||||
class AudioContentDetailTranslationService(
|
||||
private val repository: AudioContentRepository,
|
||||
private val translations: ContentTranslationRepository,
|
||||
private val detector: LanguageDetectionCacheService,
|
||||
private val memories: TranslationMemoryService,
|
||||
private val materializer: TranslationReadModelMaterializer,
|
||||
private val scheduler: ResourceTranslationJobScheduler,
|
||||
private val entityManager: EntityManager,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun resolve(contentId: Long, targetLanguage: String): AudioContentDetailTranslationResult {
|
||||
var source = transaction.execute { snapshot(contentId) } ?: return AudioContentDetailTranslationResult(null)
|
||||
if (source.languageCode.isNullOrBlank()) {
|
||||
val beforeDetection = source
|
||||
try {
|
||||
val detected = detector.detectWithCache(source.query)?.detectedLanguage
|
||||
?: return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
source = transaction.execute {
|
||||
val content = entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
||||
?: return@execute null
|
||||
val current = snapshot(contentId) ?: return@execute null
|
||||
if (current == beforeDetection) content.languageCode = detected
|
||||
snapshot(contentId)
|
||||
} ?: return AudioContentDetailTranslationResult(null)
|
||||
} catch (ex: Exception) {
|
||||
log.warn("Content detail language detection failed. contentId={}, error={}", contentId, ex.message)
|
||||
return AudioContentDetailTranslationResult(source.languageCode, detectionQuery = source.query)
|
||||
}
|
||||
}
|
||||
val language = source.languageCode
|
||||
if (language.isNullOrBlank()) return AudioContentDetailTranslationResult(language, detectionQuery = source.query)
|
||||
val target = targetLanguage.lowercase()
|
||||
if (
|
||||
!TranslationLanguages.isSupported(language) || !TranslationLanguages.isSupported(target) ||
|
||||
language.lowercase() == target
|
||||
) {
|
||||
return AudioContentDetailTranslationResult(language)
|
||||
}
|
||||
|
||||
val existing = transaction.execute { translations.findByContentIdAndLocale(contentId, target)?.renderedPayload }
|
||||
if (existing != null) {
|
||||
maintainRetries(contentId, target, scheduleMissing = false)
|
||||
return result(language, existing)
|
||||
}
|
||||
|
||||
return try {
|
||||
source.fields.forEach { field ->
|
||||
memories.ensureMemory(LanguageTranslationTargetType.CONTENT, field.fieldKey, field.sourceText, language, target)
|
||||
}
|
||||
val payload = transaction.execute {
|
||||
entityManager.find(AudioContent::class.java, contentId, LockModeType.PESSIMISTIC_WRITE)
|
||||
check(snapshot(contentId) == source) { "content translation source changed" }
|
||||
check(materializer.materialize(LanguageTranslationTargetType.CONTENT, contentId, target)) {
|
||||
"content translation is incomplete"
|
||||
}
|
||||
requireNotNull(translations.findByContentIdAndLocale(contentId, target)).renderedPayload
|
||||
}
|
||||
maintainRetries(contentId, target, scheduleMissing = false)
|
||||
result(language, requireNotNull(payload))
|
||||
} catch (ex: Exception) {
|
||||
log.warn("Content detail translation failed. contentId={}, locale={}, error={}", contentId, target, ex.message)
|
||||
maintainRetries(contentId, target, scheduleMissing = true)
|
||||
AudioContentDetailTranslationResult(language)
|
||||
}
|
||||
}
|
||||
|
||||
private fun maintainRetries(contentId: Long, target: String, scheduleMissing: Boolean) {
|
||||
try {
|
||||
transaction.executeWithoutResult {
|
||||
scheduler.restartFailedResourceTranslationsInCurrentTransaction(
|
||||
LanguageTranslationTargetType.CONTENT,
|
||||
listOf(contentId),
|
||||
target
|
||||
)
|
||||
if (scheduleMissing) {
|
||||
scheduler.scheduleResourceTranslation(LanguageTranslationTargetType.CONTENT, contentId, target)
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
log.warn(
|
||||
"Content detail translation retry request failed. contentId={}, locale={}, error={}",
|
||||
contentId,
|
||||
target,
|
||||
ex.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun result(language: String, payload: ContentTranslationPayload) = AudioContentDetailTranslationResult(
|
||||
language,
|
||||
TranslatedContent(payload.title, payload.detail, payload.tags)
|
||||
)
|
||||
|
||||
private fun snapshot(contentId: Long): DetailSource? {
|
||||
val content = repository.findById(contentId).orElse(null) ?: return null
|
||||
val tags = content.audioContentHashTags.filter { it.isActive }.mapNotNull { it.hashTag?.tag }
|
||||
return DetailSource(
|
||||
content.languageCode,
|
||||
listOf(
|
||||
TranslationSourceField("title", content.title),
|
||||
TranslationSourceField("detail", content.detail),
|
||||
TranslationSourceField("tags", tags.joinToString(","))
|
||||
),
|
||||
listOf(content.title.trim(), content.detail.trim(), tags.joinToString(" ").trim())
|
||||
.filter { it.isNotBlank() }.joinToString(" ")
|
||||
)
|
||||
}
|
||||
|
||||
private data class DetailSource(val languageCode: String?, val fields: List<TranslationSourceField>, val query: String)
|
||||
}
|
||||
@@ -23,7 +23,6 @@ import kr.co.vividnext.sodalive.content.pin.PinContentRepository
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentThemeQueryRepository
|
||||
import kr.co.vividnext.sodalive.content.theme.translation.ContentThemeTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.translation.ContentTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.translation.TranslatedContent
|
||||
import kr.co.vividnext.sodalive.explorer.ExplorerQueryRepository
|
||||
import kr.co.vividnext.sodalive.extensions.convertLocalDateTime
|
||||
import kr.co.vividnext.sodalive.fcm.FcmDeepLinkValue
|
||||
@@ -34,7 +33,6 @@ import kr.co.vividnext.sodalive.i18n.LangContext
|
||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationEvent
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageTranslationTargetType
|
||||
import kr.co.vividnext.sodalive.i18n.translation.ResourceTranslationJobScheduler
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import kr.co.vividnext.sodalive.member.block.BlockMemberRepository
|
||||
@@ -83,8 +81,8 @@ class AudioContentService(
|
||||
private val pinContentRepository: PinContentRepository,
|
||||
private val memberRepository: MemberRepository,
|
||||
|
||||
private val resourceTranslationJobScheduler: ResourceTranslationJobScheduler,
|
||||
private val contentTranslationRepository: ContentTranslationRepository,
|
||||
private val detailTranslationService: AudioContentDetailTranslationService,
|
||||
|
||||
private val s3Uploader: S3Uploader,
|
||||
private val objectMapper: ObjectMapper,
|
||||
@@ -875,54 +873,14 @@ class AudioContentService(
|
||||
listOf()
|
||||
}
|
||||
|
||||
var translated: TranslatedContent? = null
|
||||
|
||||
/**
|
||||
* audioContent.languageCode != languageCode
|
||||
*
|
||||
* 번역 콘텐츠를 조회한다. - contentId, locale
|
||||
* 번역 콘텐츠가 있으면
|
||||
* TranslatedContent로 가공한다
|
||||
*
|
||||
* 번역 콘텐츠가 없으면
|
||||
* 누락 번역 job을 예약하고 원문 응답을 유지한다.
|
||||
*
|
||||
* 번역 대상: title, detail, tags
|
||||
*
|
||||
* 파파고로 번역한 데이터를 TranslatedContent로 가공한다
|
||||
*/
|
||||
if (audioContent.languageCode.isNullOrBlank()) {
|
||||
val translation = detailTranslationService.resolve(id, langContext.lang.code)
|
||||
if (translation.detectionQuery != null) {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageDetectEvent(
|
||||
id = id,
|
||||
query = listOf(audioContent.title.trim(), audioContent.detail.trim(), tag.trim())
|
||||
.filter { it.isNotBlank() }
|
||||
.joinToString(" ")
|
||||
query = translation.detectionQuery
|
||||
)
|
||||
)
|
||||
} else if (audioContent.languageCode != langContext.lang.code) {
|
||||
resourceTranslationJobScheduler.restartFailedResourceTranslations(
|
||||
LanguageTranslationTargetType.CONTENT,
|
||||
listOf(id),
|
||||
langContext.lang.code
|
||||
)
|
||||
val existing = contentTranslationRepository
|
||||
.findByContentIdAndLocale(audioContent.id!!, langContext.lang.code)
|
||||
|
||||
if (existing != null) {
|
||||
val payload = existing.renderedPayload
|
||||
translated = TranslatedContent(
|
||||
title = payload.title,
|
||||
detail = payload.detail,
|
||||
tags = payload.tags
|
||||
)
|
||||
} else {
|
||||
resourceTranslationJobScheduler.scheduleResourceTranslation(
|
||||
resourceType = LanguageTranslationTargetType.CONTENT,
|
||||
resourceId = audioContent.id!!,
|
||||
targetLanguage = langContext.lang.code
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -964,7 +922,7 @@ class AudioContentService(
|
||||
contentId = audioContent.id!!,
|
||||
title = audioContent.title,
|
||||
detail = contentDetail,
|
||||
languageCode = audioContent.languageCode,
|
||||
languageCode = translation.languageCode,
|
||||
coverImageUrl = "$coverImageHost/${audioContent.coverImage!!}",
|
||||
contentUrl = audioContentUrl,
|
||||
themeStr = themeStrTranslated,
|
||||
@@ -1009,7 +967,7 @@ class AudioContentService(
|
||||
nextContent = nextContent,
|
||||
buyerList = buyerList,
|
||||
isAvailableUsePoint = audioContent.isPointAvailable && audioContent.price > 0,
|
||||
translated = translated,
|
||||
translated = translation.translated,
|
||||
subtitle = if (
|
||||
(isExistsAudioContent || creatorId == member.id || audioContent.price <= 0) &&
|
||||
(audioContent.releaseDate == null || audioContent.releaseDate!! <= LocalDateTime.now() || creatorId == member.id)
|
||||
|
||||
+11
@@ -1,9 +1,20 @@
|
||||
package kr.co.vividnext.sodalive.content.translation
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Lock
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.query.Param
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
interface ContentTranslationRepository : JpaRepository<ContentTranslation, Long> {
|
||||
fun findByContentIdAndLocale(contentId: Long, locale: String): ContentTranslation?
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select t from ContentTranslation t where t.contentId = :contentId and t.locale = :locale")
|
||||
fun findByContentIdAndLocaleForUpdate(
|
||||
@Param("contentId") contentId: Long,
|
||||
@Param("locale") locale: String
|
||||
): ContentTranslation?
|
||||
|
||||
fun findByContentIdInAndLocale(contentIds: List<Long>, locale: String): List<ContentTranslation>
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ import java.time.LocalDateTime
|
||||
@Component
|
||||
class TranslationJobWorker(
|
||||
private val translationJobRepository: TranslationJobRepository,
|
||||
private val translationMemoryRepository: TranslationMemoryRepository,
|
||||
private val translationCoordinator: TranslationCoordinator,
|
||||
private val translationMemoryService: TranslationMemoryService,
|
||||
private val materializer: TranslationReadModelMaterializer,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
@@ -34,7 +33,13 @@ class TranslationJobWorker(
|
||||
val failedJobs = mutableListOf<Pair<TranslationJob, Exception>>()
|
||||
jobs.forEach { job ->
|
||||
try {
|
||||
ensureMemory(job)
|
||||
translationMemoryService.ensureMemory(
|
||||
job.resourceType,
|
||||
job.fieldKey,
|
||||
job.sourceText,
|
||||
job.sourceLanguage,
|
||||
job.targetLanguage
|
||||
)
|
||||
succeededJobs.add(job)
|
||||
} catch (ex: Exception) {
|
||||
failedJobs.add(job to ex)
|
||||
@@ -68,57 +73,6 @@ class TranslationJobWorker(
|
||||
}.orEmpty()
|
||||
}
|
||||
|
||||
private fun ensureMemory(job: TranslationJob) {
|
||||
val rejectUnchangedAwsResult = job.resourceType == LanguageTranslationTargetType.CONTENT && job.fieldKey == "tags"
|
||||
val existing = translationCoordinator.findCached(
|
||||
job.sourceHash,
|
||||
job.sourceLanguage,
|
||||
job.targetLanguage,
|
||||
rejectUnchangedAwsResult
|
||||
)
|
||||
if (existing != null) return
|
||||
|
||||
val response = translationCoordinator.translate(
|
||||
TranslateRequest(
|
||||
texts = listOf(job.sourceText),
|
||||
sourceLanguage = job.sourceLanguage,
|
||||
targetLanguage = job.targetLanguage,
|
||||
rejectUnchangedAwsResult = rejectUnchangedAwsResult
|
||||
)
|
||||
)
|
||||
val translated = response.result.translatedText.single()
|
||||
|
||||
transactionTemplate.executeWithoutResult {
|
||||
val memory = translationMemoryRepository
|
||||
.findBySourceHashAndSourceLanguageAndTargetLanguageAndProviderAndNormalizationVersion(
|
||||
sourceHash = job.sourceHash,
|
||||
sourceLanguage = job.sourceLanguage,
|
||||
targetLanguage = job.targetLanguage,
|
||||
provider = response.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
if (memory == null) {
|
||||
translationMemoryRepository.save(
|
||||
TranslationMemory(
|
||||
sourceHash = job.sourceHash,
|
||||
sourceText = job.sourceText,
|
||||
sourceLanguage = job.sourceLanguage,
|
||||
targetLanguage = job.targetLanguage,
|
||||
translatedText = translated,
|
||||
provider = response.providerId.code,
|
||||
providerVersion = response.providerVersion
|
||||
)
|
||||
)
|
||||
} else if (
|
||||
rejectUnchangedAwsResult && response.providerId == LanguageProviderId.AWS &&
|
||||
SourceTextNormalizer.hasSameCommaSeparatedText(memory.sourceText, memory.translatedText)
|
||||
) {
|
||||
memory.translatedText = translated
|
||||
translationMemoryRepository.save(memory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun completeJob(jobId: Long) {
|
||||
transactionTemplate.executeWithoutResult {
|
||||
val job = translationJobRepository.findById(jobId).orElse(null) ?: return@executeWithoutResult
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package kr.co.vividnext.sodalive.i18n.translation
|
||||
|
||||
import org.hibernate.exception.ConstraintViolationException
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import java.sql.SQLException
|
||||
|
||||
@Service
|
||||
class TranslationMemoryService(
|
||||
private val translationMemoryRepository: TranslationMemoryRepository,
|
||||
private val translationCoordinator: TranslationCoordinator,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun ensureMemory(
|
||||
resourceType: LanguageTranslationTargetType,
|
||||
fieldKey: String,
|
||||
sourceText: String,
|
||||
sourceLanguage: String,
|
||||
targetLanguage: String
|
||||
) {
|
||||
val text = SourceTextNormalizer.normalize(sourceText)
|
||||
if (text.isBlank()) return
|
||||
val sourceHash = SourceTextNormalizer.hash(text)
|
||||
val source = sourceLanguage.lowercase()
|
||||
val target = targetLanguage.lowercase()
|
||||
val rejectUnchangedAwsResult = resourceType == LanguageTranslationTargetType.CONTENT && fieldKey == "tags"
|
||||
val existing = transaction.execute {
|
||||
translationCoordinator.findCached(sourceHash, source, target, rejectUnchangedAwsResult)
|
||||
}
|
||||
if (existing != null) return
|
||||
|
||||
val response = translationCoordinator.translate(
|
||||
TranslateRequest(listOf(text), source, target, rejectUnchangedAwsResult)
|
||||
)
|
||||
val translated = response.result.translatedText.single()
|
||||
try {
|
||||
transaction.executeWithoutResult {
|
||||
val memory = translationMemoryRepository
|
||||
.findBySourceHashAndSourceLanguageAndTargetLanguageAndProviderAndNormalizationVersion(
|
||||
sourceHash,
|
||||
source,
|
||||
target,
|
||||
response.providerId.code,
|
||||
SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
if (memory == null) {
|
||||
translationMemoryRepository.save(
|
||||
TranslationMemory(
|
||||
sourceHash,
|
||||
text,
|
||||
source,
|
||||
target,
|
||||
translated,
|
||||
response.providerId.code,
|
||||
response.providerVersion
|
||||
)
|
||||
)
|
||||
} else if (
|
||||
rejectUnchangedAwsResult && response.providerId == LanguageProviderId.AWS &&
|
||||
SourceTextNormalizer.hasSameCommaSeparatedText(memory.sourceText, memory.translatedText)
|
||||
) {
|
||||
memory.translatedText = translated
|
||||
translationMemoryRepository.save(memory)
|
||||
}
|
||||
}
|
||||
} catch (ex: DataIntegrityViolationException) {
|
||||
val constraint = generateSequence<Throwable>(ex) { it.cause }
|
||||
.filterIsInstance<ConstraintViolationException>().firstOrNull()?.constraintName
|
||||
if (constraint?.contains("uk_translation_memory_source_target_provider", ignoreCase = true) != true) throw ex
|
||||
val sql = generateSequence<Throwable>(ex) { it.cause }.filterIsInstance<SQLException>().firstOrNull()
|
||||
if (sql?.sqlState != "23505" && sql?.errorCode != 1062) throw ex
|
||||
val winner = transaction.execute {
|
||||
translationCoordinator.findCached(sourceHash, source, target, rejectUnchangedAwsResult)
|
||||
}
|
||||
if (winner == null) throw ex
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -6,6 +6,7 @@ import kr.co.vividnext.sodalive.chat.character.translate.AiCharacterTranslationR
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslation
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationPayload
|
||||
import kr.co.vividnext.sodalive.chat.original.translation.OriginalWorkTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslation
|
||||
import kr.co.vividnext.sodalive.content.category.CategoryTranslationRepository
|
||||
import kr.co.vividnext.sodalive.content.series.translation.SeriesGenreTranslation
|
||||
@@ -90,7 +91,8 @@ class TranslationReadModelMaterializer(
|
||||
detail = translations["detail"].orEmpty(),
|
||||
tags = translations["tags"].orEmpty()
|
||||
)
|
||||
val existing = contentTranslationRepository.findByContentIdAndLocale(resourceId, targetLanguage)
|
||||
entityManager.find(AudioContent::class.java, resourceId, LockModeType.PESSIMISTIC_WRITE)
|
||||
val existing = contentTranslationRepository.findByContentIdAndLocaleForUpdate(resourceId, targetLanguage)
|
||||
if (existing == null) {
|
||||
contentTranslationRepository.save(ContentTranslation(resourceId, targetLanguage, payload))
|
||||
} else {
|
||||
|
||||
+1
@@ -52,6 +52,7 @@ class TranslationSourceExtractor(
|
||||
val content = audioContentRepository.findByIdOrNull(resourceId) ?: return null
|
||||
val sourceLanguage = content.languageCode?.takeIf { it.isNotBlank() } ?: return null
|
||||
val tags = content.audioContentHashTags
|
||||
.filter { it.isActive }
|
||||
.mapNotNull { it.hashTag?.tag }
|
||||
.joinToString(",")
|
||||
return TranslationSource(
|
||||
|
||||
Reference in New Issue
Block a user