Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ dependencies {
implementation(project(":core:nips"))
implementation(project(":core:app-config"))
implementation(project(":core:networking-primal"))
implementation(project(":core:networking-http"))
implementation(project(":core:networking-upload"))
implementation(project(":core:networking-lightning"))
implementation(project(":core:caching"))
Expand All @@ -305,6 +306,7 @@ dependencies {
implementation(project(":data:account:signer"))

implementation(libs.bignum)
implementation(libs.androidx.security.crypto)
implementation(libs.core.ktx)
implementation(libs.core.splashscreen)
implementation(libs.lifecycle.runtime.ktx)
Expand Down Expand Up @@ -430,6 +432,7 @@ dependencies {

testImplementation(project(":core:testing"))
testImplementation(libs.junit)
testImplementation(libs.ktor.client.mock)
testImplementation(libs.androidx.test.runner)
testImplementation(libs.androidx.test.ext.junit)
testImplementation(libs.androidx.test.ext.junit.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import net.primal.android.R
import net.primal.android.core.compose.AppBarIcon
Expand All @@ -42,6 +43,7 @@ fun DecoratedSettingsOutlinedTextField(
onFocus: (() -> Unit)? = null,
showSupportContent: Boolean = false,
supportingActionText: String = "",
visualTransformation: VisualTransformation = VisualTransformation.None,
) {
val focusManager = LocalFocusManager.current

Expand All @@ -62,6 +64,7 @@ fun DecoratedSettingsOutlinedTextField(
},
onFocus = onFocus,
placeholderText = placeholderText,
visualTransformation = visualTransformation,
)

if (showSupportContent) {
Expand Down Expand Up @@ -95,6 +98,7 @@ private fun SettingsOutlinedTextField(
onButtonClick: () -> Unit,
placeholderText: String,
onFocus: (() -> Unit)? = null,
visualTransformation: VisualTransformation = VisualTransformation.None,
) {
Row(
modifier = modifier,
Expand All @@ -112,6 +116,7 @@ private fun SettingsOutlinedTextField(
shape = AppTheme.shapes.medium,
value = value,
onValueChange = onValueChange,
visualTransformation = visualTransformation,
singleLine = true,
textStyle = AppTheme.typography.bodyMedium,
placeholder = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import net.primal.android.settings.connected.session.remote.RemoteSessionDetails
import net.primal.android.settings.connected.session.remote.RemoteSessionDetailsViewModel
import net.primal.android.settings.content.ContentDisplaySettingsScreen
import net.primal.android.settings.content.ContentDisplaySettingsViewModel
import net.primal.android.settings.translation.TranslationSettingsScreen
import net.primal.android.settings.translation.TranslationSettingsViewModel
import net.primal.android.settings.developer.DeveloperToolsScreen
import net.primal.android.settings.developer.DeveloperToolsViewModel
import net.primal.android.settings.developer.datainspector.DataInspectorScreen
Expand Down Expand Up @@ -75,6 +77,7 @@ private fun NavController.navigateToWalletScanNwcUrl() = navigate(route = "walle
private fun NavController.navigateToCreateNewWalletConnection() = navigate(route = "wallet_settings/create_new_nwc")
private fun NavController.navigateToAppearanceSettings() = navigate(route = "appearance_settings")
private fun NavController.navigateToContentDisplaySettings() = navigate(route = "content_display")
private fun NavController.navigateToTranslationSettings() = navigate(route = "translation_settings")
fun NavController.navigateToNotificationsSettings() = navigate(route = "notifications_settings")
private fun NavController.navigateToZapsSettings() = navigate(route = "zaps_settings")
private fun NavController.navigateToMutedAccounts() = navigate(route = "muted_accounts_settings")
Expand Down Expand Up @@ -132,6 +135,7 @@ fun NavGraphBuilder.settingsNavigation(route: String, navController: NavControll
PrimalSettingsSection.Wallet -> navController.navigateToWalletSettings()
PrimalSettingsSection.Appearance -> navController.navigateToAppearanceSettings()
PrimalSettingsSection.ContentDisplay -> navController.navigateToContentDisplaySettings()
PrimalSettingsSection.Translation -> navController.navigateToTranslationSettings()
PrimalSettingsSection.Notifications -> navController.navigateToNotificationsSettings()
PrimalSettingsSection.Zaps -> navController.navigateToZapsSettings()
PrimalSettingsSection.MutedAccounts -> navController.navigateToMutedAccounts()
Expand Down Expand Up @@ -173,6 +177,7 @@ fun NavGraphBuilder.settingsNavigation(route: String, navController: NavControll
network(route = "network", navController = navController)
appearance(route = "appearance_settings", navController = navController)
contentDisplay(route = "content_display", navController = navController)
translation(route = "translation_settings", navController = navController)
mutedAccounts(route = "muted_accounts_settings", navController = navController)
mediaUploads(route = "media_uploads_settings", navController = navController)
notifications(route = "notifications_settings", navController = navController)
Expand Down Expand Up @@ -463,6 +468,22 @@ private fun NavGraphBuilder.contentDisplay(route: String, navController: NavCont
)
}

private fun NavGraphBuilder.translation(route: String, navController: NavController) =
composable(
route = route,
enterTransition = { primalSlideInHorizontallyFromEnd },
exitTransition = { primalScaleOut },
popEnterTransition = { primalScaleIn },
popExitTransition = { primalSlideOutHorizontallyToEnd },
) {
val viewModel = hiltViewModel<TranslationSettingsViewModel>(it)
LockToOrientationPortrait()
TranslationSettingsScreen(
viewModel = viewModel,
onClose = { navController.navigateUp() },
)
}

private fun NavGraphBuilder.zaps(route: String, navController: NavController) =
composable(
route = route,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import net.primal.android.notes.feed.note.NoteContract.UiEvent
import net.primal.android.notes.feed.note.ui.FeedNoteActionsRow
import net.primal.android.notes.feed.note.ui.FeedNoteHeader
import net.primal.android.notes.feed.note.ui.NoteContent
import net.primal.android.notes.translation.ui.NoteTranslateBar
import net.primal.android.notes.feed.note.ui.NoteDropdownMenuIcon
import net.primal.android.notes.feed.note.ui.NoteSurfaceCard
import net.primal.android.notes.feed.note.ui.RepostedNotice
Expand Down Expand Up @@ -609,6 +610,15 @@ private fun FeedNote(
onPollOptionSelected = onPollOptionSelected,
)

NoteTranslateBar(
noteId = data.postId,
content = data.content,
modifier = Modifier
.padding(horizontal = 8.dp)
.padding(start = contentIndentDp)
.padding(top = 4.dp),
)

if (isFeedLayout) {
Box(modifier = Modifier.padding(start = contentIndentDp)) {
contentFooter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package net.primal.android.notes.translation

/**
* Lightweight, offline language detection for note prose.
*
* This intentionally does NOT try to be a general-purpose detector. It first
* checks for scripts that map (almost) unambiguously to a language, then
* falls back to stopword scoring for Latin-script languages. It returns null
* whenever it is not reasonably confident, and callers must treat null as
* "unknown" — never as English.
*
* The detected code is only used to (a) hide the Translate action when a note
* already appears to be in the user's target language and (b) label the
* result ("Translated from Spanish"). The translation request itself always
* uses source = "auto" on the provider.
*
* Ported from the web implementation in PrimalHQ/primal-web-app
* (src/lib/languageDetect.ts, PR #240).
*/
object LanguageDetector {

private val STOPWORDS: Map<String, List<String>> = mapOf(
"en" to listOf(
"the", "and", "is", "to", "of", "a", "in", "that", "it", "was", "for", "on", "are", "with", "as",
"i", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "words", "but",
"not", "what", "all", "were", "we", "when", "your", "can", "said", "there", "use", "an", "each",
"which", "she", "do", "how", "their", "will",
),
"es" to listOf(
"el", "la", "de", "que", "y", "en", "un", "una", "los", "las", "del", "se", "no", "es", "por",
"con", "su", "para", "como", "está", "más", "pero", "sus", "le", "ya", "o", "este", "sí",
"porque", "cuando", "muy", "sin", "sobre", "también", "me", "hasta", "hay", "donde", "quien",
"desde", "todo", "nos", "durante", "todos", "uno", "les", "ni", "contra", "otros", "ese",
),
"pt" to listOf(
"o", "a", "de", "e", "do", "da", "que", "em", "um", "para", "é", "com", "não", "uma", "os",
"no", "se", "na", "por", "mais", "as", "dos", "como", "mas", "foi", "ao", "ele", "das", "tem",
"à", "seu", "sua", "ou", "ser", "quando", "muito", "há", "nos", "já", "está", "eu", "também",
"só", "pelo", "pela", "até", "isso", "ela",
),
"fr" to listOf(
"le", "la", "de", "des", "et", "les", "à", "un", "une", "du", "est", "dans", "que", "qui",
"pour", "pas", "sur", "plus", "par", "au", "aux", "avec", "ce", "en", "et", "on", "se", "ne",
"mais", "ou", "comme", "tout", "sont", "être", "aussi", "leur", "bien", "peut", "ces", "y",
"deux", "même", "si", "mon", "sans", "nous",
),
"de" to listOf(
"der", "die", "und", "in", "den", "von", "zu", "mit", "sich", "des", "auf", "für", "ist",
"im", "dem", "nicht", "ein", "eine", "als", "auch", "es", "an", "werden", "aus", "er", "hat",
"dass", "sie", "nach", "wird", "bei", "einer", "um", "am", "sind", "noch", "wie", "einem",
"über", "einen", "so", "zum", "haben", "nur", "oder", "aber", "vor",
),
"it" to listOf(
"il", "di", "che", "e", "la", "per", "un", "una", "in", "con", "del", "sono", "le", "è",
"dei", "gli", "da", "lo", "come", "questo", "ma", "anche", "questa", "essere", "ci", "ne",
"della", "nel", "al", "più", "se", "era", "si", "delle", "a", "lui", "lei", "io", "noi",
"loro", "mio", "tuo", "suo", "nostro", "vostro", "cosa", "fare", "dire", "dove",
),
"nl" to listOf(
"de", "het", "een", "van", "ik", "te", "dat", "die", "in", "een", "hij", "het", "niet",
"zijn", "is", "op", "aan", "met", "als", "voor", "zijn", "er", "maar", "om", "hem", "dan",
"zou", "of", "wat", "mijn", "men", "dit", "zo", "door", "over", "ze", "zich", "bij", "ook",
"tot", "je", "mij", "uit", "der", "daar", "haar", "naar", "heb", "hoe", "heeft",
),
"id" to listOf(
"yang", "dan", "di", "ini", "itu", "dengan", "untuk", "tidak", "dari", "dalam", "akan",
"pada", "juga", "saya", "ke", "adalah", "oleh", "atau", "ada", "mereka", "sebagai", "telah",
"sudah", "bisa", "kami", "kita", "dia", "ia", "sangat", "banyak", "lebih", "harus", "dapat",
"tersebut", "sebuah", "karena", "saat", "tapi", "namun", "jadi", "bagi", "atas", "lagi",
"masih", "kamu", "anda", "aku", "kalau", "jika", "bukan",
),
"tr" to listOf(
"bir", "ve", "bu", "da", "de", "için", "çok", "en", "o", "ne", "gibi", "daha", "ama",
"kadar", "sonra", "önce", "nasıl", "niye", "ya", "ki", "mü", "ise", "olan", "olarak",
"şey", "sen", "ben", "biz", "siz", "onlar", "bana", "sana", "ona", "bizi", "sizi", "onları",
"kendi", "şu", "her", "hiç", "zaman", "yerde", "göre", "rağmen", "dolayı", "tarafından",
),
"pl" to listOf(
"i", "w", "nie", "się", "na", "to", "z", "jest", "że", "do", "tak", "jak", "co", "o", "ale",
"po", "za", "tym", "od", "przez", "ich", "który", "być", "ten", "go", "już", "tylko", "dla",
"podczas", "może", "przy", "kiedy", "bardzo", "też", "jako", "nad", "jeszcze", "przed",
"bez", "tego", "lub", "aby", "wiele", "kilka", "coraz", "gdzie", "czy", "między",
),
)

private val WORD_SPLIT_RE = Regex("""[^\p{L}]+""")

/** Best-effort ISO 639-1 code for [text], or null when not confident. */
fun detectLanguage(text: String): String? {
val sample = text.trim()
if (sample.isEmpty()) return null

detectByScript(sample)?.let { return it }
return detectLatinByStopwords(sample)
}

private fun detectByScript(sample: String): String? {
// Kana => Japanese; Hangul => Korean; Han alone defaults to Chinese.
if (sample.any { it in '\u3040'..'\u30FF' }) return "ja"
if (sample.any { it in '\uAC00'..'\uD7A3' || it in '\u1100'..'\u11FF' }) return "ko"
if (sample.any { it in '\u0E00'..'\u0E7F' }) return "th"
if (sample.any { it in '\u0900'..'\u097F' }) return "hi"
if (sample.any { it in '\u0590'..'\u05FF' }) return "he"
if (sample.any { it in '\u0600'..'\u06FF' }) return "ar"
if (sample.any { it in '\u0370'..'\u03FF' }) return "el"
if (sample.any { it in '\u0400'..'\u04FF' }) return detectCyrillic(sample)
if (sample.any { it in '\u4E00'..'\u9FFF' }) return "zh"
return null
}

private fun detectCyrillic(sample: String): String {
// Letters distinctive to Ukrainian / Serbian; otherwise default to Russian.
if (sample.any { it in "іїєґІЇЄҐ" }) return "uk"
if (sample.any { it in "ђјљњћџЂЈЉЊЋЏ" }) return "sr"
return "ru"
}

private fun detectLatinByStopwords(sample: String): String? {
val words = sample.lowercase().split(WORD_SPLIT_RE).filter { it.isNotEmpty() }
if (words.size < 4) return null

var best: String? = null
var bestScore = 0
var secondScore = 0
for ((code, stopwords) in STOPWORDS) {
val stopwordSet = stopwords.toSet()
val score = words.count { it in stopwordSet }
if (score > bestScore) {
secondScore = bestScore
bestScore = score
best = code
} else if (score > secondScore) {
secondScore = score
}
}
// Require a clear winner with a handful of hits to avoid coin flips
// between related languages on short notes.
return if (best != null && bestScore >= 2 && bestScore > secondScore) best else null
}

/** 'en-US' -> 'en'; 'EN' -> 'en'. Used to compare locale-ish codes. */
fun primaryLanguageCode(code: String?): String? {
if (code.isNullOrBlank()) return null
val primary = code.trim().lowercase().split('-', '_').firstOrNull()
return if (primary.isNullOrEmpty()) null else primary
}
}
Loading