Skip to content

Commit a8999fe

Browse files
authored
Merge pull request #331 from Inhishonor/translation-skill
2 parents 0f7d9e4 + 03779bd commit a8999fe

27 files changed

+1068
-44
lines changed

app/build.gradle.kts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import org.gradle.configurationcache.extensions.capitalized
22
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3+
import org.stypox.dicio.unicodeCldrPlugin.UnicodeCldrLanguagesTask
34

45
buildscript {
56
repositories {
67
mavenCentral()
78
}
89
dependencies {
910
classpath(libs.dicio.sentences.compiler.plugin)
11+
classpath(libs.dicio.unicode.cldr.plugin)
1012
}
1113
}
1214

@@ -20,6 +22,7 @@ plugins {
2022
alias(libs.plugins.com.google.dagger.hilt.android)
2123
alias(libs.plugins.com.google.protobuf)
2224
alias(libs.plugins.dicio.sentences.compiler.plugin)
25+
alias(libs.plugins.dicio.unicode.cldr.plugin)
2326
}
2427

2528
android {
@@ -36,11 +39,6 @@ android {
3639

3740
vectorDrawables.useSupportLibrary = true
3841

39-
// add folders generated by sentencesCompiler task
40-
sourceSets["main"].java {
41-
srcDir("build/generated/source/sentences/main")
42-
}
43-
4442
ndk {
4543
abiFilters += arrayOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
4644
}
@@ -116,6 +114,12 @@ androidComponents {
116114
}
117115
}
118116

117+
tasks.withType(UnicodeCldrLanguagesTask::class) {
118+
// tell the UnicodeCldrLanguagesTask plugin which git commit of the
119+
// https://github.com/unicode-org/cldr repo to use as a source of data
120+
unicodeCldrGitCommit = libs.versions.unicodeCldrGitCommit
121+
}
122+
119123
dependencies {
120124
// Desugaring
121125
coreLibraryDesugaring(libs.desugar.jdk.libs)

app/src/main/kotlin/org/stypox/dicio/error/ExceptionUtils.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package org.stypox.dicio.error
22

3-
import java.io.IOException
43
import java.io.InterruptedIOException
54
import java.io.PrintWriter
65
import java.io.StringWriter
6+
import java.net.ConnectException
77
import java.net.SocketException
8+
import java.net.UnknownHostException
9+
import javax.net.ssl.SSLException
810

911
object ExceptionUtils {
1012
fun isNetworkError(throwable: Throwable?): Boolean {
1113
return hasAssignableCause(
12-
throwable, // network api cancellation
13-
IOException::class.java, SocketException::class.java, // blocking code disposed
14-
InterruptedException::class.java, InterruptedIOException::class.java
14+
throwable,
15+
16+
UnknownHostException::class.java,
17+
SSLException::class.java,
18+
ConnectException::class.java,
19+
SocketException::class.java,
20+
21+
// blocking code disposed
22+
InterruptedException::class.java,
23+
InterruptedIOException::class.java,
1524
)
1625
}
1726

app/src/main/kotlin/org/stypox/dicio/eval/SkillHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.stypox.dicio.skills.open.OpenInfo
2828
import org.stypox.dicio.skills.search.SearchInfo
2929
import org.stypox.dicio.skills.telephone.TelephoneInfo
3030
import org.stypox.dicio.skills.timer.TimerInfo
31+
import org.stypox.dicio.skills.translation.TranslationInfo
3132
import org.stypox.dicio.skills.weather.WeatherInfo
3233
import org.stypox.dicio.skills.joke.JokeInfo
3334
import javax.inject.Inject
@@ -53,6 +54,7 @@ class SkillHandler @Inject constructor(
5354
MediaInfo,
5455
JokeInfo,
5556
ListeningInfo(dataStore),
57+
TranslationInfo,
5658
)
5759

5860
// TODO add more fallback skills (e.g. search)

app/src/main/kotlin/org/stypox/dicio/skills/search/SearchSkill.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,12 @@ internal fun searchOnDuckDuckGo(ctx: SkillContext, query: String): List<SearchOu
4949
// make request using headers
5050
val html: String = ConnectionUtils.getPage(
5151
DUCK_DUCK_GO_SEARCH_URL + ConnectionUtils.urlEncode(query),
52-
object : HashMap<String?, String?>() {
53-
init {
54-
put(
55-
"User-Agent",
56-
"Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0"
57-
)
58-
put(
59-
"Accept",
60-
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
61-
)
62-
put(
63-
"Host",
64-
"html.duckduckgo.com"
65-
)
66-
put("Cookie", "kl=$locale")
67-
}
68-
}
52+
mapOf(
53+
"User-Agent" to "Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0",
54+
"Accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
55+
"Host" to "html.duckduckgo.com",
56+
"Cookie" to "kl=$locale",
57+
)
6958
)
7059

7160
val document: Document = Jsoup.parse(html)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.stypox.dicio.skills.translation
2+
3+
import android.content.Context
4+
import androidx.compose.material.icons.Icons
5+
import androidx.compose.material.icons.filled.Language
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.graphics.vector.rememberVectorPainter
8+
import androidx.core.os.LocaleListCompat
9+
import org.dicio.skill.context.SkillContext
10+
import org.dicio.skill.skill.Skill
11+
import org.dicio.skill.skill.SkillInfo
12+
import org.stypox.dicio.R
13+
import org.stypox.dicio.sentences.Sentences
14+
import org.stypox.dicio.util.LocaleUtils
15+
16+
object TranslationInfo : SkillInfo("translation") {
17+
override fun name(context: Context) =
18+
context.getString(R.string.skill_name_translation)
19+
20+
override fun sentenceExample(context: Context) =
21+
context.getString(R.string.skill_sentence_example_translation)
22+
23+
@Composable
24+
override fun icon() =
25+
rememberVectorPainter(Icons.Default.Language)
26+
27+
override fun isAvailable(ctx: SkillContext): Boolean {
28+
val hasSupportedLocale = try {
29+
LocaleUtils.resolveSupportedLocale(
30+
LocaleListCompat.create(ctx.locale),
31+
TranslationSkill.TRANSLATE_SUPPORTED_LOCALES
32+
)
33+
true
34+
} catch (ignored: LocaleUtils.UnsupportedLocaleException) {
35+
false
36+
}
37+
return (Sentences.Translation[ctx.sentencesLanguage] != null) && hasSupportedLocale
38+
}
39+
40+
override fun build(ctx: SkillContext): Skill<*> {
41+
return TranslationSkill(TranslationInfo, Sentences.Translation[ctx.sentencesLanguage]!!)
42+
}
43+
}

0 commit comments

Comments
 (0)