mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added internal support of skipping translation for multiple languages.
This commit is contained in:
parent
ccb3bbea15
commit
a043e22622
6 changed files with 87 additions and 45 deletions
|
@ -493,6 +493,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
"lng_settings_change_lang" = "Change language";
|
"lng_settings_change_lang" = "Change language";
|
||||||
"lng_languages" = "Languages";
|
"lng_languages" = "Languages";
|
||||||
"lng_languages_none" = "No languages found.";
|
"lng_languages_none" = "No languages found.";
|
||||||
|
"lng_languages_count#one" = "{count} language";
|
||||||
|
"lng_languages_count#other" = "{count} languages";
|
||||||
"lng_sure_save_language" = "Telegram will restart in order to change language";
|
"lng_sure_save_language" = "Telegram will restart in order to change language";
|
||||||
"lng_settings_update_automatically" = "Update automatically";
|
"lng_settings_update_automatically" = "Update automatically";
|
||||||
"lng_settings_install_beta" = "Install beta versions";
|
"lng_settings_install_beta" = "Install beta versions";
|
||||||
|
|
|
@ -1117,7 +1117,8 @@ void LanguageBox::prepare() {
|
||||||
Core::App().saveSettingsDelayed();
|
Core::App().saveSettingsDelayed();
|
||||||
}, translateEnabled->lifetime());
|
}, translateEnabled->lifetime());
|
||||||
|
|
||||||
const auto label = lifetime().make_state<rpl::event_stream<QLocale>>();
|
using Locales = std::vector<QLocale>;
|
||||||
|
const auto label = lifetime().make_state<rpl::event_stream<Locales>>();
|
||||||
const auto translateSkipWrap = topContainer->add(
|
const auto translateSkipWrap = topContainer->add(
|
||||||
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
||||||
topContainer,
|
topContainer,
|
||||||
|
@ -1129,28 +1130,24 @@ void LanguageBox::prepare() {
|
||||||
const auto translateSkip = Settings::AddButtonWithLabel(
|
const auto translateSkip = Settings::AddButtonWithLabel(
|
||||||
translateSkipWrap->entity(),
|
translateSkipWrap->entity(),
|
||||||
tr::lng_translate_settings_choose(),
|
tr::lng_translate_settings_choose(),
|
||||||
label->events() | rpl::map(Ui::LanguageName),
|
label->events(
|
||||||
|
) | rpl::map([](const Locales &locales) {
|
||||||
|
return (locales.size() > 1)
|
||||||
|
? tr::lng_languages_count(tr::now, lt_count, locales.size())
|
||||||
|
: Ui::LanguageName(locales.front());
|
||||||
|
}),
|
||||||
st::settingsButtonNoIcon);
|
st::settingsButtonNoIcon);
|
||||||
|
|
||||||
{
|
label->fire(Ui::Translate::LocalesFromSettings());
|
||||||
const auto settingsLang =
|
|
||||||
Core::App().settings().skipTranslationForLanguage();
|
|
||||||
const auto locale = (settingsLang == QLocale::English)
|
|
||||||
? QLocale(Lang::LanguageIdOrDefault(Lang::Id()))
|
|
||||||
: (settingsLang == QLocale::C)
|
|
||||||
? QLocale(QLocale::English)
|
|
||||||
: QLocale(settingsLang);
|
|
||||||
label->fire_copy(locale);
|
|
||||||
}
|
|
||||||
translateSkip->setClickedCallback([=] {
|
translateSkip->setClickedCallback([=] {
|
||||||
Ui::BoxShow(this).showBox(
|
Ui::BoxShow(this).showBox(
|
||||||
Box(Ui::ChooseLanguageBox, [=](QLocale locale) {
|
Box(Ui::ChooseLanguageBox, [=](QLocale locale) {
|
||||||
label->fire_copy(locale);
|
label->fire({ locale });
|
||||||
const auto result = (locale.language() == QLocale::English)
|
const auto result = (locale.language() == QLocale::English)
|
||||||
? QLocale::c()
|
? QLocale::c()
|
||||||
: locale;
|
: locale;
|
||||||
Core::App().settings().setSkipTranslationForLanguage(
|
Core::App().settings().setSkipTranslationForLanguages(
|
||||||
result.language());
|
{ result.language() });
|
||||||
Core::App().saveSettingsDelayed();
|
Core::App().saveSettingsDelayed();
|
||||||
}),
|
}),
|
||||||
Ui::LayerOption::KeepOther);
|
Ui::LayerOption::KeepOther);
|
||||||
|
|
|
@ -129,6 +129,29 @@ rpl::producer<Qt::MouseButton> ShowButton::clicks() const {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace Translate {
|
||||||
|
|
||||||
|
std::vector<QLocale> LocalesFromSettings() {
|
||||||
|
const auto langs = Core::App().settings().skipTranslationForLanguages();
|
||||||
|
if (langs.empty()) {
|
||||||
|
return { QLocale(QLocale::English) };
|
||||||
|
}
|
||||||
|
return ranges::views::all(
|
||||||
|
langs
|
||||||
|
) | ranges::view::transform([](int langId) {
|
||||||
|
const auto lang = QLocale::Language(langId);
|
||||||
|
return (lang == QLocale::English)
|
||||||
|
? QLocale(Lang::LanguageIdOrDefault(Lang::Id()))
|
||||||
|
: (lang == QLocale::C)
|
||||||
|
? QLocale(QLocale::English)
|
||||||
|
: QLocale(lang);
|
||||||
|
}) | ranges::to_vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Translate
|
||||||
|
|
||||||
|
using namespace Translate;
|
||||||
|
|
||||||
QString LanguageName(const QLocale &locale) {
|
QString LanguageName(const QLocale &locale) {
|
||||||
if (locale.language() == QLocale::English
|
if (locale.language() == QLocale::English
|
||||||
&& (locale.country() == QLocale::UnitedStates
|
&& (locale.country() == QLocale::UnitedStates
|
||||||
|
@ -149,13 +172,7 @@ void TranslateBox(
|
||||||
box->setWidth(st::boxWideWidth);
|
box->setWidth(st::boxWideWidth);
|
||||||
box->addButton(tr::lng_box_ok(), [=] { box->closeBox(); });
|
box->addButton(tr::lng_box_ok(), [=] { box->closeBox(); });
|
||||||
const auto container = box->verticalLayout();
|
const auto container = box->verticalLayout();
|
||||||
const auto settingsLang =
|
const auto defaultId = LocalesFromSettings().front().name().mid(0, 2);
|
||||||
Core::App().settings().skipTranslationForLanguage();
|
|
||||||
const auto defaultId = (settingsLang == QLocale::English)
|
|
||||||
? Lang::LanguageIdOrDefault(Lang::Id())
|
|
||||||
: (settingsLang == QLocale::C)
|
|
||||||
? u"en"_q
|
|
||||||
: QLocale(settingsLang).name().mid(0, 2);
|
|
||||||
|
|
||||||
const auto api = box->lifetime().make_state<MTP::Sender>(
|
const auto api = box->lifetime().make_state<MTP::Sender>(
|
||||||
&peer->session().mtp());
|
&peer->session().mtp());
|
||||||
|
@ -356,14 +373,9 @@ bool SkipTranslate(TextWithEntities textWithEntities) {
|
||||||
if (result.unknown) {
|
if (result.unknown) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const auto settingsLang =
|
return ranges::any_of(LocalesFromSettings(), [&](const QLocale &l) {
|
||||||
Core::App().settings().skipTranslationForLanguage();
|
return result.locale.language() == l.language();
|
||||||
const auto skip = (settingsLang == QLocale::English)
|
});
|
||||||
? QLocale(Lang::LanguageIdOrDefault(Lang::Id())).language()
|
|
||||||
: (settingsLang == QLocale::C)
|
|
||||||
? QLocale::English
|
|
||||||
: settingsLang;
|
|
||||||
return (result.locale.language() == skip);
|
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -10,6 +10,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
class PeerData;
|
class PeerData;
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
|
namespace Translate {
|
||||||
|
[[nodiscard]] std::vector<QLocale> LocalesFromSettings();
|
||||||
|
} // namespace Translate
|
||||||
|
|
||||||
class GenericBox;
|
class GenericBox;
|
||||||
|
|
||||||
|
|
|
@ -267,7 +267,13 @@ QByteArray Settings::serialize() const {
|
||||||
<< qint32(_hardwareAcceleratedVideo ? 1 : 0)
|
<< qint32(_hardwareAcceleratedVideo ? 1 : 0)
|
||||||
<< qint32(_suggestAnimatedEmoji ? 1 : 0)
|
<< qint32(_suggestAnimatedEmoji ? 1 : 0)
|
||||||
<< qint32(_cornerReaction.current() ? 1 : 0)
|
<< qint32(_cornerReaction.current() ? 1 : 0)
|
||||||
<< qint32(_skipTranslationForLanguage);
|
<< qint32(_translateButtonEnabled ? 1 : 0);
|
||||||
|
|
||||||
|
stream
|
||||||
|
<< qint32(_skipTranslationForLanguages.size());
|
||||||
|
for (const auto &lang : _skipTranslationForLanguages) {
|
||||||
|
stream << quint64(lang);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -361,7 +367,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
|
||||||
qint32 chatQuickAction = static_cast<qint32>(_chatQuickAction);
|
qint32 chatQuickAction = static_cast<qint32>(_chatQuickAction);
|
||||||
qint32 suggestAnimatedEmoji = _suggestAnimatedEmoji ? 1 : 0;
|
qint32 suggestAnimatedEmoji = _suggestAnimatedEmoji ? 1 : 0;
|
||||||
qint32 cornerReaction = _cornerReaction.current() ? 1 : 0;
|
qint32 cornerReaction = _cornerReaction.current() ? 1 : 0;
|
||||||
qint32 skipTranslationForLanguage = _skipTranslationForLanguage;
|
qint32 legacySkipTranslationForLanguage = _translateButtonEnabled ? 1 : 0;
|
||||||
|
qint32 skipTranslationForLanguagesCount = 0;
|
||||||
|
std::vector<int> skipTranslationForLanguages;
|
||||||
|
|
||||||
stream >> themesAccentColors;
|
stream >> themesAccentColors;
|
||||||
if (!stream.atEnd()) {
|
if (!stream.atEnd()) {
|
||||||
|
@ -560,7 +568,17 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
|
||||||
stream >> cornerReaction;
|
stream >> cornerReaction;
|
||||||
}
|
}
|
||||||
if (!stream.atEnd()) {
|
if (!stream.atEnd()) {
|
||||||
stream >> skipTranslationForLanguage;
|
stream >> legacySkipTranslationForLanguage;
|
||||||
|
}
|
||||||
|
if (!stream.atEnd()) {
|
||||||
|
stream >> skipTranslationForLanguagesCount;
|
||||||
|
if (stream.status() == QDataStream::Ok) {
|
||||||
|
for (auto i = 0; i != skipTranslationForLanguagesCount; ++i) {
|
||||||
|
quint64 language;
|
||||||
|
stream >> language;
|
||||||
|
skipTranslationForLanguages.emplace_back(language);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (stream.status() != QDataStream::Ok) {
|
if (stream.status() != QDataStream::Ok) {
|
||||||
LOG(("App Error: "
|
LOG(("App Error: "
|
||||||
|
@ -731,7 +749,18 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
|
||||||
}
|
}
|
||||||
_suggestAnimatedEmoji = (suggestAnimatedEmoji == 1);
|
_suggestAnimatedEmoji = (suggestAnimatedEmoji == 1);
|
||||||
_cornerReaction = (cornerReaction == 1);
|
_cornerReaction = (cornerReaction == 1);
|
||||||
_skipTranslationForLanguage = skipTranslationForLanguage;
|
{ // Parse the legacy translation setting.
|
||||||
|
_skipTranslationForLanguages = skipTranslationForLanguages;
|
||||||
|
if (legacySkipTranslationForLanguage == 0) {
|
||||||
|
_translateButtonEnabled = false;
|
||||||
|
} else if (legacySkipTranslationForLanguage == 1) {
|
||||||
|
_translateButtonEnabled = true;
|
||||||
|
} else {
|
||||||
|
_translateButtonEnabled = (legacySkipTranslationForLanguage > 0);
|
||||||
|
_skipTranslationForLanguages.push_back(
|
||||||
|
std::abs(legacySkipTranslationForLanguage));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Settings::getSoundPath(const QString &key) const {
|
QString Settings::getSoundPath(const QString &key) const {
|
||||||
|
@ -1042,18 +1071,16 @@ float64 Settings::DefaultDialogsWidthRatio() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setTranslateButtonEnabled(bool value) {
|
void Settings::setTranslateButtonEnabled(bool value) {
|
||||||
_skipTranslationForLanguage = std::abs(_skipTranslationForLanguage)
|
_translateButtonEnabled = value;
|
||||||
* (value ? 1 : -1);
|
|
||||||
}
|
}
|
||||||
bool Settings::translateButtonEnabled() const {
|
bool Settings::translateButtonEnabled() const {
|
||||||
return _skipTranslationForLanguage > 0;
|
return _translateButtonEnabled;
|
||||||
}
|
}
|
||||||
void Settings::setSkipTranslationForLanguage(QLocale::Language language) {
|
void Settings::setSkipTranslationForLanguages(std::vector<int> languages) {
|
||||||
const auto enabled = translateButtonEnabled();
|
_skipTranslationForLanguages = std::move(languages);
|
||||||
_skipTranslationForLanguage = language * (enabled ? 1 : -1);
|
|
||||||
}
|
}
|
||||||
QLocale::Language Settings::skipTranslationForLanguage() const {
|
std::vector<int> Settings::skipTranslationForLanguages() const {
|
||||||
return QLocale::Language(std::abs(_skipTranslationForLanguage));
|
return _skipTranslationForLanguages;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|
|
@ -724,8 +724,8 @@ public:
|
||||||
|
|
||||||
void setTranslateButtonEnabled(bool value);
|
void setTranslateButtonEnabled(bool value);
|
||||||
[[nodiscard]] bool translateButtonEnabled() const;
|
[[nodiscard]] bool translateButtonEnabled() const;
|
||||||
void setSkipTranslationForLanguage(QLocale::Language language);
|
void setSkipTranslationForLanguages(std::vector<int> languages);
|
||||||
[[nodiscard]] QLocale::Language skipTranslationForLanguage() const;
|
[[nodiscard]] std::vector<int> skipTranslationForLanguages() const;
|
||||||
|
|
||||||
[[nodiscard]] static bool ThirdColumnByDefault();
|
[[nodiscard]] static bool ThirdColumnByDefault();
|
||||||
[[nodiscard]] static float64 DefaultDialogsWidthRatio();
|
[[nodiscard]] static float64 DefaultDialogsWidthRatio();
|
||||||
|
@ -841,7 +841,8 @@ private:
|
||||||
#endif // Q_OS_MAC
|
#endif // Q_OS_MAC
|
||||||
HistoryView::DoubleClickQuickAction _chatQuickAction =
|
HistoryView::DoubleClickQuickAction _chatQuickAction =
|
||||||
HistoryView::DoubleClickQuickAction();
|
HistoryView::DoubleClickQuickAction();
|
||||||
int _skipTranslationForLanguage = -int(QLocale::English);
|
bool _translateButtonEnabled = false;
|
||||||
|
std::vector<int> _skipTranslationForLanguages;
|
||||||
|
|
||||||
bool _tabbedReplacedWithInfo = false; // per-window
|
bool _tabbedReplacedWithInfo = false; // per-window
|
||||||
rpl::event_stream<bool> _tabbedReplacedWithInfoValue; // per-window
|
rpl::event_stream<bool> _tabbedReplacedWithInfoValue; // per-window
|
||||||
|
|
Loading…
Add table
Reference in a new issue