Added ability to use platform provider of translations on macOS.

This commit is contained in:
23rd
2026-03-02 14:37:01 +03:00
parent d7e8199365
commit 1e0a16a214
5 changed files with 59 additions and 2 deletions
+2
View File
@@ -6777,6 +6777,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_translate_settings_subtitle" = "Translate Messages";
"lng_translate_settings_show" = "Show Translate Button";
"lng_translate_settings_use_platform_mac" = "Use Apple Translations";
"lng_translate_settings_use_platform_mac_about" = "Translation on macOS won't work until you download local language packs in System Settings.";
"lng_translate_settings_chat" = "Translate Entire Chats";
"lng_translate_settings_choose" = "Do Not Translate";
"lng_translate_settings_about" = "The 'Translate' button will appear in the context menu of messages containing text.";
@@ -35,8 +35,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwidget.h"
#include "mainwindow.h"
#include "core/application.h"
#include "base/platform/base_platform_info.h"
#include "lang/lang_instance.h"
#include "lang/lang_cloud_manager.h"
#include "platform/platform_translate_provider.h"
#include "settings/settings_common.h"
#include "spellcheck/spellcheck_types.h"
#include "window/window_controller.h"
@@ -1217,6 +1219,36 @@ void LanguageBox::setupTop(not_null<Ui::VerticalLayout*> container) {
Core::App().saveSettingsDelayed();
}, translateEnabled->lifetime());
if (Platform::IsMac() && Platform::IsTranslateProviderAvailable()) {
const auto platformTranslateWrap = container->add(
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
container,
object_ptr<Ui::VerticalLayout>(container)));
platformTranslateWrap->toggle(
translateEnabled->toggled(),
anim::type::instant);
platformTranslateWrap->toggleOn(translateEnabled->toggledValue());
const auto platformTranslateEnabled = platformTranslateWrap->entity()->add(
object_ptr<Ui::SettingsButton>(
platformTranslateWrap->entity(),
tr::lng_translate_settings_use_platform_mac(),
st::settingsButtonNoIcon))->toggleOn(
rpl::single(
Core::App().settings().usePlatformTranslation()));
platformTranslateEnabled->toggledValue(
) | rpl::filter([](bool checked) {
return (checked
!= Core::App().settings().usePlatformTranslation());
}) | rpl::on_next([=](bool checked) {
Core::App().settings().setUsePlatformTranslation(checked);
Core::App().saveSettingsDelayed();
}, platformTranslateEnabled->lifetime());
Ui::AddSkip(platformTranslateWrap->entity());
Ui::AddDividerText(
platformTranslateWrap->entity(),
tr::lng_translate_settings_use_platform_mac_about());
}
using namespace rpl::mappers;
auto premium = Data::AmPremiumValue(&_controller->session());
const auto translateChat = container->add(object_ptr<Ui::SettingsButton>(
+16 -2
View File
@@ -246,7 +246,7 @@ QByteArray Settings::serialize() const {
+ sizeof(ushort)
+ sizeof(qint32) // _notificationsDisplayChecksum
+ Serialize::bytearraySize(callPanelPosition)
+ sizeof(qint32) * 2; // _cornerReply + _systemAccentColorEnabled
+ sizeof(qint32) * 3; // _cornerReply + _systemAccentColorEnabled + _usePlatformTranslation
auto result = QByteArray();
result.reserve(size);
@@ -413,7 +413,8 @@ QByteArray Settings::serialize() const {
<< _notificationsDisplayChecksum
<< callPanelPosition
<< qint32(_cornerReply.current() ? 1 : 0)
<< qint32(_systemAccentColorEnabled ? 1 : 0);
<< qint32(_systemAccentColorEnabled ? 1 : 0)
<< qint32(_usePlatformTranslation ? 1 : 0);
}
Ensures(result.size() == size);
@@ -550,6 +551,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
qint32 systemAccentColorEnabled = _systemAccentColorEnabled
? 1
: 0;
qint32 usePlatformTranslation = _usePlatformTranslation ? 1 : 0;
stream >> themesAccentColors;
if (!stream.atEnd()) {
@@ -896,6 +898,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) {
stream >> systemAccentColorEnabled;
}
if (!stream.atEnd()) {
stream >> usePlatformTranslation;
}
if (stream.status() != QDataStream::Ok) {
LOG(("App Error: "
"Bad data for Core::Settings::constructFromSerialized()"));
@@ -937,6 +942,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
}
_notificationsDisplayChecksum = notificationsDisplayChecksum;
_systemAccentColorEnabled = (systemAccentColorEnabled == 1);
_usePlatformTranslation = (usePlatformTranslation == 1);
_includeMutedCounter = (includeMutedCounter == 1);
_includeMutedCounterFolders = (includeMutedCounterFolders == 1);
_countUnreadMessages = (countUnreadMessages == 1);
@@ -1591,6 +1597,14 @@ bool Settings::translateButtonEnabled() const {
return _translateButtonEnabled;
}
void Settings::setUsePlatformTranslation(bool value) {
_usePlatformTranslation = value;
}
bool Settings::usePlatformTranslation() const {
return _usePlatformTranslation;
}
void Settings::setTranslateChatEnabled(bool value) {
_translateChatEnabled = value;
}
@@ -857,6 +857,8 @@ public:
void setTranslateButtonEnabled(bool value);
[[nodiscard]] bool translateButtonEnabled() const;
void setUsePlatformTranslation(bool value);
[[nodiscard]] bool usePlatformTranslation() const;
void setTranslateChatEnabled(bool value);
[[nodiscard]] bool translateChatEnabled() const;
[[nodiscard]] rpl::producer<bool> translateChatEnabledValue() const;
@@ -1098,6 +1100,7 @@ private:
HistoryView::DoubleClickQuickAction _chatQuickAction
= HistoryView::DoubleClickQuickAction();
bool _translateButtonEnabled = false;
bool _usePlatformTranslation = false;
rpl::variable<bool> _translateChatEnabled = true;
rpl::variable<int> _translateToRaw = 0;
rpl::variable<std::vector<LanguageId>> _skipTranslationLanguages;
@@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "lang/translate_provider.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "data/data_msg_id.h"
#include "data/data_peer.h"
#include "data/data_session.h"
@@ -18,6 +20,10 @@ namespace Ui {
std::unique_ptr<TranslateProvider> CreateTranslateProvider(
not_null<Main::Session*> session) {
if (Core::App().settings().usePlatformTranslation()
&& Platform::IsTranslateProviderAvailable()) {
return Platform::CreateTranslateProvider();
}
return CreateMTProtoTranslateProvider(session);
}