Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Translations for Akkoma
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacocococo committed Nov 14, 2023
1 parent 0c376d5 commit 46c7fc0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.joinmastodon.android.api.requests.statuses;

import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.AkkomaTranslation;

public class AkkomaTranslateStatus extends MastodonAPIRequest<AkkomaTranslation>{
public AkkomaTranslateStatus(String id, String lang){
super(HttpMethod.GET, "/statuses/"+id+"/translations/"+lang.toUpperCase(), AkkomaTranslation.class);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships;
import org.joinmastodon.android.api.requests.polls.SubmitPollVote;
import org.joinmastodon.android.api.requests.statuses.AkkomaTranslateStatus;
import org.joinmastodon.android.api.requests.statuses.TranslateStatus;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.events.PollUpdatedEvent;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.AkkomaTranslation;
import org.joinmastodon.android.model.DisplayItemsParent;
import org.joinmastodon.android.model.Poll;
import org.joinmastodon.android.model.Relationship;
Expand Down Expand Up @@ -855,38 +857,69 @@ public void togglePostTranslation(Status status, String itemID){
status.translationState=Status.TranslationState.SHOWN;
}else{
status.translationState=Status.TranslationState.LOADING;
new TranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage())
.setCallback(new Callback<>(){
@Override
public void onSuccess(Translation result){
if(getActivity()==null)
return;
status.translation=result;
status.translationState=Status.TranslationState.SHOWN;
updateTranslation(itemID);
}
if(!isInstanceAkkoma()){
new TranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage())
.setCallback(new Callback<>(){
@Override
public void onSuccess(Translation result){
if(getActivity()==null)
return;
status.translation=result;
status.translationState=Status.TranslationState.SHOWN;
updateTranslation(itemID);
}

@Override
public void onError(ErrorResponse error){
if(getActivity()==null)
return;
translationCallbackError(status, itemID);
}
})
.setTimeout(60000) // 1 minute
.exec(accountID);
}else{
new AkkomaTranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage())
.setCallback(new Callback<>(){
@Override
public void onSuccess(AkkomaTranslation result){
if(getActivity()==null)
return;
status.translation=new Translation();
status.translation.content=result.text;
status.translation.detectedSourceLanguage=result.detectedLanguage;
status.translation.provider="";
status.translationState=Status.TranslationState.SHOWN;
updateTranslation(itemID);
}

@Override
public void onError(ErrorResponse error){
if(getActivity()==null)
return;
translationCallbackError(status, itemID);
}
})
.setTimeout(60000) // 1 minute
.exec(accountID);
}

@Override
public void onError(ErrorResponse error){
if(getActivity()==null)
return;
status.translationState=Status.TranslationState.HIDDEN;
updateTranslation(itemID);
new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.error)
.setMessage(R.string.translation_failed)
.setPositiveButton(R.string.ok, null)
.show();
}
})
.setTimeout(60000) // 1 minute
.exec(accountID);
}
}
}
updateTranslation(itemID);
}

private void translationCallbackError(Status status, String itemID) {
status.translationState=Status.TranslationState.HIDDEN;
updateTranslation(itemID);
new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.error)
.setMessage(R.string.translation_failed)
.setPositiveButton(R.string.ok, null)
.show();
}

private void updateTranslation(String itemID) {
TextStatusDisplayItem.Holder text=findHolderOfType(itemID, TextStatusDisplayItem.Holder.class);
if(text!=null){
Expand All @@ -896,6 +929,9 @@ private void updateTranslation(String itemID) {
notifyItemChanged(itemID, TextStatusDisplayItem.class);
}

if(isInstanceAkkoma())
return;

SpoilerStatusDisplayItem.Holder spoiler=findHolderOfType(itemID, SpoilerStatusDisplayItem.Holder.class);
if(spoiler!=null){
spoiler.rebind();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.joinmastodon.android.model;

public class AkkomaTranslation extends BaseModel{
public String detectedLanguage;
public String text;
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,15 @@ public boolean hasFeature(Feature feature) {
case BUBBLE_TIMELINE -> pleromaFeatures
.map(f -> f.contains("bubble_timeline"))
.orElse(false);
case MACHINE_TRANSLATION -> pleromaFeatures
.map(f -> f.contains("akkoma:machine_translation"))
.orElse(false);
};
}

public enum Feature {
BUBBLE_TIMELINE
BUBBLE_TIMELINE,
MACHINE_TRANSLATION
}

@Parcel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public boolean isEligibleForTranslation(AccountSession session){
Instance instanceInfo = AccountSessionManager.getInstance().getInstanceInfo(session.domain);
boolean translateEnabled = instanceInfo != null &&
instanceInfo.v2 != null && instanceInfo.v2.configuration.translation != null &&
instanceInfo.v2.configuration.translation.enabled;
instanceInfo.v2.configuration.translation.enabled ||
(instanceInfo != null && instanceInfo.isAkkoma() && instanceInfo.hasFeature(Instance.Feature.MACHINE_TRANSLATION));

try {
Pair<String, List<String>> decoded=BOTTOM_TEXT_PATTERN.matcher(getStrippedText()).find()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private CustomEmojiHelper getEmojiHelper(){
public void updateTranslation(boolean updateText){
if(item.status==null)
return;
boolean translateEnabled=!item.disableTranslate && item.status.isEligibleForTranslation(item.parentFragment.getSession());
boolean translateEnabled=!item.disableTranslate && item.status.isEligibleForTranslation(item.parentFragment.getSession()) && !item.isForQuote;
if(translationFooter==null && translateEnabled){
translationFooter=translationFooterStub.inflate();
translationInfo=findViewById(R.id.translation_info_text);
Expand Down

0 comments on commit 46c7fc0

Please sign in to comment.