show links to known dictionaries when enabling a locale without local dictionary

This commit is contained in:
Helium314 2024-03-15 18:01:15 +01:00
parent 7058fb78b8
commit 3ee768fe17
2 changed files with 42 additions and 37 deletions

View file

@ -275,40 +275,11 @@ class LanguageSettingsDialog(
binding.secondaryLocales.addView(rowBinding.root)
}
private fun createDictionaryText(locale: Locale, context: Context): String {
val link = "<a href='$DICTIONARY_URL'>" + context.getString(R.string.dictionary_link_text) + "</a>"
val message = context.getString(R.string.add_dictionary, link)
val knownDicts = mutableListOf<String>()
context.assets.open("dictionaries_in_dict_repo.csv").reader().forEachLine {
if (it.isBlank()) return@forEachLine
val (type, localeString, experimental) = it.split(",")
// we use a locale string here because that's in the dictionaries repo
// ideally the repo would switch to language tag, but not sure how this is handled in the dictionary header
// further, the dicts in the dictionaries repo should be compatible with other AOSP-based keyboards
val dictLocale = localeString.constructLocale()
if (LocaleUtils.getMatchLevel(locale, dictLocale) < 3) return@forEachLine
val rawDictString = "$type: ${dictLocale.getDisplayName(context.resources.configuration.locale())}"
val dictString = if (experimental.isEmpty()) rawDictString
else context.getString(R.string.available_dictionary_experimental, rawDictString)
val dictBaseUrl = DICTIONARY_URL + DICTIONARY_DOWNLOAD_SUFFIX +
if (experimental.isEmpty()) DICTIONARY_NORMAL_SUFFIX else DICTIONARY_EXPERIMENTAL_SUFFIX
val dictLink = dictBaseUrl + type + "_" + localeString.lowercase() + ".dict"
val fullText = "<li><a href='$dictLink'>$dictString</a></li>"
knownDicts.add(fullText)
}
if (knownDicts.isEmpty()) return message
return """
<p>$message</p>
<b>${context.getString(R.string.dictionary_available)}</b>
<ul>
${knownDicts.joinToString("\n")}
</ul>
""".trimIndent()
}
private fun fillDictionariesView() {
binding.addDictionary.setOnClickListener {
val messageRawText = createDictionaryText(mainLocale, context)
val dictLink = "<a href='$DICTIONARY_URL'>" + context.getString(R.string.dictionary_link_text) + "</a>"
val startMessage = context.getString(R.string.add_dictionary, dictLink)
val messageRawText = createDictionaryTextHtml(startMessage, mainLocale, context)
val message = SpannableStringUtils.fromHtml(messageRawText)
val dialog = Builder(context)
.setTitle(R.string.add_new_dictionary_title)

View file

@ -8,7 +8,9 @@ import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import helium314.keyboard.compat.locale
import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.LocaleUtils
import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.settings.Settings
import java.io.File
@ -43,16 +45,18 @@ fun showMissingDictionaryDialog(context: Context, locale: Locale) {
val repositoryLink = "<a href='$DICTIONARY_URL'>" + context.getString(R.string.dictionary_link_text) + "</a>"
val dictionaryLink = "<a href='$DICTIONARY_URL/src/branch/main/dictionaries/main_$locale.dict'>" + context.getString(
R.string.dictionary_link_text) + "</a>"
val message = SpannableStringUtils.fromHtml(context.getString(
val startMessage = context.getString( // todo: now with the available dicts csv, is the full text still necessary?
R.string.no_dictionary_message,
repositoryLink,
locale.toString(),
locale.toString(), // toString because that's how default AOSP dictionaries are named
dictionaryLink,
))
)
val message = createDictionaryTextHtml(startMessage, locale, context)
val messageSpannable = SpannableStringUtils.fromHtml(message)
val dialog = AlertDialog.Builder(context)
.setTitle(R.string.no_dictionaries_available)
.setMessage(message)
.setMessage(messageSpannable)
.setNegativeButton(R.string.dialog_close, null)
.setNeutralButton(R.string.no_dictionary_dont_show_again_button) { _, _ ->
prefs.edit { putBoolean(Settings.PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG, true) }
@ -62,6 +66,36 @@ fun showMissingDictionaryDialog(context: Context, locale: Locale) {
(dialog.findViewById<View>(android.R.id.message) as? TextView)?.movementMethod = LinkMovementMethod.getInstance()
}
/** returns the [message], and if dictionaries for [locale] or language are available, a links to them */
fun createDictionaryTextHtml(message: String, locale: Locale, context: Context): String {
val knownDicts = mutableListOf<String>()
context.assets.open("dictionaries_in_dict_repo.csv").reader().forEachLine {
if (it.isBlank()) return@forEachLine
val (type, localeString, experimental) = it.split(",")
// we use a locale string here because that's in the dictionaries repo
// ideally the repo would switch to language tag, but not sure how this is handled in the dictionary header
// further, the dicts in the dictionaries repo should be compatible with other AOSP-based keyboards
val dictLocale = localeString.constructLocale()
if (LocaleUtils.getMatchLevel(locale, dictLocale) < 3) return@forEachLine
val rawDictString = "$type: ${dictLocale.getDisplayName(context.resources.configuration.locale())}"
val dictString = if (experimental.isEmpty()) rawDictString
else context.getString(R.string.available_dictionary_experimental, rawDictString)
val dictBaseUrl = DICTIONARY_URL + DICTIONARY_DOWNLOAD_SUFFIX +
if (experimental.isEmpty()) DICTIONARY_NORMAL_SUFFIX else DICTIONARY_EXPERIMENTAL_SUFFIX
val dictLink = dictBaseUrl + type + "_" + localeString.lowercase() + ".dict"
val fullText = "<li><a href='$dictLink'>$dictString</a></li>"
knownDicts.add(fullText)
}
if (knownDicts.isEmpty()) return message
return """
<p>$message</p>
<b>${context.getString(R.string.dictionary_available)}</b>
<ul>
${knownDicts.joinToString("\n")}
</ul>
""".trimIndent()
}
fun cleanUnusedMainDicts(context: Context) {
val dictionaryDir = File(DictionaryInfoUtils.getWordListCacheDirectory(context))
val dirs = dictionaryDir.listFiles() ?: return