-
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.
Integrate Google Cloud Translate for simplified Chinese!
- Loading branch information
1 parent
f5d611d
commit 6ba8704
Showing
10 changed files
with
125 additions
and
33 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
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
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
42 changes: 42 additions & 0 deletions
42
...fications/src/main/kotlin/com/kevlina/budgetplus/feature/push/notifications/Translator.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,42 @@ | ||
package com.kevlina.budgetplus.feature.push.notifications | ||
|
||
import android.content.Context | ||
import com.google.cloud.translate.Translate.TranslateOption | ||
import com.google.cloud.translate.TranslateOptions | ||
import com.kevlina.budgetplus.core.common.R | ||
import dagger.Reusable | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@Reusable | ||
internal class Translator @Inject constructor( | ||
@ApplicationContext context: Context, | ||
) { | ||
private val translationService by lazy { | ||
@Suppress("DEPRECATION") | ||
TranslateOptions.newBuilder() | ||
.setApiKey(context.getString(R.string.google_cloud_translate_api_key)) | ||
.build() | ||
.service | ||
} | ||
|
||
suspend fun translate( | ||
text: String, | ||
sourceLanCode: String?, | ||
targetLanCode: String, | ||
): String = withContext(Dispatchers.IO) { | ||
try { | ||
translationService.translate( | ||
text, | ||
TranslateOption.sourceLanguage(sourceLanCode), | ||
TranslateOption.targetLanguage(targetLanCode) | ||
).translatedText | ||
} catch (e: Exception) { | ||
Timber.e(e, "Translation failed, return original text as fallback.") | ||
text | ||
} | ||
} | ||
} |
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