From 937d243a4cf189a883f4ddc21b632a9999826047 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 18 Nov 2022 17:00:19 +0300 Subject: [PATCH] Respected translation preferences in sections. --- Telegram/SourceFiles/boxes/translate_box.cpp | 31 +++++++++++ Telegram/SourceFiles/boxes/translate_box.h | 2 + .../history/history_inner_widget.cpp | 51 +++++++++++-------- .../view/history_view_context_menu.cpp | 7 ++- 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/boxes/translate_box.cpp b/Telegram/SourceFiles/boxes/translate_box.cpp index 09504d258..18864b287 100644 --- a/Telegram/SourceFiles/boxes/translate_box.cpp +++ b/Telegram/SourceFiles/boxes/translate_box.cpp @@ -300,4 +300,35 @@ void ChooseLanguageBox( 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 diff --git a/Telegram/SourceFiles/boxes/translate_box.h b/Telegram/SourceFiles/boxes/translate_box.h index 9d7116f75..22daec17b 100644 --- a/Telegram/SourceFiles/boxes/translate_box.h +++ b/Telegram/SourceFiles/boxes/translate_box.h @@ -21,6 +21,8 @@ void TranslateBox( MsgId msgId, TextWithEntities text); +[[nodiscard]] bool SkipTranslate(TextWithEntities textWithEntities); + void ChooseLanguageBox( not_null box, Fn callback); diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index b122c6397..d3f2f3925 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -2261,20 +2261,24 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { if (lnkPhoto || lnkDocument) { const auto item = _dragStateItem; const auto itemId = item ? item->fullId() : FullMsgId(); - if (isUponSelected > 0 && !hasCopyRestrictionForSelected()) { - _menu->addAction( - (isUponSelected > 1 - ? tr::lng_context_copy_selected_items(tr::now) - : tr::lng_context_copy_selected(tr::now)), - [=] { copySelectedText(); }, - &st::menuIconCopy); - _menu->addAction(tr::lng_context_translate_selected({}), [=] { - _controller->show(Box( - Ui::TranslateBox, - item->history()->peer, - MsgId(), - getSelectedText().rich)); - }, &st::menuIconTranslate); + if (isUponSelected > 0) { + if (!hasCopyRestrictionForSelected()) { + _menu->addAction( + (isUponSelected > 1 + ? tr::lng_context_copy_selected_items(tr::now) + : tr::lng_context_copy_selected(tr::now)), + [=] { copySelectedText(); }, + &st::menuIconCopy); + } + if (!Ui::SkipTranslate(getSelectedText().rich)) { + _menu->addAction(tr::lng_context_translate_selected({}), [=] { + _controller->show(Box( + Ui::TranslateBox, + item->history()->peer, + MsgId(), + getSelectedText().rich)); + }, &st::menuIconTranslate); + } } addItemActions(item, item); if (!selectedState.count) { @@ -2366,13 +2370,15 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { [=] { copySelectedText(); }, &st::menuIconCopy); } - _menu->addAction(tr::lng_context_translate_selected({}), [=] { - _controller->show(Box( - Ui::TranslateBox, - item->history()->peer, - MsgId(), - getSelectedText().rich)); - }, &st::menuIconTranslate); + if (!Ui::SkipTranslate(getSelectedText().rich)) { + _menu->addAction(tr::lng_context_translate_selected({}), [=] { + _controller->show(Box( + Ui::TranslateBox, + item->history()->peer, + MsgId(), + getSelectedText().rich)); + }, &st::menuIconTranslate); + } addItemActions(item, item); } else { addItemActions(item, albumPartItem); @@ -2433,7 +2439,8 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { if (!item->isService() && view && actionText.isEmpty() - && (view->hasVisibleText() || mediaHasTextForCopy)) { + && (view->hasVisibleText() || mediaHasTextForCopy) + && !Ui::SkipTranslate(item->originalText())) { _menu->addAction(tr::lng_context_translate(tr::now), [=] { _controller->show(Box( Ui::TranslateBox, diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 06497c91a..a9aea0f34 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -1005,7 +1005,8 @@ base::unique_qptr FillContextMenu( } }, &st::menuIconCopy); } - if (request.overSelection) { + if (request.overSelection + && !Ui::SkipTranslate(list->getSelectedText().rich)) { const auto owner = &view->history()->owner(); result->addAction(tr::lng_context_translate_selected(tr::now), [=] { if (const auto item = owner->message(itemId)) { @@ -1051,7 +1052,9 @@ base::unique_qptr FillContextMenu( } }, &st::menuIconCopy); } - if (!link && (view->hasVisibleText() || mediaHasTextForCopy)) { + if (!link + && (view->hasVisibleText() || mediaHasTextForCopy) + && !Ui::SkipTranslate(item->originalText())) { result->addAction(tr::lng_context_translate(tr::now), [=] { if (const auto item = owner->message(itemId)) { list->controller()->show(Box(