aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/spellchecker.ts
blob: aa9a2350fddb332867f07a0cb4e4bb33ff0ab291 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { getCurrentWebContents } from '@electron/remote';
import { SPELLCHECKER_LOCALES } from '../i18n/languages';
import { DEFAULT_APP_SETTINGS, isMac } from '../environment';

const debug = require('debug')('Ferdi:spellchecker');

const webContents = getCurrentWebContents();
const [defaultLocale] = webContents.session.getSpellCheckerLanguages();
debug('Spellchecker default locale is', defaultLocale);

export function getSpellcheckerLocaleByFuzzyIdentifier(identifier: string) {
  const locales = Object.keys(SPELLCHECKER_LOCALES).filter((key) => key.toLocaleLowerCase() === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase());

  return locales.length >= 1 ? locales[0] : null;
}

export function switchDict(locale: string) {
  if (isMac) {
    debug('Ignoring dictionary changes on macOS');
    return;
  }

  debug('Setting spellchecker locale to', locale);

  const locales: string[] = [];

  const foundLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale);
  if (foundLocale) {
    locales.push(foundLocale);
  }

  locales.push(defaultLocale, DEFAULT_APP_SETTINGS.fallbackLocale);

  webContents.session.setSpellCheckerLanguages(locales);
}