Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
feat(Spell checking): Add option to automatically detect language
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Feb 8, 2019
1 parent b56673e commit e2437f2
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 76 deletions.
10 changes: 6 additions & 4 deletions src/components/settings/services/EditServiceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ export default @observer class EditServiceForm extends Component {
isSaving: PropTypes.bool.isRequired,
isDeleting: PropTypes.bool.isRequired,
isProxyFeatureEnabled: PropTypes.bool.isRequired,
isProxyFeaturePremiumFeature: PropTypes.bool.isRequired,
isProxyPremiumFeature: PropTypes.bool.isRequired,
isSpellcheckerPremiumFeature: PropTypes.bool.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -191,7 +192,8 @@ export default @observer class EditServiceForm extends Component {
isDeleting,
onDelete,
isProxyFeatureEnabled,
isProxyFeaturePremiumFeature,
isProxyPremiumFeature,
isSpellcheckerPremiumFeature,
} = this.props;
const { intl } = this.context;

Expand Down Expand Up @@ -339,14 +341,14 @@ export default @observer class EditServiceForm extends Component {
</div>
</div>

<PremiumFeatureContainer>
<PremiumFeatureContainer condition={isSpellcheckerPremiumFeature}>
<div className="settings__settings-group">
<Select field={form.$('spellcheckerLanguage')} />
</div>
</PremiumFeatureContainer>

{isProxyFeatureEnabled && (
<PremiumFeatureContainer condition={isProxyFeaturePremiumFeature}>
<PremiumFeatureContainer condition={isProxyPremiumFeature}>
<div className="settings__settings-group">
<h3>
{intl.formatMessage(messages.headlineProxy)}
Expand Down
40 changes: 25 additions & 15 deletions src/containers/settings/EditServiceScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import { required, url, oneRequired } from '../../helpers/validation-helpers';
import { getSelectOptions } from '../../helpers/i18n-helpers';

import { config as proxyFeature } from '../../features/serviceProxy';
import { config as spellcheckerFeature } from '../../features/spellchecker';

import { SPELLCHECKER_LOCALES } from '../../i18n/languages';

import globalMessages from '../../i18n/globalMessages';

const messages = defineMessages({
name: {
id: 'settings.service.form.name',
Expand Down Expand Up @@ -83,14 +86,6 @@ const messages = defineMessages({
id: 'settings.service.form.proxy.password',
defaultMessage: '!!!Password',
},
spellcheckerLanguage: {
id: 'settings.service.form.spellcheckerLanguage',
defaultMessage: '!!!Spell checking Language',
},
spellcheckerSystemDefault: {
id: 'settings.service.form.spellcheckerLanguage.default',
defaultMessage: '!!!Use System Default ({default})',
},
});

export default @inject('stores', 'actions') @observer class EditServiceScreen extends Component {
Expand Down Expand Up @@ -118,12 +113,26 @@ export default @inject('stores', 'actions') @observer class EditServiceScreen ex
}

prepareForm(recipe, service, proxy) {
const {
intl,
} = this.context;

const {
stores,
} = this.props;

let defaultSpellcheckerLanguage = SPELLCHECKER_LOCALES[stores.settings.app.spellcheckerLanguage];

if (stores.settings.app.spellcheckerLanguage === 'automatic') {
defaultSpellcheckerLanguage = intl.formatMessage(globalMessages.spellcheckerAutomaticDetectionShort);
}

const spellcheckerLanguage = getSelectOptions({
locales: SPELLCHECKER_LOCALES,
resetToDefaultText: this.context.intl.formatMessage(messages.spellcheckerSystemDefault, { default: SPELLCHECKER_LOCALES[this.props.stores.settings.app.spellcheckerLanguage] }),
resetToDefaultText: intl.formatMessage(globalMessages.spellcheckerSystemDefault, { default: defaultSpellcheckerLanguage }),
automaticDetectionText: stores.settings.app.spellcheckerLanguage !== 'automatic' ? intl.formatMessage(globalMessages.spellcheckerAutomaticDetection) : '',
});

const { intl } = this.context;
const config = {
fields: {
name: {
Expand Down Expand Up @@ -160,13 +169,13 @@ export default @inject('stores', 'actions') @observer class EditServiceScreen ex
isDarkModeEnabled: {
label: intl.formatMessage(messages.enableDarkMode),
value: service.isDarkModeEnabled,
default: this.props.stores.settings.app.darkMode,
default: stores.settings.app.darkMode,
},
spellcheckerLanguage: {
label: intl.formatMessage(messages.spellcheckerLanguage),
label: intl.formatMessage(globalMessages.spellcheckerLanguage),
value: service.spellcheckerLanguage,
options: spellcheckerLanguage,
disabled: !this.props.stores.settings.app.enableSpellchecking,
disabled: !stores.settings.app.enableSpellchecking,
},
},
};
Expand Down Expand Up @@ -220,7 +229,7 @@ export default @inject('stores', 'actions') @observer class EditServiceScreen ex
}

if (proxy.isEnabled) {
const serviceProxyConfig = this.props.stores.settings.proxy[service.id] || {};
const serviceProxyConfig = stores.settings.proxy[service.id] || {};

Object.assign(config.fields, {
proxy: {
Expand Down Expand Up @@ -326,7 +335,8 @@ export default @inject('stores', 'actions') @observer class EditServiceScreen ex
onSubmit={d => this.onSubmit(d)}
onDelete={() => this.deleteService()}
isProxyFeatureEnabled={proxyFeature.isEnabled}
isProxyFeaturePremiumFeature={proxyFeature.isPremium}
isProxyPremiumFeature={proxyFeature.isPremium}
isSpellcheckerPremiumFeature={spellcheckerFeature.isPremium}
/>
</ErrorBoundary>
);
Expand Down
16 changes: 7 additions & 9 deletions src/containers/settings/EditSettingsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import { config as spellcheckerConfig } from '../../features/spellchecker';

import { getSelectOptions } from '../../helpers/i18n-helpers';


import EditSettingsForm from '../../components/settings/settings/EditSettingsForm';
import ErrorBoundary from '../../components/util/ErrorBoundary';

import globalMessages from '../../i18n/globalMessages';

const messages = defineMessages({
autoLaunchOnStart: {
id: 'settings.app.form.autoLaunchOnStart',
Expand Down Expand Up @@ -63,10 +64,6 @@ const messages = defineMessages({
id: 'settings.app.form.enableGPUAcceleration',
defaultMessage: '!!!Enable GPU Acceleration',
},
spellcheckerLanguage: {
id: 'settings.app.form.spellcheckerLanguage',
defaultMessage: '!!!Language for spell checking',
},
beta: {
id: 'settings.app.form.beta',
defaultMessage: '!!!Include beta versions',
Expand Down Expand Up @@ -125,6 +122,7 @@ export default @inject('stores', 'actions') @observer class EditSettingsScreen e

const spellcheckingLanguages = getSelectOptions({
locales: SPELLCHECKER_LOCALES,
automaticDetectionText: this.context.intl.formatMessage(globalMessages.spellcheckerAutomaticDetection),
});

const config = {
Expand Down Expand Up @@ -166,11 +164,11 @@ export default @inject('stores', 'actions') @observer class EditSettingsScreen e
},
enableSpellchecking: {
label: intl.formatMessage(messages.enableSpellchecking),
value: !this.props.stores.user.data.isPremium && spellcheckerConfig.isPremiumFeature ? false : settings.all.app.enableSpellchecking,
default: !this.props.stores.user.data.isPremium && spellcheckerConfig.isPremiumFeature ? false : DEFAULT_APP_SETTINGS.enableSpellchecking,
value: !this.props.stores.user.data.isPremium && spellcheckerConfig.isPremium ? false : settings.all.app.enableSpellchecking,
default: !this.props.stores.user.data.isPremium && spellcheckerConfig.isPremium ? false : DEFAULT_APP_SETTINGS.enableSpellchecking,
},
spellcheckerLanguage: {
label: intl.formatMessage(messages.spellcheckerLanguage),
label: intl.formatMessage(globalMessages.spellcheckerLanguage),
value: settings.all.app.spellcheckerLanguage,
options: spellcheckingLanguages,
default: DEFAULT_APP_SETTINGS.spellcheckerLanguage,
Expand Down Expand Up @@ -230,7 +228,7 @@ export default @inject('stores', 'actions') @observer class EditSettingsScreen e
cacheSize={cacheSize}
isClearingAllCache={isClearingAllCache}
onClearAllCache={clearAllCache}
isSpellcheckerPremiumFeature={spellcheckerConfig.isPremiumFeature}
isSpellcheckerPremiumFeature={spellcheckerConfig.isPremium}
/>
</ErrorBoundary>
);
Expand Down
8 changes: 5 additions & 3 deletions src/features/spellchecker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DEFAULT_FEATURES_CONFIG } from '../../config';
const debug = require('debug')('Franz:feature:spellchecker');

export const config = observable({
isPremiumFeature: DEFAULT_FEATURES_CONFIG.isSpellcheckerPremiumFeature,
isPremium: DEFAULT_FEATURES_CONFIG.isSpellcheckerPremiumFeature,
});

export default function init(stores) {
Expand All @@ -14,9 +14,11 @@ export default function init(stores) {
autorun(() => {
const { isSpellcheckerPremiumFeature } = stores.features.features;

config.isPremiumFeature = isSpellcheckerPremiumFeature !== undefined ? isSpellcheckerPremiumFeature : DEFAULT_FEATURES_CONFIG.isSpellcheckerPremiumFeature;
console.log('isSpellcheckerPremiumFeature', isSpellcheckerPremiumFeature);

if (!stores.user.data.isPremium && config.isPremiumFeature && stores.settings.app.enableSpellchecking) {
config.isPremium = isSpellcheckerPremiumFeature !== undefined ? isSpellcheckerPremiumFeature : DEFAULT_FEATURES_CONFIG.isSpellcheckerPremiumFeature;

if (!stores.user.data.isPremium && config.isPremium && stores.settings.app.enableSpellchecking) {
debug('Override settings.spellcheckerEnabled flag to false');

Object.assign(stores.settings.app, {
Expand Down
27 changes: 19 additions & 8 deletions src/helpers/i18n-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,33 @@ export function getLocale({
return localeStr;
}

export function getSelectOptions({ locales, resetToDefaultText = '' }) {
let options = [];
export function getSelectOptions({ locales, resetToDefaultText = '', automaticDetectionText = '' }) {
const options = [];

if (resetToDefaultText) {
options = [
options.push(
{
value: '',
label: resetToDefaultText,
}, {
value: '───',
label: '───',
disabled: true,
},
];
);
}

if (automaticDetectionText) {
options.push(
{
value: 'automatic',
label: automaticDetectionText,
},
);
}

options.push({
value: '───',
label: '───',
disabled: true,
});

Object.keys(locales).sort(Intl.Collator().compare).forEach((key) => {
options.push({
value: key,
Expand Down
16 changes: 16 additions & 0 deletions src/i18n/globalMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@ export default defineMessages({
id: 'global.notConnectedToTheInternet',
defaultMessage: '!!!You are not connected to the internet.',
},
spellcheckerLanguage: {
id: 'global.spellchecking.language',
defaultMessage: '!!!Spell checking language',
},
spellcheckerSystemDefault: {
id: 'global.spellchecker.useDefault',
defaultMessage: '!!!Use System Default ({default})',
},
spellcheckerAutomaticDetection: {
id: 'global.spellchecking.autodetect',
defaultMessage: '!!!Detect language automatically',
},
spellcheckerAutomaticDetectionShort: {
id: 'global.spellchecking.autodetect.short',
defaultMessage: '!!!Automatic',
},
});
4 changes: 4 additions & 0 deletions src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"global.api.unhealthy": "Can't connect to Franz online services",
"global.notConnectedToTheInternet": "You are not connected to the internet.",
"global.spellchecking.language": "Spell checking language",
"global.spellchecker.useDefault": "Use System Default ({default})",
"global.spellchecking.autodetect": "Detect language automatically",
"global.spellchecking.autodetect.short": "Automatic",
"welcome.signupButton": "Create a free account",
"welcome.loginButton": "Login to your account",
"welcome.slogan": "Messaging that works for you",
Expand Down
11 changes: 5 additions & 6 deletions src/webview/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,20 @@ const buildMenuTpl = (props, suggestions, isSpellcheckEnabled, defaultSpellcheck
ipcRenderer.sendToHost('set-service-spellchecker-language', 'reset');
},
},
{
type: 'separator',
visible: defaultSpellcheckerLanguage !== spellcheckerLanguage,
},
{
id: 'automaticDetection',
label: 'Automatic language detection',
type: 'radio',
visible: defaultSpellcheckerLanguage !== spellcheckerLanguage,
checked: spellcheckerLanguage === 'automatic',
click() {
debug('Detect spellchecker language automatically');
debug('Detect language automatically');
ipcRenderer.sendToHost('set-service-spellchecker-language', 'automatic');
},
},
{
type: 'separator',
visible: defaultSpellcheckerLanguage !== spellcheckerLanguage,
},
...spellcheckingLanguages],
});

Expand Down
Loading

0 comments on commit e2437f2

Please sign in to comment.