Skip to content
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 @@ -104,11 +104,16 @@ class DeeplEngine : TranslationEngine(
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val convertedTarget = when(target){
"zh-Hant", "zh-Hans" -> "zh"
else -> target
}

if (getApiKey().isNotEmpty()) {
val response = api.translate(
apiKeyString,
sourceOrAuto(source.uppercase()),
target.uppercase(),
convertedTarget.uppercase(),
query
)

Expand All @@ -130,7 +135,7 @@ class DeeplEngine : TranslationEngine(
),
splitting = "newlines",
lang = DeeplWebTranslationRequestParamsLang(
targetLang = target.uppercase(),
targetLang = convertedTarget.uppercase(),
sourceLangUserSelected = sourceOrAuto(source.uppercase()).ifEmpty { "auto" },
preference = DeeplWebTranslationRequestParamsLangPreference(
weight = emptyMap()
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/bnyro/translate/api/gl/GlEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,15 @@ class GlEngine : TranslationEngine(
).map { Language(it.first.trim(), it.second) }
}

protected fun convertLanguageCode(languageCode: String): String {
return when(languageCode){
"zh-Hant", "zh-Hans" -> "zh"
else -> languageCode
}
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val translation = api.translate(source, target, query)
val translation = api.translate(convertLanguageCode(source), convertLanguageCode(target), query)
return Translation(translation.translation)
}
}
11 changes: 9 additions & 2 deletions app/src/main/java/com/bnyro/translate/api/lv/LVEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ class LVEngine : TranslationEngine(
}
}

protected fun convertLanguageCode(languageCode: String): String {
return when(languageCode){
"zh-Hant", "zh-Hans" -> "zh"
else -> languageCode
}
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val response = api.translate(
sourceOrAuto(source),
target,
convertLanguageCode(sourceOrAuto(source)),
convertLanguageCode(target),
query.replace("/", "")
)
return Translation(
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,20 @@ class MhEngine : TranslationEngine(
.map { Language(it.id, it.name) }
}

protected fun convertLanguageCode(languageCode: String): String {
return when(languageCode){
"zh-Hant" -> "zh-TW"
"zh-Hans" -> "zh-CN"
else -> languageCode
}
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val response = api.translate(
engine = getSelectedEngine(),
source = sourceOrAuto(source.take(2)),
source = convertLanguageCode(sourceOrAuto(source)),
query = query,
target = target.take(2),
target = convertLanguageCode(target),
)
return Translation(
translatedText = response.translatedText,
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/bnyro/translate/api/wm/WmEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ class WmEngine : TranslationEngine(
}
}

protected fun convertLanguageCode(languageCode: String): String {
return when(languageCode){
"zh-Hant", "zh-Hans" -> "zh"
else -> languageCode
}
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val response = api.translate(source, target, WmTranslationRequest(query))
val response = api.translate(convertLanguageCode(source), convertLanguageCode(target), WmTranslationRequest(query))
return Translation(response.translation)
}
}
11 changes: 10 additions & 1 deletion app/src/main/java/com/bnyro/translate/api/ya/YandexEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ class YandexEngine : TranslationEngine(
).map { (key, value) -> Language(code = key, name = value) }
}

protected fun convertLanguageCode(languageCode: String): String {
return when(languageCode){
"zh-Hant", "zh-Hans" -> "zh"
else -> languageCode
}
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val lang = if (source.isEmpty()) target else "$source-$target"
val convertedSource = convertLanguageCode(source)
val convertedTarget = convertLanguageCode(target)
val lang = if (convertedSource.isEmpty()) convertedTarget else "$convertedSource-$convertedTarget"

val uuid = UUID.randomUUID().toString().replace("-", "") + "-0-0"
val response = api.translate(lang, query, "android", uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,17 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) {
@Composable
fun SwapLanguagesButton(viewModel: TranslationModel) {
val context = LocalContext.current

val switchBtnEnabled = viewModel.sourceLanguage.code.isNotEmpty()

IconButton(
onClick = {
if (switchBtnEnabled) viewModel.swapLanguages(context)
viewModel.swapLanguages(context)
}
) {
Icon(
painterResource(R.drawable.ic_switch),
null,
modifier = Modifier
.size(18.dp),
tint = if (switchBtnEnabled) MaterialTheme.colorScheme.onSurface else Color.Gray
tint = MaterialTheme.colorScheme.onSurface
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ package com.bnyro.translate.ui.components
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.DoneAll
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.VolumeUp
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -43,11 +46,15 @@ import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bnyro.translate.R
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.ui.models.TranslationModel
import com.bnyro.translate.util.Preferences
import com.bnyro.translate.util.SpeechHelper
import com.bnyro.translate.util.TranslationEngine

@Composable
fun TranslationField(
Expand All @@ -57,6 +64,7 @@ fun TranslationField(
language: Language,
setLanguage: (Language) -> Unit = {},
showLanguageSelector: Boolean = false,
translationEngine: TranslationEngine = translationModel.engine,
onTextChange: (String) -> Unit = {}
) {
val context = LocalContext.current
Expand All @@ -70,8 +78,13 @@ fun TranslationField(
Preferences.get(Preferences.charCounterLimitKey, "")
}

val translationModelTranslation: Translation = translationModel
.translatedTexts[translationEngine.name]?:Translation("")
val inverseTranslationModelTranslation: Translation = translationModel
.inverseTranslatedTexts[translationEngine.name]?:Translation("")

AnimatedVisibility(
visible = text.isNotEmpty(),
visible = text.isNotEmpty() || !isSourceField, // 翻譯欄位總是顯示,但源欄位只在有內容時顯示按鈕
enter = expandVertically(),
exit = shrinkVertically(),
label = "text actions fade"
Expand All @@ -84,7 +97,7 @@ fun TranslationField(
LanguageSelector(
translationModel.availableLanguages,
language,
autoLanguageEnabled = translationModel.engine.autoLanguageCode != null && isSourceField,
autoLanguageEnabled = translationEngine.autoLanguageCode != null && isSourceField,
viewModel = translationModel,
useElevatedButton = false
) {
Expand All @@ -93,6 +106,19 @@ fun TranslationField(
}
}

if (!isSourceField){
Text(
modifier = Modifier.padding(
top = 20.dp,
bottom = 10.dp,
start = 15.dp
),
text = translationEngine.name,
color = MaterialTheme.colorScheme.primary,
fontSize = 14.sp
)
}

Spacer(modifier = Modifier.weight(1f))

if (isSourceField && showLanguageSelector) {
Expand Down Expand Up @@ -128,7 +154,7 @@ fun TranslationField(
}
)

if (translationModel.engine.supportsAudio && language.code.isNotEmpty()) {
if (translationEngine.supportsAudio && language.code.isNotEmpty()) {
StyledIconButton(
imageVector = Icons.Default.VolumeUp
) {
Expand All @@ -145,11 +171,11 @@ fun TranslationField(
}

StyledTextField(
text = text + (if (!isSourceField) "\n\n\n" else ""),
text = text + (if (!isSourceField) "" else ""),
placeholder = if (isSourceField) stringResource(R.string.enter_text) else null,
readOnly = !isSourceField,
textColor = if (
charPref.isNotEmpty() && translationModel.translation.translatedText.length >= charPref.toInt()
charPref.isNotEmpty() && translationModelTranslation.translatedText.length >= charPref.toInt()
) {
MaterialTheme.colorScheme.error
} else {
Expand All @@ -158,4 +184,17 @@ fun TranslationField(
) {
onTextChange(it)
}

if (!isSourceField){
Text(
modifier = Modifier.padding(
top = 0.dp,
bottom = 10.dp,
start = 15.dp
),
text = inverseTranslationModelTranslation.translatedText,
color = MaterialTheme.colorScheme.secondary,
fontSize = 12.sp
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import androidx.compose.runtime.mutableStateMapOf
import com.bnyro.translate.ext.toastFromMainThread

class TranslationModel : ViewModel() {
var engine by mutableStateOf(getCurrentEngine())
Expand All @@ -70,9 +73,9 @@ class TranslationModel : ViewModel() {

var translation by mutableStateOf(Translation(""))

var translatedTexts = TranslationEngines.engines
.associate { it.name to Translation("") }
.toMutableMap()
var translatedTexts = mutableStateMapOf<String, Translation>()

var inverseTranslatedTexts = mutableStateMapOf<String, Translation>()

var bookmarkedLanguages by mutableStateOf(listOf<Language>())

Expand All @@ -85,6 +88,15 @@ class TranslationModel : ViewModel() {
private var mediaPlayer: MediaPlayer? = null
private var audioFile: File? = null

fun detectedLanguageMostly(): String?{
val detectedLanguageList = translatedTexts.filter {
it.value.detectedLanguage != null
}
return detectedLanguageList.maxByOrNull {
it.value.detectedLanguage?:""
}?.value?.detectedLanguage
}

private fun getLanguageByPrefKey(key: String): Language? {
return runCatching {
JsonHelper.json.decodeFromString<Language>(Preferences.get(key, ""))
Expand Down Expand Up @@ -117,10 +129,8 @@ class TranslationModel : ViewModel() {

translating = true

translatedTexts = TranslationEngines.engines
.associate { it.name to Translation("") }
.toMutableMap()

translatedTexts.clear()
TranslationEngines.engines.forEach { translatedTexts[it.name] = Translation("") }
viewModelScope.launch(Dispatchers.IO) {
val translation = try {
engine.translate(
Expand All @@ -132,7 +142,7 @@ class TranslationModel : ViewModel() {
Log.e("error", e.message.toString())
_apiError.emit(e)
translating = false
return@launch
Translation("")
}
translating = false

Expand Down Expand Up @@ -160,6 +170,19 @@ class TranslationModel : ViewModel() {
sourceLanguage.code,
targetLanguage.code
)
//if sourceLanguage "auto" use detectedLanguage
val sourceLanguageCode = if(sourceLanguage.code == ""){
val detectedLanguage = translatedTexts[it.name]?.detectedLanguage
if (detectedLanguage == "" || detectedLanguage == null){
detectedLanguageMostly()
}else{ detectedLanguage }?:""
} else { sourceLanguage.code }

inverseTranslatedTexts[it.name] = it.translate(
translatedTexts[it.name]?.translatedText?:return@runCatching,
targetLanguage.code,
sourceLanguageCode
)
}
}
}
Expand Down Expand Up @@ -281,7 +304,10 @@ class TranslationModel : ViewModel() {
fun swapLanguages(context: Context) {
if (availableLanguages.isEmpty()) return

val temp = sourceLanguage
val temp = if(sourceLanguage.code == ""){
availableLanguages.find { it.code == detectedLanguageMostly() }
?:Language("en", "English")
} else { sourceLanguage }
sourceLanguage = targetLanguage
targetLanguage = temp

Expand Down
Loading
Loading