Added translation preferences.

This commit is contained in:
23rd 2022-11-18 13:42:12 +03:00 committed by John Preston
parent 7aede75e43
commit f82bae15f0
6 changed files with 119 additions and 26 deletions

View file

@ -3390,6 +3390,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_translate_box_original" = "Original";
"lng_translate_box_error" = "Translate failed.";
"lng_translate_settings_subtitle" = "Translate Messages";
"lng_translate_settings_show" = "Show Translate Button";
"lng_translate_settings_choose" = "Do Not Translate";
"lng_translate_settings_about" = "The 'Translate' button will appear when you open a context menu on a text message.";
"lng_launch_exe_warning" = "This file has a {extension} extension.\nAre you sure you want to run it?";
"lng_launch_svg_warning" = "Opening this file can potentially expose your IP address to its sender. Continue?";
"lng_launch_exe_sure" = "Run";

View file

@ -23,18 +23,21 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/text/text_options.h"
#include "ui/painter.h"
#include "storage/localstorage.h"
#include "boxes/translate_box.h"
#include "ui/boxes/confirm_box.h"
#include "mainwidget.h"
#include "mainwindow.h"
#include "core/application.h"
#include "lang/lang_instance.h"
#include "lang/lang_cloud_manager.h"
#include "settings/settings_common.h"
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
#include "styles/style_info.h"
#include "styles/style_passport.h"
#include "styles/style_chat_helpers.h"
#include "styles/style_menu_icons.h"
#include "styles/style_settings.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QClipboard>
@ -1096,6 +1099,44 @@ void LanguageBox::prepare() {
setTitle(tr::lng_languages());
const auto topContainer = Ui::CreateChild<Ui::VerticalLayout>(this);
Settings::AddSubsectionTitle(
topContainer,
tr::lng_translate_settings_subtitle());
const auto translateEnabled = Settings::AddButton(
topContainer,
tr::lng_translate_settings_show(),
st::settingsButtonNoIcon
)->toggleOn(rpl::single(Core::App().settings().translateButtonEnabled()));
translateEnabled->toggledValue(
) | rpl::filter([](bool checked) {
return (checked != Core::App().settings().translateButtonEnabled());
}) | rpl::start_with_next([=](bool checked) {
Core::App().settings().setTranslateButtonEnabled(checked);
Core::App().saveSettingsDelayed();
}, translateEnabled->lifetime());
const auto label = lifetime().make_state<rpl::event_stream<QLocale>>();
const auto translateSkip = Settings::AddButtonWithLabel(
topContainer,
tr::lng_translate_settings_choose(),
label->events() | rpl::map(Ui::LanguageName),
st::settingsButtonNoIcon);
label->fire(QLocale(Core::App().settings().skipTranslationForLanguage()));
translateSkip->setClickedCallback([=] {
Ui::BoxShow(this).showBox(
Box(Ui::ChooseLanguageBox, [=](QLocale locale) {
label->fire(QLocale(locale));
Core::App().settings().setSkipTranslationForLanguage(
locale.language());
}),
Ui::LayerOption::KeepOther);
});
Settings::AddSkip(topContainer);
Settings::AddDividerText(
topContainer,
tr::lng_translate_settings_about());
const auto select = topContainer->add(
object_ptr<Ui::MultiSelect>(

View file

@ -7,12 +7,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "boxes/translate_box.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "data/data_peer.h"
#include "lang/lang_instance.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "mtproto/sender.h"
#include "settings/settings_common.h"
#include "spellcheck/platform/platform_language.h"
#include "ui/effects/loading_element.h"
#include "ui/layers/generic_box.h"
#include "ui/widgets/buttons.h"
@ -73,13 +76,6 @@ namespace {
};
}
[[nodiscard]] QString LanguageName(const QLocale &locale) {
return (locale.language() == QLocale::English
&& locale.country() == QLocale::UnitedStates)
? u"English"_q
: locale.nativeLanguageName();
}
class ShowButton : public RpWidget {
public:
ShowButton(not_null<Ui::RpWidget*> parent);
@ -129,6 +125,17 @@ rpl::producer<Qt::MouseButton> ShowButton::clicks() const {
} // namespace
QString LanguageName(const QLocale &locale) {
if (locale.language() == QLocale::English
&& (locale.country() == QLocale::UnitedStates
|| locale.country() == QLocale::AnyCountry)) {
return u"English"_q;
} else {
const auto name = locale.nativeLanguageName();
return name.left(1).toUpper() + name.mid(1);
}
}
void TranslateBox(
not_null<Ui::GenericBox*> box,
not_null<PeerData*> peer,
@ -137,7 +144,8 @@ void TranslateBox(
box->setWidth(st::boxWideWidth);
box->addButton(tr::lng_box_ok(), [=] { box->closeBox(); });
const auto container = box->verticalLayout();
const auto defaultId = Lang::LanguageIdOrDefault(Lang::Id());
const auto defaultId = QLocale(
Core::App().settings().skipTranslationForLanguage()).name().mid(0, 2);
const auto api = box->lifetime().make_state<MTP::Sender>(
&peer->session().mtp());
@ -264,26 +272,32 @@ void TranslateBox(
if (loading->toggled()) {
return;
}
Ui::BoxShow(box).showBox(Box([=](not_null<GenericBox*> b) {
b->setTitle(tr::lng_languages());
for (const auto &lang : Languages()) {
const auto locale = QLocale(lang);
const auto button = Settings::AddButton(
b->verticalLayout(),
rpl::single(LanguageName(locale)),
st::defaultSettingsButton);
button->setClickedCallback([=] {
state->locale.fire_copy(locale);
loading->show(anim::type::instant);
translated->hide(anim::type::instant);
send(locale.name().mid(0, 2));
b->closeBox();
});
}
b->addButton(tr::lng_cancel(), [=] { b->closeBox(); });
Ui::BoxShow(box).showBox(Box(ChooseLanguageBox, [=](QLocale locale) {
state->locale.fire_copy(locale);
loading->show(anim::type::instant);
translated->hide(anim::type::instant);
send(locale.name().mid(0, 2));
}));
});
}
void ChooseLanguageBox(
not_null<Ui::GenericBox*> box,
Fn<void(QLocale)> callback) {
box->setTitle(tr::lng_languages());
for (const auto &lang : Languages()) {
const auto locale = QLocale(lang);
const auto button = Settings::AddButton(
box->verticalLayout(),
rpl::single(LanguageName(locale)),
st::defaultSettingsButton);
button->setClickedCallback([=] {
callback(locale);
box->closeBox();
});
}
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
}
} // namespace Ui

View file

@ -13,10 +13,16 @@ namespace Ui {
class GenericBox;
[[nodiscard]] QString LanguageName(const QLocale &locale);
void TranslateBox(
not_null<Ui::GenericBox*> box,
not_null<PeerData*> peer,
MsgId msgId,
TextWithEntities text);
void ChooseLanguageBox(
not_null<Ui::GenericBox*> box,
Fn<void(QLocale)> callback);
} // namespace Ui

View file

@ -267,7 +267,8 @@ QByteArray Settings::serialize() const {
<< qint32(_chatQuickAction)
<< qint32(_hardwareAcceleratedVideo ? 1 : 0)
<< qint32(_suggestAnimatedEmoji ? 1 : 0)
<< qint32(_cornerReaction.current() ? 1 : 0);
<< qint32(_cornerReaction.current() ? 1 : 0)
<< qint32(_skipTranslationForLanguage);
}
return result;
}
@ -361,6 +362,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
qint32 chatQuickAction = static_cast<qint32>(_chatQuickAction);
qint32 suggestAnimatedEmoji = _suggestAnimatedEmoji ? 1 : 0;
qint32 cornerReaction = _cornerReaction.current() ? 1 : 0;
qint32 skipTranslationForLanguage = _skipTranslationForLanguage;
stream >> themesAccentColors;
if (!stream.atEnd()) {
@ -558,6 +560,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) {
stream >> cornerReaction;
}
if (!stream.atEnd()) {
stream >> skipTranslationForLanguage;
}
if (stream.status() != QDataStream::Ok) {
LOG(("App Error: "
"Bad data for Core::Settings::constructFromSerialized()"));
@ -727,6 +732,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
}
_suggestAnimatedEmoji = (suggestAnimatedEmoji == 1);
_cornerReaction = (cornerReaction == 1);
_skipTranslationForLanguage = skipTranslationForLanguage;
}
QString Settings::getSoundPath(const QString &key) const {
@ -1036,4 +1042,19 @@ float64 Settings::DefaultDialogsWidthRatio() {
: kDefaultDialogsWidthRatio;
}
void Settings::setTranslateButtonEnabled(bool value) {
_skipTranslationForLanguage = std::abs(_skipTranslationForLanguage)
* (value ? 1 : -1);
}
bool Settings::translateButtonEnabled() const {
return _skipTranslationForLanguage > 0;
}
void Settings::setSkipTranslationForLanguage(QLocale::Language language) {
const auto enabled = translateButtonEnabled();
_skipTranslationForLanguage = language * (enabled ? 1 : -1);
}
QLocale::Language Settings::skipTranslationForLanguage() const {
return QLocale::Language(std::abs(_skipTranslationForLanguage));
}
} // namespace Core

View file

@ -722,6 +722,11 @@ public:
return _chatQuickAction;
}
void setTranslateButtonEnabled(bool value);
[[nodiscard]] bool translateButtonEnabled() const;
void setSkipTranslationForLanguage(QLocale::Language language);
[[nodiscard]] QLocale::Language skipTranslationForLanguage() const;
[[nodiscard]] static bool ThirdColumnByDefault();
[[nodiscard]] static float64 DefaultDialogsWidthRatio();
[[nodiscard]] static qint32 SerializePlaybackSpeed(float64 speed) {
@ -836,6 +841,7 @@ private:
#endif // Q_OS_MAC
HistoryView::DoubleClickQuickAction _chatQuickAction =
HistoryView::DoubleClickQuickAction();
int _skipTranslationForLanguage = -int(QLocale::English);
bool _tabbedReplacedWithInfo = false; // per-window
rpl::event_stream<bool> _tabbedReplacedWithInfoValue; // per-window