feat(content): 관리자 번역 후보 선택과 필드별 승인 보호를 구현한다
This commit is contained in:
@@ -4,29 +4,48 @@ import kr.co.vividnext.sodalive.i18n.translation.DetectedLanguage
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageDetectionProvider
|
||||
import kr.co.vividnext.sodalive.i18n.translation.LanguageProviderProperties
|
||||
import kr.co.vividnext.sodalive.i18n.translation.SourceTextNormalizer
|
||||
import kr.co.vividnext.sodalive.i18n.translation.TranslationCallControl
|
||||
import kr.co.vividnext.sodalive.i18n.translation.translationCallStop
|
||||
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
|
||||
|
||||
@Service
|
||||
class LanguageDetectionCacheService(
|
||||
private val languageDetectionResultRepository: LanguageDetectionResultRepository,
|
||||
providers: List<LanguageDetectionProvider>,
|
||||
properties: LanguageProviderProperties
|
||||
properties: LanguageProviderProperties,
|
||||
transactionManager: PlatformTransactionManager
|
||||
) {
|
||||
private val providers = properties.orderedProviders(properties.detectionOrder, providers) { it.providerId }
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun detectWithCache(query: String): DetectedLanguage? {
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectWithCache(query: String): DetectedLanguage? = detect(query, null)
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun detectWithCache(query: String, authorizeDispatch: () -> Boolean): DetectedLanguage? {
|
||||
return detect(query, TranslationCallControl(authorizeDispatch))
|
||||
}
|
||||
|
||||
private fun detect(query: String, control: TranslationCallControl?): DetectedLanguage? {
|
||||
val normalizedQuery = SourceTextNormalizer.normalize(query)
|
||||
if (normalizedQuery.isBlank()) return null
|
||||
|
||||
val sourceHash = SourceTextNormalizer.hash(normalizedQuery)
|
||||
providers.forEach { provider ->
|
||||
val cached = languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
val cached = transaction.execute {
|
||||
languageDetectionResultRepository.findBySourceHashAndProviderAndNormalizationVersion(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
}
|
||||
if (cached != null && cached.detectedLanguage.isNotBlank()) {
|
||||
return DetectedLanguage(cached.detectedLanguage, provider.providerId)
|
||||
}
|
||||
@@ -34,26 +53,30 @@ class LanguageDetectionCacheService(
|
||||
|
||||
providers.forEach { provider ->
|
||||
val detectedLanguage = try {
|
||||
provider.detect(query)
|
||||
} catch (_: Exception) {
|
||||
control?.beforeCall()
|
||||
if (control == null) provider.detect(query) else provider.detect(query, control)
|
||||
} catch (error: Exception) {
|
||||
if (error.translationCallStop() != null) return null
|
||||
null
|
||||
}?.takeIf { it.isNotBlank() }?.lowercase() ?: return@forEach
|
||||
languageDetectionResultRepository.insertIfAbsent(
|
||||
sourceHash = sourceHash,
|
||||
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
||||
detectedLanguage = detectedLanguage,
|
||||
provider = provider.providerId.code,
|
||||
confidence = null,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
val winner = requireNotNull(
|
||||
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
||||
val winner = transaction.execute {
|
||||
languageDetectionResultRepository.insertIfAbsent(
|
||||
sourceHash = sourceHash,
|
||||
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
|
||||
detectedLanguage = detectedLanguage,
|
||||
provider = provider.providerId.code,
|
||||
confidence = null,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
)
|
||||
return DetectedLanguage(winner, provider.providerId)
|
||||
requireNotNull(
|
||||
languageDetectionResultRepository.findDetectedLanguageForUpdate(
|
||||
sourceHash = sourceHash,
|
||||
provider = provider.providerId.code,
|
||||
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
|
||||
)
|
||||
)
|
||||
}
|
||||
return DetectedLanguage(requireNotNull(winner), provider.providerId)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user