Avoids to translate URLs and TAGs
This commit is contained in:
parent
874f482f02
commit
907235878b
|
@ -33,6 +33,7 @@ import android.text.Html;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.method.LinkMovementMethod;
|
import android.text.method.LinkMovementMethod;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.Patterns;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
@ -63,7 +64,11 @@ import java.io.File;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import fr.gouv.etalab.mastodon.activities.MediaActivity;
|
import fr.gouv.etalab.mastodon.activities.MediaActivity;
|
||||||
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
||||||
|
@ -104,6 +109,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
||||||
private final int FAVOURITE = 2;
|
private final int FAVOURITE = 2;
|
||||||
private RetrieveFeedsAsyncTask.Type type;
|
private RetrieveFeedsAsyncTask.Type type;
|
||||||
private String targetedId;
|
private String targetedId;
|
||||||
|
private HashMap<String, String> urlConversion;
|
||||||
|
private HashMap<String, String> tagConversion;
|
||||||
|
|
||||||
public StatusListAdapter(Context context, RetrieveFeedsAsyncTask.Type type, String targetedId, boolean isOnWifi, int behaviorWithAttachments, List<Status> statuses){
|
public StatusListAdapter(Context context, RetrieveFeedsAsyncTask.Type type, String targetedId, boolean isOnWifi, int behaviorWithAttachments, List<Status> statuses){
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
@ -301,8 +308,41 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
try {
|
try {
|
||||||
|
String text = status.getContent();
|
||||||
if( !status.isTranslated() ){
|
if( !status.isTranslated() ){
|
||||||
new YandexQuery(StatusListAdapter.this).getYandexTextview(position, status.getContent(), currentLocale);
|
tagConversion = new HashMap<>();
|
||||||
|
urlConversion = new HashMap<>();
|
||||||
|
Matcher matcher;
|
||||||
|
//Extracts urls
|
||||||
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
|
||||||
|
matcher = Patterns.WEB_URL.matcher(text);
|
||||||
|
else
|
||||||
|
matcher = Helper.urlPattern.matcher(text);
|
||||||
|
int i = 0;
|
||||||
|
//replaces them by a kind of variable which shouldn't be translated ie: __u0__, __u1__, etc.
|
||||||
|
while (matcher.find()){
|
||||||
|
String key = "__u" + String.valueOf(i) + "__";
|
||||||
|
String value = matcher.group(0);
|
||||||
|
if( value != null) {
|
||||||
|
urlConversion.put(key, value);
|
||||||
|
text = text.replace(value, key);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
//Same for tags with __t0__, __t1__, etc.
|
||||||
|
matcher = Helper.hashtagPattern.matcher(text);
|
||||||
|
while (matcher.find()){
|
||||||
|
String key = "__t" + String.valueOf(i) + "__";
|
||||||
|
String value = matcher.group(0);
|
||||||
|
tagConversion.put(key, value);
|
||||||
|
if( value != null) {
|
||||||
|
tagConversion.put(key, value);
|
||||||
|
text = text.replace(value, key);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
new YandexQuery(StatusListAdapter.this).getYandexTextview(position, text, currentLocale);
|
||||||
}else {
|
}else {
|
||||||
status.setTranslationShown(!status.isTranslationShown());
|
status.setTranslationShown(!status.isTranslationShown());
|
||||||
statusListAdapter.notifyDataSetChanged();
|
statusListAdapter.notifyDataSetChanged();
|
||||||
|
@ -872,6 +912,18 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
||||||
}else if( statuses.size() > position) {
|
}else if( statuses.size() > position) {
|
||||||
try {
|
try {
|
||||||
String aJsonString = yandexTranslateToText(translatedResult);
|
String aJsonString = yandexTranslateToText(translatedResult);
|
||||||
|
Iterator itU = urlConversion.entrySet().iterator();
|
||||||
|
while (itU.hasNext()) {
|
||||||
|
Map.Entry pair = (Map.Entry)itU.next();
|
||||||
|
aJsonString = aJsonString.replace(pair.getKey().toString(), pair.getValue().toString());
|
||||||
|
itU.remove();
|
||||||
|
}
|
||||||
|
Iterator itT = tagConversion.entrySet().iterator();
|
||||||
|
while (itT.hasNext()) {
|
||||||
|
Map.Entry pair = (Map.Entry)itT.next();
|
||||||
|
aJsonString = aJsonString.replace(pair.getKey().toString(), pair.getValue().toString());
|
||||||
|
itT.remove();
|
||||||
|
}
|
||||||
statuses.get(position).setTranslated(true);
|
statuses.get(position).setTranslated(true);
|
||||||
statuses.get(position).setTranslationShown(true);
|
statuses.get(position).setTranslationShown(true);
|
||||||
statuses.get(position).setContent_translated(aJsonString);
|
statuses.get(position).setContent_translated(aJsonString);
|
||||||
|
|
|
@ -247,11 +247,11 @@ public class Helper {
|
||||||
|
|
||||||
private static final Pattern SHORTNAME_PATTERN = Pattern.compile(":([-+\\w]+):");
|
private static final Pattern SHORTNAME_PATTERN = Pattern.compile(":([-+\\w]+):");
|
||||||
|
|
||||||
private static final Pattern urlPattern = Pattern.compile(
|
public static final Pattern urlPattern = Pattern.compile(
|
||||||
"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))",
|
"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))",
|
||||||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
|
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
|
||||||
|
|
||||||
private static final Pattern hashtagPattern = Pattern.compile("(#[\\w_À-ú-]{1,})");
|
public static final Pattern hashtagPattern = Pattern.compile("(#[\\w_À-ú-]{1,})");
|
||||||
/**
|
/**
|
||||||
* Converts emojis in input to unicode
|
* Converts emojis in input to unicode
|
||||||
* @param input String
|
* @param input String
|
||||||
|
|
|
@ -25,7 +25,6 @@ import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface;
|
||||||
public class YandexQuery {
|
public class YandexQuery {
|
||||||
|
|
||||||
private OnTranslatedInterface listener;
|
private OnTranslatedInterface listener;
|
||||||
|
|
||||||
public YandexQuery(OnTranslatedInterface listenner) {
|
public YandexQuery(OnTranslatedInterface listenner) {
|
||||||
this.listener = listenner;
|
this.listener = listenner;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +34,7 @@ public class YandexQuery {
|
||||||
YandexClient.get(text, toLanguage, new AsyncHttpResponseHandler() {
|
YandexClient.get(text, toLanguage, new AsyncHttpResponseHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue