Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Error on translation to the same language.
MT Error doesn't end with 500 anymore, but only with logged error
- Loading branch information
Showing
10 changed files
with
366 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
...ta/src/main/kotlin/io/tolgee/component/machineTranslation/MtServiceManagerCachingProxy.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...c/main/kotlin/io/tolgee/component/machineTranslation/providers/AbstractMtValueProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.tolgee.component.machineTranslation.providers | ||
|
||
import io.tolgee.component.machineTranslation.LanguageTagConvertor | ||
import io.tolgee.component.machineTranslation.MtValueProvider | ||
|
||
abstract class AbstractMtValueProvider : MtValueProvider { | ||
abstract val supportedLanguages: Array<String> | ||
|
||
private val String.toSuitableTag: String? | ||
get() { | ||
return LanguageTagConvertor.findSuitableTag(supportedLanguages, this) | ||
} | ||
|
||
override fun translate(text: String, sourceLanguageTag: String, targetLanguageTag: String): String? { | ||
val suitableSourceTag = sourceLanguageTag.toSuitableTag | ||
val suitableTargetTag = targetLanguageTag.toSuitableTag | ||
|
||
if (suitableSourceTag.isNullOrEmpty() || suitableTargetTag.isNullOrEmpty()) { | ||
return null | ||
} | ||
|
||
if (suitableSourceTag == suitableTargetTag) { | ||
return text | ||
} | ||
|
||
return translateViaProvider(text, suitableSourceTag, suitableTargetTag) | ||
} | ||
|
||
override fun calculatePrice(text: String, sourceLanguageTag: String, targetLanguageTag: String): Int { | ||
val suitableSourceTag = sourceLanguageTag.toSuitableTag | ||
val suitableTargetTag = targetLanguageTag.toSuitableTag | ||
|
||
if (suitableSourceTag.isNullOrEmpty() || | ||
suitableTargetTag.isNullOrEmpty() || | ||
suitableSourceTag == suitableTargetTag | ||
) { | ||
return 0 | ||
} | ||
|
||
return calculateProviderPrice(text) | ||
} | ||
|
||
/** | ||
* Translates the text via provider. | ||
* All inputs are already checked. | ||
*/ | ||
protected abstract fun translateViaProvider(text: String, sourceTag: String, targetTag: String): String? | ||
|
||
/** | ||
* Calculates provider's credit price. | ||
* All inputs are already checked. | ||
*/ | ||
protected abstract fun calculateProviderPrice(text: String): Int | ||
} |
115 changes: 115 additions & 0 deletions
115
...ta/src/main/kotlin/io/tolgee/component/machineTranslation/providers/AwsMtValueProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package io.tolgee.component.machineTranslation.providers | ||
|
||
import com.amazonaws.services.translate.AmazonTranslate | ||
import com.amazonaws.services.translate.model.TranslateTextRequest | ||
import com.amazonaws.services.translate.model.TranslateTextResult | ||
import io.tolgee.configuration.tolgee.machineTranslation.AwsMachineTranslationProperties | ||
import org.springframework.beans.factory.config.ConfigurableBeanFactory | ||
import org.springframework.context.annotation.Scope | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) | ||
class AwsMtValueProvider( | ||
private val awsMachineTranslationProperties: AwsMachineTranslationProperties, | ||
private val amazonTranslate: AmazonTranslate? | ||
) : AbstractMtValueProvider() { | ||
override val isEnabled: Boolean | ||
get() = !awsMachineTranslationProperties.accessKey.isNullOrEmpty() && | ||
!awsMachineTranslationProperties.secretKey.isNullOrEmpty() | ||
|
||
override fun calculateProviderPrice(text: String): Int { | ||
return text.length * 100 | ||
} | ||
|
||
override fun translateViaProvider(text: String, sourceTag: String, targetTag: String): String? { | ||
val request: TranslateTextRequest = TranslateTextRequest() | ||
.withText(text) | ||
.withSourceLanguageCode(sourceTag) | ||
.withTargetLanguageCode(targetTag) | ||
val result: TranslateTextResult = translateService.translateText(request) | ||
return result.translatedText | ||
} | ||
|
||
private val translateService by lazy { | ||
amazonTranslate ?: throw IllegalStateException("AmazonTranslate is not injected") | ||
} | ||
|
||
override val supportedLanguages = arrayOf( | ||
"af", | ||
"sq", | ||
"am", | ||
"ar", | ||
"hy", | ||
"az", | ||
"bn", | ||
"bs", | ||
"bg", | ||
"ca", | ||
"zh", | ||
"zh-TW", | ||
"hr", | ||
"cs", | ||
"da ", | ||
"fa-AF", | ||
"nl ", | ||
"en", | ||
"et", | ||
"fa", | ||
"tl", | ||
"fi", | ||
"fr", | ||
"fr-CA", | ||
"ka", | ||
"de", | ||
"el", | ||
"gu", | ||
"ht", | ||
"ha", | ||
"he ", | ||
"hi", | ||
"hu", | ||
"is", | ||
"id ", | ||
"ga", | ||
"it", | ||
"ja", | ||
"kn", | ||
"kk", | ||
"ko", | ||
"lv", | ||
"lt", | ||
"mk", | ||
"ms", | ||
"ml", | ||
"mt", | ||
"mr", | ||
"mn", | ||
"no", | ||
"ps", | ||
"pl", | ||
"pt", | ||
"pt-PT", | ||
"pa", | ||
"ro", | ||
"ru", | ||
"sr", | ||
"si", | ||
"sk", | ||
"sl", | ||
"so", | ||
"es", | ||
"es-MX", | ||
"sw", | ||
"sv", | ||
"ta", | ||
"te", | ||
"th", | ||
"tr", | ||
"uk", | ||
"ur", | ||
"uz", | ||
"vi", | ||
"cy" | ||
) | ||
} |
Oops, something went wrong.