Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions app/src/main/java/be/scri/helpers/StringUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package be.scri.helpers

/**
* Utility object for handling string-related operations.
*/
object StringUtils {
/**
* Checks if a word is capitalized (i.e., starts with an uppercase letter).
* @param word The word to check.
* @return `true` if the word is capitalized, `false` otherwise.
*/
fun isWordCapitalized(word: String): Boolean {
if (word.isEmpty()) return false
return word[0].isUpperCase()
}
}
21 changes: 18 additions & 3 deletions app/src/main/java/be/scri/helpers/data/PluralFormsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package be.scri.helpers.data
import DataContract
import android.database.sqlite.SQLiteDatabase
import be.scri.helpers.DatabaseFileManager
import be.scri.helpers.StringUtils.isWordCapitalized

/**
* Manages and queries plural forms of words from the database.
Expand Down Expand Up @@ -49,9 +50,23 @@ class PluralFormsManager(
val pluralCol = numbers.values.firstOrNull()

if (singularCol != null && pluralCol != null) {
fileManager.getLanguageDatabase(language)?.use { db ->
querySpecificPlural(db, singularCol, pluralCol, noun)
}
val wasCapitalized = isWordCapitalized(noun)
val lowerNoun = noun.lowercase()

val result =
fileManager.getLanguageDatabase(language)?.use { db ->
querySpecificPlural(db, singularCol, pluralCol, lowerNoun)
} ?: emptyMap()

if (result.isEmpty()) return emptyMap()

val (singular, plural) = result.entries.first()

val singularOut = if (wasCapitalized) singular.replaceFirstChar { it.uppercase() } else singular

val pluralOut = if (wasCapitalized) plural?.replaceFirstChar { it.uppercase() } else plural

return mapOf(singularOut to pluralOut)
} else {
null
}
Expand Down
48 changes: 45 additions & 3 deletions app/src/main/java/be/scri/helpers/data/TranslationDataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.database.sqlite.SQLiteDatabase
import be.scri.helpers.DatabaseFileManager
import be.scri.helpers.PreferencesHelper
import be.scri.helpers.StringUtils.isWordCapitalized

/**
* Manages translations from a local SQLite database.
Expand Down Expand Up @@ -47,9 +48,50 @@ class TranslationDataManager(

val sourceTable = generateLanguageNameForISOCode(sourceCode)

return fileManager.getTranslationDatabase()?.use { db ->
queryForTranslation(db, sourceTable, destCode, word)
} ?: ""
val isGerman = sourceCode == "de"

// Special German logic
if (isGerman) {
val db = fileManager.getTranslationDatabase() ?: return ""

return db.use { database ->
// Try exact match first ("Buch", "buch", "BUCH")
val direct = queryForTranslation(database, sourceTable, destCode, word)
if (direct.isNotEmpty()) return@use direct

// Try lowercase (this catches verbs/adjectives)
val lower = queryForTranslation(database, sourceTable, destCode, word.lowercase())
if (lower.isNotEmpty()) return@use lower

// Try canonical noun capitalization ("buch" → "Buch")
val canonical = word.lowercase().replaceFirstChar { it.uppercase() }
val nounMatch = queryForTranslation(database, sourceTable, destCode, canonical)
if (nounMatch.isNotEmpty()) return@use nounMatch

""
}
}

val exact =
fileManager.getTranslationDatabase()?.use { db ->
queryForTranslation(db, sourceTable, destCode, word)
} ?: ""

if (exact.isNotEmpty()) return exact

if (isWordCapitalized(word)) {
val lowerCaseWord = word.lowercase()
val translatedLowerCaseWord =
fileManager.getTranslationDatabase()?.use { db ->
queryForTranslation(db, sourceTable, destCode, lowerCaseWord)
} ?: ""

if (translatedLowerCaseWord.isNotEmpty()) {
return translatedLowerCaseWord.replaceFirstChar { it.uppercase() }
}
}

return ""
}

/**
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/be/scri/services/GeneralKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2291,14 +2291,15 @@ abstract class GeneralKeyboardIME(
* @param rawInput The verb entered in the command bar.
*/
private fun handleConjugateState(rawInput: String) {
val searchInput = rawInput.lowercase()
currentVerbForConjugation = rawInput
val languageAlias = getLanguageAlias(language)

val tempOutput =
dbManagers.conjugateDataManager.getTheConjugateLabels(
languageAlias,
dataContract,
rawInput,
searchInput,
)

conjugateOutput =
Expand All @@ -2311,7 +2312,7 @@ abstract class GeneralKeyboardIME(
conjugateLabels =
dbManagers.conjugateDataManager.extractConjugateHeadings(
dataContract,
rawInput,
searchInput,
)

currentState =
Expand Down
Loading