1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Desktop: Fixed crash when setting spellchecker language to en-IN

This commit is contained in:
Laurent Cozic 2022-10-29 11:44:12 +01:00
parent 4779891154
commit ae178016ab

View File

@ -3,6 +3,7 @@
import SpellCheckerServiceDriverBase from '@joplin/lib/services/spellChecker/SpellCheckerServiceDriverBase'; import SpellCheckerServiceDriverBase from '@joplin/lib/services/spellChecker/SpellCheckerServiceDriverBase';
import bridge from '../bridge'; import bridge from '../bridge';
import Logger from '@joplin/lib/Logger'; import Logger from '@joplin/lib/Logger';
import { languageCodeOnly, localesFromLanguageCode } from '@joplin/lib/locale';
const logger = Logger.create('SpellCheckerServiceDriverNative'); const logger = Logger.create('SpellCheckerServiceDriverNative');
@ -18,15 +19,45 @@ export default class SpellCheckerServiceDriverNative extends SpellCheckerService
// Language can be set to [] to disable spell-checking // Language can be set to [] to disable spell-checking
public setLanguages(v: string[]) { public setLanguages(v: string[]) {
// Note that in order to validate the language we need ot set it on the
// session and check if Electron has thrown an exception or not. This is
// fine because the actual languages will be set below after the calls
// to this functions.
const validateLanguage = (v: string) => {
const languagesToTry = [
v,
languageCodeOnly(v),
].concat(localesFromLanguageCode(languageCodeOnly(v), this.availableLanguages));
for (const toTry of languagesToTry) {
try {
this.session().setSpellCheckerLanguages([toTry]);
return toTry;
} catch (error) {
logger.warn(`Failed to set language to "${toTry}". Will try the next one in this list: ${JSON.stringify(languagesToTry)}`);
logger.warn('Error was:', error);
}
}
return null;
};
const effectiveLanguages: string[] = [];
for (const language of v) {
const effectiveLanguage = validateLanguage(language);
if (effectiveLanguage) effectiveLanguages.push(effectiveLanguage);
}
// If we pass an empty array, it disables spell checking // If we pass an empty array, it disables spell checking
// https://github.com/electron/electron/issues/25228 // https://github.com/electron/electron/issues/25228
if (v.length === 0) { if (effectiveLanguages.length === 0) {
this.session().setSpellCheckerLanguages([]); this.session().setSpellCheckerLanguages([]);
return; return;
} }
this.session().setSpellCheckerLanguages(v); this.session().setSpellCheckerLanguages(effectiveLanguages);
logger.info(`Set effective languages to "${v}"`); logger.info(`Set effective languages to "${effectiveLanguages}"`);
} }
public get language(): string { public get language(): string {