fix(i18n): 메타데이터 조회와 요청에서 현재 원문의 실패 번역을 재개한다

This commit is contained in:
2026-11-16 21:47:54 +09:00
parent d1c9f3a3d8
commit 081f22b2c0
16 changed files with 846 additions and 6 deletions
@@ -186,6 +186,11 @@ class ChatCharacterController(
var translated: TranslatedAiCharacterDetail? = null
if (langContext.lang.code != character.languageCode) {
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.CHARACTER,
listOf(characterId),
langContext.lang.code
)
val existing = aiCharacterTranslationRepository
.findByCharacterIdAndLocale(character.id!!, langContext.lang.code)
@@ -28,8 +28,15 @@ class OriginalWorkTranslationService(
return null
}
val originalWorkId = requireNotNull(originalWork.id)
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.ORIGINAL_WORK,
listOf(originalWorkId),
target
)
// 기존 번역 조회
val existed = translationRepository.findByOriginalWorkIdAndLocale(originalWork.id!!, target)
val existed = translationRepository.findByOriginalWorkIdAndLocale(originalWorkId, target)
val existedPayload = existed?.renderedPayload
if (existedPayload != null) {
val t = existedPayload.title.trim()
@@ -51,7 +58,7 @@ class OriginalWorkTranslationService(
resourceTranslationJobScheduler.scheduleResourceTranslation(
resourceType = LanguageTranslationTargetType.ORIGINAL_WORK,
resourceId = originalWork.id!!,
resourceId = originalWorkId,
targetLanguage = target
)
return null
@@ -896,6 +896,11 @@ class AudioContentService(
audioContent.languageCode!!.isNotBlank() &&
audioContent.languageCode != langContext.lang.code
) {
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.CONTENT,
listOf(id),
langContext.lang.code
)
val existing = contentTranslationRepository
.findByContentIdAndLocale(audioContent.id!!, langContext.lang.code)
@@ -141,6 +141,11 @@ class CategoryService(
val categoryIds = baseList.map { it.categoryId }
val entities = repository.findAllById(categoryIds)
val entityMap = entities.associateBy { it.id!! }
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.CREATOR_CONTENT_CATEGORY,
categoryIds,
locale
)
// 요청 로케일로 이미 저장된 번역 일괄 조회
val translations = categoryTranslationRepository
@@ -98,6 +98,11 @@ class ContentSeriesService(
if (currentLang == Lang.EN || currentLang == Lang.JA) {
val targetLocale = currentLang.code
val ids = genres.map { it.id }
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.SERIES_GENRE,
ids,
targetLocale
)
// 기존 번역 일괄 조회
val existing = if (ids.isNotEmpty()) {
@@ -278,6 +283,11 @@ class ContentSeriesService(
val languageCode = series.languageCode
// 원본 언어가 존재하고, 요청 언어와 다를 때만 번역 처리
if (!languageCode.isNullOrBlank() && languageCode != locale) {
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.SERIES,
listOf(seriesId),
locale
)
val existing = seriesTranslationRepository.findBySeriesIdAndLocale(seriesId = seriesId, locale = locale)
if (existing != null) {
val payload = existing.renderedPayload
@@ -58,6 +58,11 @@ class AudioContentThemeService(
val targetLocale = currentLang.code
// 1) 기존 번역을 한 번에 조회
val ids = themesWithIds.map { it.id }
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.CONTENT_THEME,
ids,
targetLocale
)
val existingTranslations = if (ids.isNotEmpty()) {
contentThemeTranslationRepository.findByContentThemeIdInAndLocale(ids, targetLocale)
} else {
@@ -2,12 +2,15 @@ package kr.co.vividnext.sodalive.i18n.translation
import kr.co.vividnext.sodalive.i18n.translation.TranslationLanguages.getTranslatableLanguageCodes
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
@Service
class ResourceTranslationJobScheduler(
private val sourceExtractor: TranslationSourceExtractor,
private val translationJobScheduler: TranslationJobScheduler
private val translationJobScheduler: TranslationJobScheduler,
private val translationJobRepository: TranslationJobRepository
) {
@Transactional
fun scheduleResourceTranslations(resourceType: LanguageTranslationTargetType, resourceId: Long) {
@@ -27,6 +30,45 @@ class ResourceTranslationJobScheduler(
scheduleSource(source, targetLanguage)
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun restartFailedResourceTranslations(
resourceType: LanguageTranslationTargetType,
resourceIds: Collection<Long>,
targetLanguage: String
) {
restartFailedResourceTranslationsInCurrentTransaction(resourceType, resourceIds, targetLanguage)
}
@Transactional
fun restartFailedResourceTranslationsInCurrentTransaction(
resourceType: LanguageTranslationTargetType,
resourceIds: Collection<Long>,
targetLanguage: String
) {
if (resourceIds.isEmpty()) return
val target = targetLanguage.lowercase()
val candidates = translationJobRepository.findByResourceTypeAndResourceIdInAndTargetLanguageAndStatus(
resourceType,
resourceIds,
target,
TranslationJobStatus.FAILED
)
val now = LocalDateTime.now()
candidates.groupBy { it.resourceId }.forEach { (resourceId, failedJobs) ->
val source = sourceExtractor.extract(resourceType, resourceId) ?: return@forEach
val sourceLanguage = source.sourceLanguage.lowercase()
if (!TranslationLanguages.isSupported(sourceLanguage) || sourceLanguage == target) return@forEach
val sourceHashes = source.fields.associate { it.fieldKey to SourceTextNormalizer.hash(it.sourceText) }
failedJobs.filter { job ->
job.sourceLanguage == sourceLanguage && job.sourceHash == sourceHashes[job.fieldKey]
}.forEach { job ->
translationJobRepository.restartFailedJob(requireNotNull(job.id), now)
}
}
}
private fun scheduleSource(source: TranslationSource, targetLanguage: String) {
source.fields.forEach { field ->
translationJobScheduler.scheduleMissingTranslation(
@@ -35,6 +35,17 @@ class CreatorCommunityTranslationService(
val normalizedLocale = locale.lowercase()
val posts = creatorCommunityRepository.findAllById(postIds).filter { it.isActive }
val translationPostIds = posts.filter { post ->
val sourceLanguage = post.languageCode?.lowercase()
!sourceLanguage.isNullOrBlank() && sourceLanguage != normalizedLocale
}.mapNotNull { it.id }
if (translationPostIds.isNotEmpty()) {
resourceTranslationJobScheduler.restartFailedResourceTranslations(
LanguageTranslationTargetType.CREATOR_COMMUNITY,
translationPostIds,
normalizedLocale
)
}
val translations = creatorCommunityTranslationRepository
.findByCreatorCommunityIdInAndLocale(posts.mapNotNull { it.id }, normalizedLocale)
.associateBy { it.creatorCommunityId }
@@ -62,6 +73,11 @@ class CreatorCommunityTranslationService(
targetLanguages
.filter { it != sourceLanguage }
.forEach { language ->
resourceTranslationJobScheduler.restartFailedResourceTranslationsInCurrentTransaction(
LanguageTranslationTargetType.CREATOR_COMMUNITY,
listOf(postId),
language
)
if (!materializer.materialize(LanguageTranslationTargetType.CREATOR_COMMUNITY, postId, language)) {
resourceTranslationJobScheduler.scheduleResourceTranslation(
resourceType = LanguageTranslationTargetType.CREATOR_COMMUNITY,