Spellcheck: Fallback to all xx-YY locales when xx isn't available
This commit is contained in:
parent
a12ac2aa37
commit
14b11041ea
2 changed files with 65 additions and 4 deletions
|
@ -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;
|
||||
|
|
40
test/app/spell_check_test.js
Normal file
40
test/app/spell_check_test.js
Normal file
|
@ -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',
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue