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(); });
}
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

View file

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

View file

@ -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,

View file

@ -1005,7 +1005,8 @@ base::unique_qptr<Ui::PopupMenu> 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<Ui::PopupMenu> 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(