forked from tuskyapp/Tusky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tuskyapp#134 I could not find a way to replace the text in the post b…
…ut I open an alert dialog to show translation. can someone please help me to set the translation in the main post?
- Loading branch information
1 parent
68f20e0
commit 4f6273a
Showing
6 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/keylesspalace/tusky/util/GoogleTranslator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.keylesspalace.tusky.util | ||
|
||
import android.util.Log | ||
import com.google.gson.Gson | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import javax.inject.Inject | ||
|
||
class GoogleTranslator @Inject constructor(private val httpClient: OkHttpClient) { | ||
private val url = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t" | ||
|
||
var gson = Gson() | ||
|
||
fun translate(query: String, from: String = "auto", to: String = "en"): String { | ||
var result = query | ||
val httpUrl = url.toHttpUrl().newBuilder() | ||
.addQueryParameter("sl", from) | ||
.addQueryParameter("tl", to) | ||
.addQueryParameter("dj", "1") | ||
.addQueryParameter("q", query) | ||
.build() | ||
|
||
val request = Request.Builder().url(httpUrl).build() | ||
val call = httpClient.newCall(request) | ||
val response = call.execute() | ||
return response.body?.string()?.let { responseText -> | ||
val fromJson = gson.fromJson(responseText, Sentences::class.java) | ||
fromJson.sentences.forEach { | ||
result = result.replace(it.orig, it.trans) | ||
} | ||
Log.d("GOOGLE_TRANSLATE", result) | ||
return result | ||
}.toString() | ||
} | ||
} | ||
|
||
data class Sentences(val sentences: List<Translates>) | ||
|
||
data class Translates(val trans: String, val orig: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters