Use language of messages for translations with MinT

This commit is contained in:
Thomas 2025-03-28 09:24:48 +01:00
parent fec6b8510e
commit 4387cac67f
6 changed files with 16 additions and 22 deletions

View file

@ -190,7 +190,7 @@ public class MediaActivity extends BaseTransparentActivity implements OnDownload
binding.mediaDescription.setText(description);
binding.translate.setOnClickListener(v -> {
String descriptionToTranslate = attachments.get(mediaPosition - 1).description;
TranslateHelper.translate(MediaActivity.this, descriptionToTranslate, translated -> {
TranslateHelper.translate(MediaActivity.this, descriptionToTranslate, status.language, translated -> {
if (translated != null) {
attachments.get(mediaPosition - 1).translation = translated;
binding.mediaDescriptionTranslated.setText(translated);
@ -239,7 +239,7 @@ public class MediaActivity extends BaseTransparentActivity implements OnDownload
}
binding.translate.setOnClickListener(v -> {
String descriptionToTranslate = attachments.get(position).description;
TranslateHelper.translate(MediaActivity.this, descriptionToTranslate, translated -> {
TranslateHelper.translate(MediaActivity.this, descriptionToTranslate, status.language, translated -> {
if (translated != null) {
attachments.get(position).translation = translated;
binding.mediaDescriptionTranslated.setText(translated);

View file

@ -38,7 +38,7 @@ import es.dmoral.toasty.Toasty;
public class TranslateHelper {
public static void translate(Context context, String toTranslate, Translate callback) {
public static void translate(Context context, String toTranslate, String languageSource, Translate callback) {
String statusToTranslate;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
statusToTranslate = Html.fromHtml(toTranslate, Html.FROM_HTML_MODE_LEGACY).toString();
@ -61,7 +61,15 @@ public class TranslateHelper {
Params params = new Params();
params.setSplit_sentences(false);
params.setFormat(Params.fType.TEXT);
params.setSource_lang("auto");
if(et != MyTransL.translatorEngine.MINT) {
params.setSource_lang("auto");
} else {
if(languageSource != null && !languageSource.isEmpty()) {
params.setSource_lang(languageSource);
} else {
params.setSource_lang("en");
}
}
if (translator.compareToIgnoreCase("FEDILAB") == 0) {
myTransL.setLibretranslateDomain("translate.fedilab.app");
} else if (translator.compareToIgnoreCase("LINGVA") == 0) {

View file

@ -74,7 +74,7 @@ public class ReleaseNoteAdapter extends RecyclerView.Adapter<ReleaseNoteAdapter.
holder.binding.containerTrans.setVisibility(View.GONE);
holder.binding.translate.setVisibility(View.VISIBLE);
}
holder.binding.translate.setOnClickListener(v -> TranslateHelper.translate(context, note.note, translated -> {
holder.binding.translate.setOnClickListener(v -> TranslateHelper.translate(context, note.note, "en", translated -> {
if (translated != null) {
note.noteTranslated = translated;
notifyItemChanged(holder.getBindingAdapterPosition());

View file

@ -2765,7 +2765,7 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
statusToTranslate = Html.fromHtml(statusToDeal.content).toString();
int countMorseChar = ComposeAdapter.countMorseChar(statusToTranslate);
if (countMorseChar < 4) {
TranslateHelper.translate(context, statusToDeal.content, translated -> {
TranslateHelper.translate(context, statusToDeal.content, statusToDeal.language, translated -> {
if (translated != null) {
statusToDeal.translationShown = true;
statusToDeal.translationContent = translated;

View file

@ -29,6 +29,5 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation("com.github.pemistahl:lingua:1.2.2")
implementation "com.google.code.gson:gson:2.10.1"
}

View file

@ -17,9 +17,6 @@ package com.github.stom79.mytransl.async;
import android.os.Handler;
import android.os.Looper;
import com.github.pemistahl.lingua.api.Language;
import com.github.pemistahl.lingua.api.LanguageDetector;
import com.github.pemistahl.lingua.api.LanguageDetectorBuilder;
import com.github.stom79.mytransl.MyTransL;
import com.github.stom79.mytransl.client.Client;
import com.github.stom79.mytransl.client.HttpsConnectionException;
@ -35,7 +32,6 @@ import java.io.IOException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.SortedMap;
/**
@ -157,19 +153,10 @@ public class TransAsync {
str_response = new Client().get(lingvaURL, this.timeout);
} else if(te == MyTransL.translatorEngine.MINT) {
JSONObject params = new JSONObject();
LanguageDetector languageDetector = LanguageDetectorBuilder.fromAllLanguages().withMinimumRelativeDistance(0.25).build();
SortedMap<Language, Double> languages = languageDetector.computeLanguageConfidenceValues(contentToSend);
String fromLanguage = null;
if(!languages.isEmpty()) {
Language language = languages.firstKey();
fromLanguage = language.getIsoCode639_1().name();
}
if(fromLanguage == null) {
fromLanguage = "en";
}
try {
params.put("source_language", fromLanguage.toLowerCase());
params.put("source_language", this.params.getSource_lang());
params.put("target_language", toLanguage);
params.put("content", contentToSend);
params.put("format", "text");