Skip to content

Commit

Permalink
do not automatically titlecase words for names (#5105)
Browse files Browse the repository at this point in the history
* do not automatically titlecase words for names (fixes #4784)

* remove unnecessary import
  • Loading branch information
westnordost committed Jul 4, 2023
1 parent b359447 commit 2403f58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Abbreviations(config: Map<String, String>, val locale: Locale) {
for ((regex, replacement) in abbreviations) {
if (!regex.matches(word, isFirstWord, isLastWord)) continue
val result = regex.replaceFirst(word, replacement)
return result.firstLetterToUppercase()
return if (word.first().isTitleCase()) result.titlecase(locale) else result
}
return null
}
Expand Down Expand Up @@ -72,6 +72,6 @@ class Abbreviations(config: Map<String, String>, val locale: Locale) {

return this.matches(word)
}

private fun String.firstLetterToUppercase() = get(0).titlecase(locale) + substring(1)
}

private fun String.titlecase(locale: Locale) = get(0).titlecase(locale) + substring(1)
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ class LocalizedNameAdapter(
if (emptyNamesHint == null) input.requestFocus()
}
input.setText(localizedName.name)
val languageTag = localizedName.languageTag
buttonLanguage.text = if (languageTag == "international") "🌍" else languageTag
updateLanguage(localizedName.languageTag)

// first entry is bold (the first entry is supposed to be the "default language", I
// hope that comes across to the users like this. Otherwise, a text hint is necessary)
Expand All @@ -307,13 +306,14 @@ class LocalizedNameAdapter(

showLanguageSelectMenu(v, notAddedLanguageTags) { languageTag ->
localizedName.languageTag = languageTag
buttonLanguage.text = if (languageTag == "international") "🌍" else languageTag
updateLanguage(languageTag)
updateAddLanguageButtonVisibility()
updateNameSuggestions()
updateAbbreviations()
}
}
}

private fun updateLanguage(languageTag: String) {
buttonLanguage.text = if (languageTag == "international") "🌍" else languageTag
updateNameSuggestions()
updateAbbreviations()
}
Expand All @@ -335,6 +335,7 @@ class LocalizedNameAdapter(
}

private fun updateAbbreviations() {
autoCorrectAbbreviations.abbreviations = null
// load abbreviations from file in background
viewLifecycleScope.launch {
autoCorrectAbbreviations.abbreviations = withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import android.widget.EditText
import de.westnordost.streetcomplete.data.meta.Abbreviations
import de.westnordost.streetcomplete.view.DefaultTextWatcher

/** Automatically expands abbreviations when finishing a word (via space, "-" or
* ".") and capitalizes the first letter of each word that is longer than 3 letters. */
/** Automatically expands abbreviations when finishing a word (via space, "-" or ".") */
class AutoCorrectAbbreviationsViewController(private val editText: EditText) {

var abbreviations: Abbreviations? = null
Expand Down Expand Up @@ -42,13 +41,9 @@ class AutoCorrectAbbreviationsViewController(private val editText: EditText) {

val replacement = abbrs.getExpansion(lastWordBeforeCursor, isFirstWord, isLastWord)

val wordStart = textToCursor.indexOf(lastWordBeforeCursor)
if (replacement != null) {
val wordStart = textToCursor.indexOf(lastWordBeforeCursor)
fixedReplace(s, wordStart, wordStart + lastWordBeforeCursor.length, replacement)
} else if (lastWordBeforeCursor.length > 3) {
val locale = abbrs.locale
val capital = lastWordBeforeCursor.get(0).titlecase(locale)
s.replace(wordStart, wordStart + 1, capital)
}
}

Expand Down

0 comments on commit 2403f58

Please sign in to comment.