mirror of
https://github.com/mastodon/mastodon.git
synced 2025-09-06 01:41:08 +00:00
Don't auto-change the language
This commit is contained in:
parent
8f2834d02a
commit
25b0143a12
|
@ -11,7 +11,6 @@ import ImmutablePureComponent from 'react-immutable-pure-component';
|
||||||
import { length } from 'stringz';
|
import { length } from 'stringz';
|
||||||
|
|
||||||
import { missingAltTextModal } from 'mastodon/initial_state';
|
import { missingAltTextModal } from 'mastodon/initial_state';
|
||||||
import { changeComposeLanguage } from 'mastodon/actions/compose';
|
|
||||||
|
|
||||||
import AutosuggestInput from 'mastodon/components/autosuggest_input';
|
import AutosuggestInput from 'mastodon/components/autosuggest_input';
|
||||||
import AutosuggestTextarea from 'mastodon/components/autosuggest_textarea';
|
import AutosuggestTextarea from 'mastodon/components/autosuggest_textarea';
|
||||||
|
@ -46,10 +45,6 @@ const messages = defineMessages({
|
||||||
reply: { id: 'compose_form.reply', defaultMessage: 'Reply' },
|
reply: { id: 'compose_form.reply', defaultMessage: 'Reply' },
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
|
||||||
currentLanguage: state.meta.get('locale'),
|
|
||||||
});
|
|
||||||
|
|
||||||
class ComposeForm extends ImmutablePureComponent {
|
class ComposeForm extends ImmutablePureComponent {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
intl: PropTypes.object.isRequired,
|
intl: PropTypes.object.isRequired,
|
||||||
|
@ -106,25 +101,6 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
handleKeyUp = async (e) => {
|
|
||||||
const text = this.getFulltextForCharacterCounting().trim();
|
|
||||||
const currentLanguage = this.props.currentLanguage;
|
|
||||||
if (!text || countLetters(text) <= 5) {
|
|
||||||
this.props.dispatch(changeComposeLanguage(currentLanguage));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
let detectedLanguage = await debouncedGuess(text);
|
|
||||||
if (!detectedLanguage) {
|
|
||||||
this.props.dispatch(changeComposeLanguage(currentLanguage));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.props.dispatch(changeComposeLanguage(detectedLanguage));
|
|
||||||
} catch {
|
|
||||||
this.props.dispatch(changeComposeLanguage(currentLanguage));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getFulltextForCharacterCounting = () => {
|
getFulltextForCharacterCounting = () => {
|
||||||
return [this.props.spoiler? this.props.spoilerText: '', countableText(this.props.text)].join('');
|
return [this.props.spoiler? this.props.spoilerText: '', countableText(this.props.text)].join('');
|
||||||
};
|
};
|
||||||
|
@ -301,7 +277,6 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
suggestions={this.props.suggestions}
|
suggestions={this.props.suggestions}
|
||||||
onFocus={this.handleFocus}
|
onFocus={this.handleFocus}
|
||||||
onKeyDown={this.handleKeyDown}
|
onKeyDown={this.handleKeyDown}
|
||||||
onKeyUp={this.handleKeyUp}
|
|
||||||
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
|
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
|
||||||
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
|
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
|
||||||
onSuggestionSelected={this.onSuggestionSelected}
|
onSuggestionSelected={this.onSuggestionSelected}
|
||||||
|
@ -338,7 +313,7 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
>
|
>
|
||||||
{intl.formatMessage(
|
{intl.formatMessage(
|
||||||
this.props.isEditing ?
|
this.props.isEditing ?
|
||||||
messages.saveChanges :
|
messages.saveChanges :
|
||||||
(this.props.isInReply ? messages.reply : messages.publish)
|
(this.props.isInReply ? messages.reply : messages.publish)
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -352,4 +327,4 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default injectIntl(connect(mapStateToProps)(ComposeForm));
|
export default injectIntl(ComposeForm);
|
||||||
|
|
|
@ -65,13 +65,12 @@ let module;
|
||||||
if (languageDetectorInGlobalThis) {
|
if (languageDetectorInGlobalThis) {
|
||||||
if (!languageDetectorSupportedAndReady) {
|
if (!languageDetectorSupportedAndReady) {
|
||||||
// …trigger the model download
|
// …trigger the model download
|
||||||
self.LanguageDetector.create();
|
globalThis.LanguageDetector.create();
|
||||||
}
|
}
|
||||||
module = await import('./language_detection_with_languagedetector');
|
module = await import('./language_detection_with_languagedetector');
|
||||||
} else {
|
} else {
|
||||||
module = await import('./language_detection_with_laude');
|
module = await import('./language_detection_with_laude');
|
||||||
}
|
}
|
||||||
const debouncedGuess = module.debouncedGuess;
|
const debouncedGuess = module.debouncedGuess;
|
||||||
console.log(debouncedGuess)
|
|
||||||
|
|
||||||
export { debouncedGuess, countLetters, ISO_639_MAP };
|
export { debouncedGuess, countLetters, ISO_639_MAP };
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { debounce } from 'lodash';
|
import { debounce } from 'lodash';
|
||||||
|
|
||||||
import { countLetters } from './language_detection';
|
|
||||||
import { urlRegex } from './url_regex';
|
import { urlRegex } from './url_regex';
|
||||||
|
|
||||||
const guessLanguage = async (text) => {
|
const guessLanguage = async (text) => {
|
||||||
|
@ -8,17 +7,15 @@ const guessLanguage = async (text) => {
|
||||||
.replace(urlRegex, '')
|
.replace(urlRegex, '')
|
||||||
.replace(/(^|[^/\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)/ig, '');
|
.replace(/(^|[^/\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)/ig, '');
|
||||||
|
|
||||||
if (countLetters(text) > 5) {
|
try {
|
||||||
try {
|
const languageDetector = await globalThis.LanguageDetector.create();
|
||||||
const languageDetector = await self.LanguageDetector.create();
|
let {detectedLanguage, confidence} = (await languageDetector.detect(text))[0];
|
||||||
let {detectedLanguage, confidence} = (await languageDetector.detect(text))[0];
|
if (confidence > 0.8) {
|
||||||
if (confidence > 0.8) {
|
detectedLanguage = detectedLanguage.split('-')[0];
|
||||||
detectedLanguage = detectedLanguage.split('-')[0];
|
return detectedLanguage;
|
||||||
return detectedLanguage;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
|
@ -26,23 +23,17 @@ const guessLanguage = async (text) => {
|
||||||
|
|
||||||
const debouncedGuess = (() => {
|
const debouncedGuess = (() => {
|
||||||
let resolver = null;
|
let resolver = null;
|
||||||
let rejecter = null;
|
|
||||||
|
|
||||||
const debounced = debounce(async (text) => {
|
const debounced = debounce((text) => {
|
||||||
try {
|
const result = guessLanguage(text);
|
||||||
const result = await guessLanguage(text);
|
if (resolver) {
|
||||||
if (resolver) {
|
resolver(result);
|
||||||
resolver(result);
|
resolver = null;
|
||||||
resolver = null;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
rejecter('');
|
|
||||||
}
|
}
|
||||||
}, 500, { maxWait: 1500, leading: true, trailing: true });
|
}, 500, { maxWait: 1500, leading: true, trailing: true });
|
||||||
|
|
||||||
return (text) => new Promise((resolve, reject) => {
|
return (text) => new Promise((resolve) => {
|
||||||
resolver = resolve;
|
resolver = resolve;
|
||||||
rejecter = reject;
|
|
||||||
debounced(text);
|
debounced(text);
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user