aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/spellchecker.ts
blob: 92024b9b03edb2eff68566a9915632e69220d99c (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
import { ipcRenderer } from 'electron';
import { isMac } from '../environment';
import { SPELLCHECKER_LOCALES } from '../i18n/languages';

const debug = require('../preload-safe-debug')('Ferdium:spellchecker');

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 > 0 ? locales[0] : null;
}

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

  debug(`Setting spellchecker locale from: ${fuzzyLocale}`);
  const locale = getSpellcheckerLocaleByFuzzyIdentifier(fuzzyLocale);
  if (locale) {
    debug(`Sending spellcheck locales to host: ${locale}`);
    ipcRenderer.send('set-spellchecker-locales', { locale, serviceId });
  }
}