aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/spellchecker.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/webview/spellchecker.js')
-rw-r--r--src/webview/spellchecker.js48
1 files changed, 28 insertions, 20 deletions
diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js
index 1b2d60faf..27380676d 100644
--- a/src/webview/spellchecker.js
+++ b/src/webview/spellchecker.js
@@ -1,6 +1,7 @@
1import { webFrame } from 'electron'; 1import { webFrame } from 'electron';
2import { SpellCheckerProvider } from 'electron-hunspell'; 2import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell';
3import path from 'path'; 3import path from 'path';
4import { readFileSync } from 'fs';
4 5
5import { DICTIONARY_PATH } from '../config'; 6import { DICTIONARY_PATH } from '../config';
6import { SPELLCHECKER_LOCALES } from '../i18n/languages'; 7import { SPELLCHECKER_LOCALES } from '../i18n/languages';
@@ -10,18 +11,21 @@ const debug = require('debug')('Ferdi:spellchecker');
10let provider; 11let provider;
11let currentDict; 12let currentDict;
12let _isEnabled = false; 13let _isEnabled = false;
14let attached;
15
16const DEFAULT_LOCALE = 'en-us';
13 17
14async function loadDictionary(locale) { 18async function loadDictionary(locale) {
15 try { 19 try {
16 const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); 20 const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`);
17 await provider.loadDictionary(locale, `${fileLocation}.dic`, `${fileLocation}.aff`);
18 debug('Loaded dictionary', locale, 'from', fileLocation); 21 debug('Loaded dictionary', locale, 'from', fileLocation);
22 return provider.loadDictionary(locale, readFileSync(`${fileLocation}.dic`), readFileSync(`${fileLocation}.aff`));
19 } catch (err) { 23 } catch (err) {
20 console.error('Could not load dictionary', err); 24 console.error('Could not load dictionary', err);
21 } 25 }
22} 26}
23 27
24export async function switchDict(locale) { 28export async function switchDict(locale = DEFAULT_LOCALE) {
25 try { 29 try {
26 debug('Trying to load dictionary', locale); 30 debug('Trying to load dictionary', locale);
27 31
@@ -40,8 +44,8 @@ export async function switchDict(locale) {
40 if (currentDict) { 44 if (currentDict) {
41 provider.unloadDictionary(locale); 45 provider.unloadDictionary(locale);
42 } 46 }
43 loadDictionary(locale); 47 await loadDictionary(locale);
44 provider.switchDictionary(locale); 48 await attached.switchLanguage(locale);
45 49
46 debug('Switched dictionary to', locale); 50 debug('Switched dictionary to', locale);
47 51
@@ -52,18 +56,32 @@ export async function switchDict(locale) {
52 } 56 }
53} 57}
54 58
55export default async function initialize(languageCode = 'en-us') { 59export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) {
60 const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase());
61
62 if (locales.length >= 1) {
63 return locales[0];
64 }
65
66 return null;
67}
68
69export default async function initialize(languageCode = DEFAULT_LOCALE) {
56 try { 70 try {
57 provider = new SpellCheckerProvider(); 71 provider = new SpellCheckerProvider();
58 const locale = languageCode.toLowerCase(); 72 const locale = getSpellcheckerLocaleByFuzzyIdentifier(languageCode);
59 73
60 debug('Init spellchecker'); 74 debug('Init spellchecker');
61 await provider.initialize(); 75 await provider.initialize();
62 // await loadDictionaries();
63 76
64 debug('Available spellchecker dictionaries', provider.availableDictionaries); 77 debug('Attaching spellcheck provider');
78 attached = await attachSpellCheckProvider(provider);
79
80 const availableDictionaries = await provider.getAvailableDictionaries();
65 81
66 switchDict(locale); 82 debug('Available spellchecker dictionaries', availableDictionaries);
83
84 await switchDict(locale);
67 85
68 return provider; 86 return provider;
69 } catch (err) { 87 } catch (err) {
@@ -83,13 +101,3 @@ export function disable() {
83 currentDict = null; 101 currentDict = null;
84 } 102 }
85} 103}
86
87export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) {
88 const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase());
89
90 if (locales.length >= 1) {
91 return locales[0];
92 }
93
94 return null;
95}