translation fix, added error message for translation

This commit is contained in:
nuclearfog 2023-11-06 23:16:25 +01:00
parent a2b53d7527
commit 52e4d6f3e3
No known key found for this signature in database
GPG Key ID: 03488A185C476379
3 changed files with 24 additions and 10 deletions

View File

@ -83,7 +83,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -1366,7 +1365,7 @@ public class Mastodon implements Connection {
public Translation getStatusTranslation(long id) throws ConnectionException {
try {
List<String> params = new ArrayList<>();
params.add("lang=" + Locale.getDefault().getLanguage()); // set system language as destiny for translation
// params.add("lang=" + Locale.getDefault().getLanguage()); // set system language as destiny for translation
Response response = post(ENDPOINT_STATUS + id + "/translate", params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {

View File

@ -15,7 +15,7 @@ import org.nuclearfog.twidda.model.Translation;
*
* @author nuclearfog
*/
public class TranslationLoader extends AsyncExecutor<Long, TranslationLoader.Result> {
public class TranslationLoader extends AsyncExecutor<TranslationLoader.Param, TranslationLoader.Result> {
private Connection connection;
@ -28,11 +28,22 @@ public class TranslationLoader extends AsyncExecutor<Long, TranslationLoader.Res
@Override
protected Result doInBackground(@NonNull Long param) {
protected Result doInBackground(@NonNull Param param) {
try {
return new Result(connection.getStatusTranslation(param));
return new Result(connection.getStatusTranslation(param.id), null);
} catch (ConnectionException exception) {
return new Result(null);
return new Result(null, exception);
}
}
/**
*
*/
public static class Param {
final long id;
public Param(long id) {
this.id = id;
}
}
@ -42,10 +53,13 @@ public class TranslationLoader extends AsyncExecutor<Long, TranslationLoader.Res
public static class Result {
@Nullable
public Translation translation;
public final Translation translation;
@Nullable
public final ConnectionException exception;
Result(@Nullable Translation translation) {
Result(@Nullable Translation translation, @Nullable ConnectionException exception) {
this.translation = translation;
this.exception = exception;
}
}
}

View File

@ -597,7 +597,8 @@ public class StatusActivity extends AppCompatActivity implements OnClickListener
status_text.setText(spannableText);
translated = false;
} else if (translationLoader.isIdle()) {
translationLoader.execute(status.getId(), translationResult);
TranslationLoader.Param param = new TranslationLoader.Param(status.getId());
translationLoader.execute(param, translationResult);
}
}
}
@ -1099,7 +1100,7 @@ public class StatusActivity extends AppCompatActivity implements OnClickListener
translate_text.append(result.translation.getOriginalLanguage());
translated = true;
} else {
Toast.makeText(getApplicationContext(), R.string.error_translating_status, Toast.LENGTH_SHORT).show();
ErrorUtils.showErrorMessage(getApplicationContext(), result.exception);
}
}