From 14b11041ea1e8fa02bd398d52e9fe89ec10e0dcb Mon Sep 17 00:00:00 2001 From: Ken Powers Date: Wed, 15 Apr 2020 16:44:46 -0400 Subject: [PATCH] Spellcheck: Fallback to all xx-YY locales when xx isn't available --- app/spell_check.js | 29 ++++++++++++++++++++++---- test/app/spell_check_test.js | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 test/app/spell_check_test.js diff --git a/app/spell_check.js b/app/spell_check.js index 98e9a3425d9b..36a7e8e41675 100644 --- a/app/spell_check.js +++ b/app/spell_check.js @@ -3,15 +3,34 @@ const { Menu, clipboard } = require('electron'); const osLocale = require('os-locale'); +const { uniq } = require('lodash'); + +function getLanguages(userLocale, availableLocales) { + const baseLocale = userLocale.split('-')[0]; + // Attempt to find the exact locale + const candidateLocales = uniq([userLocale, baseLocale]).filter(l => + availableLocales.includes(l) + ); + + if (candidateLocales.length > 0) { + return candidateLocales; + } + + // If no languages were found then just return all locales that start with the + // base + return uniq(availableLocales.filter(l => l.startsWith(baseLocale))); +} exports.setup = (browserWindow, messages) => { const { session } = browserWindow.webContents; const userLocale = osLocale.sync().replace(/_/g, '-'); - const userLocales = [userLocale, userLocale.split('-')[0]]; - const available = session.availableSpellCheckerLanguages; - const languages = userLocales.filter(l => available.includes(l)); + const availableLocales = session.availableSpellCheckerLanguages; + const languages = getLanguages(userLocale, availableLocales); console.log(`spellcheck: user locale: ${userLocale}`); - console.log('spellcheck: available spellchecker languages: ', available); + console.log( + 'spellcheck: available spellchecker languages: ', + availableLocales + ); console.log('spellcheck: setting languages to: ', languages); session.setSpellCheckerLanguages(languages); @@ -100,3 +119,5 @@ exports.setup = (browserWindow, messages) => { } }); }; + +exports.getLanguages = getLanguages; diff --git a/test/app/spell_check_test.js b/test/app/spell_check_test.js new file mode 100644 index 000000000000..c33a1a5797f3 --- /dev/null +++ b/test/app/spell_check_test.js @@ -0,0 +1,40 @@ +const { assert } = require('chai'); + +const { getLanguages } = require('../../app/spell_check'); + +describe('SpellCheck', () => { + describe('getLanguages', () => { + it('works with locale and base available', () => { + assert.deepEqual(getLanguages('en-US', ['en-US', 'en-CA', 'en']), [ + 'en-US', + 'en', + ]); + }); + + it('works with neither locale nor base available', () => { + assert.deepEqual(getLanguages('en-US', ['en-NZ', 'en-CA']), [ + 'en-NZ', + 'en-CA', + ]); + }); + + it('works with only base locale available', () => { + assert.deepEqual(getLanguages('en-US', ['en', 'en-CA']), ['en']); + }); + + it('works with only full locale available', () => { + assert.deepEqual(getLanguages('en-US', ['en-CA', 'en-US']), ['en-US']); + }); + + it('works with base provided and base available', () => { + assert.deepEqual(getLanguages('en', ['en-CA', 'en-US', 'en']), ['en']); + }); + + it('works with base provided and base not available', () => { + assert.deepEqual(getLanguages('en', ['en-CA', 'en-US']), [ + 'en-CA', + 'en-US', + ]); + }); + }); +});