Skip to content

Commit

Permalink
tuskyapp#134 I could not find a way to replace the text in the post b…
Browse files Browse the repository at this point in the history
…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
ozkanpakdil committed Dec 27, 2022
1 parent 68f20e0 commit 4f6273a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
36 changes: 34 additions & 2 deletions app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.MenuItem
import android.view.View
Expand Down Expand Up @@ -56,15 +58,15 @@ import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.interfaces.AccountSelectionListener
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.usecase.TimelineCases
import com.keylesspalace.tusky.util.openLink
import com.keylesspalace.tusky.util.parseAsMastodonHtml
import com.keylesspalace.tusky.util.*
import com.keylesspalace.tusky.view.showMuteAccountDialog
import com.keylesspalace.tusky.viewdata.AttachmentViewData
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import kotlinx.coroutines.launch
import java.lang.IllegalStateException
import java.util.LinkedHashSet
import javax.inject.Inject
import kotlin.concurrent.thread

/* Note from Andrew on Jan. 22, 2017: This class is a design problem for me, so I left it with an
* awkward name. TimelineFragment and NotificationFragment have significant overlap but the nature
Expand All @@ -86,6 +88,9 @@ abstract class SFragment : Fragment(), Injectable {
@Inject
lateinit var timelineCases: TimelineCases

@Inject
lateinit var gTranslator: GoogleTranslator

override fun startActivity(intent: Intent) {
super.startActivity(intent)
requireActivity().overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
Expand Down Expand Up @@ -255,6 +260,10 @@ abstract class SFragment : Fragment(), Injectable {
openReportPage(accountId, accountUsername, id)
return@setOnMenuItemClickListener true
}
R.id.status_translate -> {
translateNoot(status)
return@setOnMenuItemClickListener true
}
R.id.status_unreblog_private -> {
onReblog(false, position)
return@setOnMenuItemClickListener true
Expand Down Expand Up @@ -308,6 +317,29 @@ abstract class SFragment : Fragment(), Injectable {
popup.show()
}

private fun translateNoot(status: Status) {
thread {
val translated = gTranslator.translate(status.content)

val handler = Handler(Looper.getMainLooper())
handler.post {
AlertDialog.Builder(requireContext())
.setMessage(translated)
.setNegativeButton(android.R.string.cancel, null)
.show()
}

// println(translated)
// if (this is ViewThreadFragment){
// this.viewBinding {
// it.rootView.status
// }
// }
// status.setContent(translated)

}
}

private fun onMute(accountId: String, accountUsername: String) {

showMuteAccountDialog(this.requireActivity(), accountUsername) { notifications: Boolean?, duration: Int? ->
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/keylesspalace/tusky/util/GoogleTranslator.kt
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)
3 changes: 3 additions & 0 deletions app/src/main/res/menu/status_more.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@
<item
android:id="@+id/status_report"
android:title="@string/action_report" />
<item
android:id="@+id/status_translate"
android:title="@string/translate_noot" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-en-rGB/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
<string name="error_invalid_domain">Invalid domain entered</string>
<string name="title_posts_with_replies">With replies</string>
<string name="title_posts_pinned">Pinned</string>
<string name="translate_noot">Translate</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,5 @@
<string name="pref_title_http_proxy_port_message">Порт повинен бути між %d і %d</string>
<string name="error_status_source_load">Не вдалося завантажити джерело стану з сервера.</string>
<string name="post_media_alt">ALT</string>
<string name="translate_noot">Translate</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -708,5 +708,6 @@
<string name="report_category_other">Other</string>

<string name="action_unfollow_hashtag_format">Unfollow #%s?</string>
<string name="translate_noot">Translate</string>

</resources>

0 comments on commit 4f6273a

Please sign in to comment.