Respected translation preferences in sections.

This commit is contained in:
23rd 2022-11-18 17:00:19 +03:00 committed by John Preston
parent f82bae15f0
commit 937d243a4c
4 changed files with 67 additions and 24 deletions

View file

@ -300,4 +300,35 @@ void ChooseLanguageBox(
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
} }
bool SkipTranslate(TextWithEntities textWithEntities) {
const auto &text = textWithEntities.text;
if (text.isEmpty()) {
return true;
}
if (!Core::App().settings().translateButtonEnabled()) {
return true;
}
auto hasLetters = false;
constexpr auto kFirstChunk = 100;
for (auto i = 0; i < kFirstChunk; i++) {
if (i >= text.size()) {
hasLetters = true; // Rest characters are unknown.
break;
}
if (text.at(i).isLetter()) {
hasLetters = true;
break;
}
}
if (!hasLetters) {
return true;
}
const auto result = Platform::Language::Recognize(text);
if (result.unknown) {
return false;
}
const auto skip = Core::App().settings().skipTranslationForLanguage();
return (result.locale.language() == skip);
}
} // namespace Ui } // namespace Ui

View file

@ -21,6 +21,8 @@ void TranslateBox(
MsgId msgId, MsgId msgId,
TextWithEntities text); TextWithEntities text);
[[nodiscard]] bool SkipTranslate(TextWithEntities textWithEntities);
void ChooseLanguageBox( void ChooseLanguageBox(
not_null<Ui::GenericBox*> box, not_null<Ui::GenericBox*> box,
Fn<void(QLocale)> callback); Fn<void(QLocale)> callback);

View file

@ -2261,20 +2261,24 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
if (lnkPhoto || lnkDocument) { if (lnkPhoto || lnkDocument) {
const auto item = _dragStateItem; const auto item = _dragStateItem;
const auto itemId = item ? item->fullId() : FullMsgId(); const auto itemId = item ? item->fullId() : FullMsgId();
if (isUponSelected > 0 && !hasCopyRestrictionForSelected()) { if (isUponSelected > 0) {
_menu->addAction( if (!hasCopyRestrictionForSelected()) {
(isUponSelected > 1 _menu->addAction(
? tr::lng_context_copy_selected_items(tr::now) (isUponSelected > 1
: tr::lng_context_copy_selected(tr::now)), ? tr::lng_context_copy_selected_items(tr::now)
[=] { copySelectedText(); }, : tr::lng_context_copy_selected(tr::now)),
&st::menuIconCopy); [=] { copySelectedText(); },
_menu->addAction(tr::lng_context_translate_selected({}), [=] { &st::menuIconCopy);
_controller->show(Box( }
Ui::TranslateBox, if (!Ui::SkipTranslate(getSelectedText().rich)) {
item->history()->peer, _menu->addAction(tr::lng_context_translate_selected({}), [=] {
MsgId(), _controller->show(Box(
getSelectedText().rich)); Ui::TranslateBox,
}, &st::menuIconTranslate); item->history()->peer,
MsgId(),
getSelectedText().rich));
}, &st::menuIconTranslate);
}
} }
addItemActions(item, item); addItemActions(item, item);
if (!selectedState.count) { if (!selectedState.count) {
@ -2366,13 +2370,15 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
[=] { copySelectedText(); }, [=] { copySelectedText(); },
&st::menuIconCopy); &st::menuIconCopy);
} }
_menu->addAction(tr::lng_context_translate_selected({}), [=] { if (!Ui::SkipTranslate(getSelectedText().rich)) {
_controller->show(Box( _menu->addAction(tr::lng_context_translate_selected({}), [=] {
Ui::TranslateBox, _controller->show(Box(
item->history()->peer, Ui::TranslateBox,
MsgId(), item->history()->peer,
getSelectedText().rich)); MsgId(),
}, &st::menuIconTranslate); getSelectedText().rich));
}, &st::menuIconTranslate);
}
addItemActions(item, item); addItemActions(item, item);
} else { } else {
addItemActions(item, albumPartItem); addItemActions(item, albumPartItem);
@ -2433,7 +2439,8 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
if (!item->isService() if (!item->isService()
&& view && view
&& actionText.isEmpty() && actionText.isEmpty()
&& (view->hasVisibleText() || mediaHasTextForCopy)) { && (view->hasVisibleText() || mediaHasTextForCopy)
&& !Ui::SkipTranslate(item->originalText())) {
_menu->addAction(tr::lng_context_translate(tr::now), [=] { _menu->addAction(tr::lng_context_translate(tr::now), [=] {
_controller->show(Box( _controller->show(Box(
Ui::TranslateBox, Ui::TranslateBox,

View file

@ -1005,7 +1005,8 @@ base::unique_qptr<Ui::PopupMenu> FillContextMenu(
} }
}, &st::menuIconCopy); }, &st::menuIconCopy);
} }
if (request.overSelection) { if (request.overSelection
&& !Ui::SkipTranslate(list->getSelectedText().rich)) {
const auto owner = &view->history()->owner(); const auto owner = &view->history()->owner();
result->addAction(tr::lng_context_translate_selected(tr::now), [=] { result->addAction(tr::lng_context_translate_selected(tr::now), [=] {
if (const auto item = owner->message(itemId)) { if (const auto item = owner->message(itemId)) {
@ -1051,7 +1052,9 @@ base::unique_qptr<Ui::PopupMenu> FillContextMenu(
} }
}, &st::menuIconCopy); }, &st::menuIconCopy);
} }
if (!link && (view->hasVisibleText() || mediaHasTextForCopy)) { if (!link
&& (view->hasVisibleText() || mediaHasTextForCopy)
&& !Ui::SkipTranslate(item->originalText())) {
result->addAction(tr::lng_context_translate(tr::now), [=] { result->addAction(tr::lng_context_translate(tr::now), [=] {
if (const auto item = owner->message(itemId)) { if (const auto item = owner->message(itemId)) {
list->controller()->show(Box( list->controller()->show(Box(