fix problems getting correct dictionary

This commit is contained in:
Helium 2022-03-25 09:54:27 +01:00
parent 5529da136c
commit 9490d94af4
3 changed files with 28 additions and 27 deletions

View file

@ -173,8 +173,8 @@ final public class BinaryDictionaryGetter {
for (File directory : directoryList) {
if (!directory.isDirectory()) continue;
final String dirLocale =
DictionaryInfoUtils.getWordListIdFromFileName(directory.getName());
final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale);
DictionaryInfoUtils.getWordListIdFromFileName(directory.getName()).toLowerCase(Locale.ENGLISH);
final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale.toLowerCase(Locale.ENGLISH));
if (LocaleUtils.isMatch(matchLevel)) {
final File[] wordLists = directory.listFiles();
if (null != wordLists) {
@ -314,10 +314,9 @@ final public class BinaryDictionaryGetter {
if (bestMatchName == null) return null;
// we have a match, now copy contents of the dictionary to cached word lists folder
// but take care to use the correct case, as it will need to match with locale.toString()
final String bestMatchCaseCorrected = LocaleUtils.constructLocaleFromString(bestMatchName).toString();
File dictFile = new File(DictionaryInfoUtils.getCacheDirectoryForLocale(bestMatchCaseCorrected, context) +
File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME);
final String bestMatchLocale = extractLocaleFromAssetsDictionaryFile(bestMatchName);
File dictFile = new File(DictionaryInfoUtils.getCacheDirectoryForLocale(bestMatchLocale, context) +
File.separator + DictionaryInfoUtils.getMainDictFilename(bestMatchLocale));
try {
FileUtils.copyStreamToNewFile(
context.getAssets().open(ASSETS_DICTIONARY_FOLDER + File.separator + bestMatchName),
@ -337,10 +336,10 @@ final public class BinaryDictionaryGetter {
* Returns the locale, or null if file name does not match the pattern
*/
public static String extractLocaleFromAssetsDictionaryFile(final String dictionaryFileName) {
if (dictionaryFileName.startsWith(BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY)
if (dictionaryFileName.startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX)
&& dictionaryFileName.endsWith(".dict")) {
return dictionaryFileName.substring(
BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.length() + 1,
DictionaryInfoUtils.MAIN_DICT_PREFIX.length(),
dictionaryFileName.lastIndexOf('.')
);
}

View file

@ -8,7 +8,6 @@ import android.os.Bundle
import android.preference.Preference
import android.text.Html
import android.text.method.LinkMovementMethod
import android.util.Log
import android.util.TypedValue
import android.view.Menu
import android.view.MenuInflater
@ -54,14 +53,14 @@ class DictionarySettingsFragment : SubScreenFragment() {
val internalDicts = mutableSetOf<Locale>()
// get available dictionaries
// cached (internal in use and user dicts)
DictionaryInfoUtils.getCachedDirectoryList(activity).forEach { dir ->
DictionaryInfoUtils.getCachedDirectoryList(activity)?.forEach { dir ->
if (!dir.isDirectory)
return@forEach
dir.list()?.forEach {
if (it == DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME)
internalDicts.add(dir.name.toLocale())
else if (it == DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME)
if (it == DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME)
userDicts.add(dir.name.toLocale())
else if (it.startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX))
internalDicts.add(dir.name.toLocale())
}
}
// internal only
@ -70,7 +69,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
internalDicts.add(it.toLocale())
}
}
Log.i("opb_", "user: $userDicts, internal: $internalDicts")
// TODO: decide sort order: alphabetic? or user and then built-in?
// split using categories?
@ -84,7 +82,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
// open dialog for update or delete / reset
currentDictLocale = dict
currentDictState = if (internalDicts.contains(dict)) DICT_INTERNAL_AND_USER else DICT_USER_ONLY
Log.i("opb_", "show for $dict, user: ${userDicts.contains(dict)}, internal: ${internalDicts.contains(dict)}")
showUpdateDialog()
true
}
@ -100,7 +97,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
// open dialog for update, maybe disabling if i can make it work?
currentDictLocale = dict
currentDictState = DICT_INTERNAL_ONLY
Log.i("opb_", "show for $dict, user: ${userDicts.contains(dict)}, internal: ${internalDicts.contains(dict)}")
showUpdateDialog()
true
}
@ -119,8 +115,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
if (currentDictLocale == null && currentDictState != DICT_NEW)
return
Log.i("opb_", "exists for user: $currentDictExistsForUser")
val link = "<a href='$DICTIONARY_URL'>" +
resources.getString(R.string.dictionary_link_text) + "</a>"
val message = if (currentDictState == DICT_NEW)
@ -291,7 +285,7 @@ class DictionarySettingsFragment : SubScreenFragment() {
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME
private fun Locale.getInternalDictFilename() =
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.getMainDictFilename(this.toString())
private fun String.displayName() = LocaleUtils.constructLocaleFromString(this)
.getDisplayName(resources.configuration.locale)

View file

@ -53,9 +53,9 @@ public class DictionaryInfoUtils {
private static final String TAG = DictionaryInfoUtils.class.getSimpleName();
public static final String RESOURCE_PACKAGE_NAME = R.class.getPackage().getName();
private static final String DEFAULT_MAIN_DICT = "main";
private static final String MAIN_DICT_PREFIX = "main_";
public static final String MAIN_DICTIONARY_INTERNAL_FILE_NAME = DEFAULT_MAIN_DICT + ".dict";
public static final String MAIN_DICT_PREFIX = DEFAULT_MAIN_DICT + "_";
public static final String MAIN_DICTIONARY_USER_FILE_NAME = MAIN_DICT_PREFIX + "user.dict";
private static final String DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION = "[" + BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + "_]";
private static final String DECODER_DICT_SUFFIX = DecoderSpecificConstants.DECODER_DICT_SUFFIX;
// 6 digits - unicode is limited to 21 bits
private static final int MAX_HEX_DIGITS_FOR_CODEPOINT = 6;
@ -214,10 +214,12 @@ public class DictionaryInfoUtils {
@Nullable
public static String getCategoryFromFileName(@Nonnull final String fileName) {
final String id = getWordListIdFromFileName(fileName);
final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
final String[] idArray = id.split(DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION);
// An id is supposed to be in format category:locale, so splitting on the separator
// should yield a 2-elements array
if (2 != idArray.length) {
// Also allow '_' as separator, this is ok for locales like pt_br because
// we're interested in the part before first separator anyway
if (1 == idArray.length) {
return null;
}
return idArray[0];
@ -227,7 +229,7 @@ public class DictionaryInfoUtils {
* Find out the cache directory associated with a specific locale.
*/
public static String getCacheDirectoryForLocale(final String locale, final Context context) {
final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale);
final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale).toLowerCase(Locale.ENGLISH);
final String absoluteDirectoryName = getWordListCacheDirectory(context) + File.separator
+ relativeDirectoryName;
final File directory = new File(absoluteDirectoryName);
@ -240,11 +242,13 @@ public class DictionaryInfoUtils {
}
public static boolean isMainWordListId(final String id) {
final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
final String[] idArray = id.split(DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION);
// An id is supposed to be in format category:locale, so splitting on the separator
// should yield a 2-elements array
if (2 != idArray.length) {
return id.startsWith(BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY);
// Also allow '_' as separator, this is ok for locales like pt_br because
// we're interested in the part before first separator anyway
if (1 == idArray.length) {
return false;
}
return BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.equals(idArray[0]);
}
@ -320,6 +324,10 @@ public class DictionaryInfoUtils {
BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + locale.toString().toLowerCase();
}
public static String getMainDictFilename(@Nonnull final String locale) {
return MAIN_DICT_PREFIX + locale.toLowerCase(Locale.ENGLISH) + ".dict";
}
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
final long offset, final long length) {
try {