Introduces google translate
This commit is contained in:
parent
5f9ecbadfb
commit
8b7572e8b0
|
@ -1,26 +0,0 @@
|
|||
package mastodon.etalab.gouv.fr.mastodon;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumentation test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("mastodon.etalab.gouv.fr.mastodon", appContext.getPackageName());
|
||||
}
|
||||
}
|
|
@ -55,7 +55,6 @@
|
|||
<activity android:name="fr.gouv.etalab.mastodon.activities.ShowAccountActivity"
|
||||
android:windowSoftInputMode="stateAlwaysHidden"
|
||||
android:configChanges="orientation|screenSize"
|
||||
android:noHistory="true"
|
||||
android:label="@string/app_name"
|
||||
/>
|
||||
<activity android:name="fr.gouv.etalab.mastodon.activities.HashTagActivity"
|
||||
|
|
|
@ -96,14 +96,6 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA
|
|||
startActivity(browserIntent);
|
||||
}
|
||||
});
|
||||
Button about_translation = (Button) findViewById(R.id.about_translation);
|
||||
about_translation.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://translate.yandex.com/"));
|
||||
startActivity(browserIntent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ import android.os.Build;
|
|||
import android.os.Bundle;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -61,6 +62,7 @@ import fr.gouv.etalab.mastodon.activities.TootActivity;
|
|||
import fr.gouv.etalab.mastodon.client.Entities.Error;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface;
|
||||
import fr.gouv.etalab.mastodon.translation.GoogleTranslateQuery;
|
||||
import fr.gouv.etalab.mastodon.translation.YandexQuery;
|
||||
import mastodon.etalab.gouv.fr.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
||||
|
@ -207,7 +209,9 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
|||
public void onClick(View v) {
|
||||
try {
|
||||
if( !status.isTranslated() ){
|
||||
new YandexQuery(StatusListAdapter.this).getYandexTextview(position, status.getContent(), currentLocale);
|
||||
//new YandexQuery(StatusListAdapter.this).getYandexTextview(position, status.getContent(), currentLocale);
|
||||
new GoogleTranslateQuery(StatusListAdapter.this).getGoogleTextview(position, status.getContent(), currentLocale);
|
||||
|
||||
}else {
|
||||
status.setTranslationShown(!status.isTranslationShown());
|
||||
statusListAdapter.notifyDataSetChanged();
|
||||
|
@ -656,10 +660,7 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
|||
Toast.makeText(context, R.string.toast_error_translate, Toast.LENGTH_LONG).show();
|
||||
}else if( statuses.size() > position) {
|
||||
try {
|
||||
JSONObject translationJson = new JSONObject(translatedResult);
|
||||
JSONArray aJsonArray = translationJson.getJSONArray("text");
|
||||
String aJsonString = aJsonArray.get(0).toString();
|
||||
aJsonString = URLDecoder.decode(aJsonString, "UTF-8");
|
||||
String aJsonString = googleTranslateToText(translatedResult);
|
||||
statuses.get(position).setTranslated(true);
|
||||
statuses.get(position).setTranslationShown(true);
|
||||
statuses.get(position).setContent_translated(aJsonString);
|
||||
|
@ -670,6 +671,36 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
|
|||
}
|
||||
}
|
||||
|
||||
private String yandexTranslateToText(String text) throws JSONException, UnsupportedEncodingException{
|
||||
JSONObject translationJson = new JSONObject(text);
|
||||
JSONArray aJsonArray = translationJson.getJSONArray("text");
|
||||
String aJsonString = aJsonArray.get(0).toString();
|
||||
aJsonString = URLDecoder.decode(aJsonString, "UTF-8");
|
||||
return aJsonString;
|
||||
}
|
||||
|
||||
private String googleTranslateToText(String text) throws JSONException, UnsupportedEncodingException{
|
||||
|
||||
int i = 0;
|
||||
String aJsonString = "";
|
||||
while( i < new JSONArray(new JSONArray(text).get(0).toString()).length() ) {
|
||||
aJsonString += new JSONArray(new JSONArray(new JSONArray(text).get(0).toString()).get(i).toString()).get(0).toString();
|
||||
i++;
|
||||
}
|
||||
//Some fixes due to translation with Google
|
||||
aJsonString = aJsonString.trim();
|
||||
aJsonString = aJsonString.replace("< / ","</");
|
||||
aJsonString = aJsonString.replace("</ ","</");
|
||||
aJsonString = aJsonString.replace("> ",">");
|
||||
aJsonString = aJsonString.replace(" <","<");
|
||||
aJsonString = aJsonString.replace(" // ","//");
|
||||
aJsonString = aJsonString.replace("// ","//");
|
||||
aJsonString = aJsonString.replace(" //","//");
|
||||
aJsonString = aJsonString.replace(" www .","www.");
|
||||
aJsonString = aJsonString.replace("www .","www.");
|
||||
aJsonString = URLDecoder.decode(aJsonString, "UTF-8");
|
||||
return aJsonString;
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
LinearLayout status_content_container;
|
||||
|
|
|
@ -541,7 +541,7 @@ public class Helper {
|
|||
// prepare intent which is triggered if the user click on the notification
|
||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
|
||||
PendingIntent pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
|
||||
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
// build notification
|
||||
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Thomas Schneider; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
package fr.gouv.etalab.mastodon.translation;
|
||||
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.loopj.android.http.AsyncHttpClient;
|
||||
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
|
||||
|
||||
class GoogleTranslateClient {
|
||||
|
||||
private static final String BASE_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&ie=UTF-8&oe=UTF-8&";
|
||||
|
||||
|
||||
private static AsyncHttpClient client = new AsyncHttpClient();
|
||||
|
||||
public static void get(String text, String toLanguage, AsyncHttpResponseHandler responseHandler) {
|
||||
client.post(getAbsoluteUrl(text, toLanguage), responseHandler);
|
||||
}
|
||||
|
||||
private static String getAbsoluteUrl(String content, String toLanguage) {
|
||||
String lang = "tl=" + toLanguage + "&dt=t&";
|
||||
String text;
|
||||
try {
|
||||
text = "q=" + URLEncoder.encode(content, "utf-8") + "&";
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
text = "q=" + content + "&";
|
||||
}
|
||||
String format = "format=html&";
|
||||
return BASE_URL + lang + format + text ;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Thomas Schneider; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
package fr.gouv.etalab.mastodon.translation;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import cz.msebera.android.httpclient.Header;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 12/07/2017.
|
||||
* Google client API
|
||||
*/
|
||||
public class GoogleTranslateQuery {
|
||||
|
||||
private OnTranslatedInterface listener;
|
||||
|
||||
|
||||
public GoogleTranslateQuery(OnTranslatedInterface listenner) {
|
||||
this.listener = listenner;
|
||||
}
|
||||
|
||||
public void getGoogleTextview(final int position, final String text, final String toLanguage) throws JSONException {
|
||||
|
||||
GoogleTranslateClient.get(text, toLanguage, new AsyncHttpResponseHandler() {
|
||||
@Override
|
||||
public void onStart() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
|
||||
String str_response = new String(response);
|
||||
listener.onTranslatedTextview(position, str_response,false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
|
||||
listener.onTranslatedTextview(position, null, true);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
|
@ -26,7 +26,6 @@ public class YandexQuery {
|
|||
|
||||
private OnTranslatedInterface listener;
|
||||
|
||||
|
||||
public YandexQuery(OnTranslatedInterface listenner) {
|
||||
this.listener = listenner;
|
||||
}
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package mastodon.etalab.gouv.fr.mastodon;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue