From 9a4fdb153014c1e913b27a2980de08b84d30eb97 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 20 May 2019 14:06:34 +0200 Subject: [PATCH] Use only lt_count in langpacks. --- Telegram/Resources/langs/lang.strings | 9 ----- Telegram/SourceFiles/boxes/confirm_box.cpp | 2 +- Telegram/SourceFiles/boxes/peer_list_box.cpp | 2 +- .../SourceFiles/codegen/lang/generator.cpp | 26 +++++++------ .../SourceFiles/codegen/lang/parsed_file.cpp | 36 ++++-------------- .../SourceFiles/codegen/lang/parsed_file.h | 7 ++-- .../SourceFiles/history/history_message.cpp | 2 +- .../history/media/history_media_poll.cpp | 4 +- .../history/view/history_view_message.cpp | 2 +- .../view/history_view_top_bar_widget.cpp | 12 +++--- .../info/profile/info_profile_actions.cpp | 2 +- .../info/profile/info_profile_cover.cpp | 6 +-- .../info/profile/info_profile_members.cpp | 2 +- Telegram/SourceFiles/lang/lang_tag.cpp | 38 +++++++++---------- Telegram/SourceFiles/lang/lang_tag.h | 12 +++--- 15 files changed, 68 insertions(+), 94 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 619f83ea4..a38defcf0 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -112,8 +112,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_chat_status_unaccessible" = "group is inaccessible"; "lng_chat_status_members#one" = "{count} member"; "lng_chat_status_members#other" = "{count} members"; -"lng_chat_status_members_demical#one" = "{count_decimal} member"; -"lng_chat_status_members_demical#other" = "{count_decimal} members"; "lng_chat_status_online#one" = "{count} online"; "lng_chat_status_online#other" = "{count} online"; "lng_chat_status_members_online" = "{members_count}, {online_count}"; @@ -121,9 +119,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_channel_status" = "channel"; "lng_group_status" = "group"; -"lng_channel_views#one" = "{count_short}"; -"lng_channel_views#other" = "{count_short}"; - "lng_flood_error" = "Too many tries. Please try again later."; "lng_gif_error" = "An error has occurred while reading GIF animation :("; "lng_edit_error" = "You cannot edit this message"; @@ -1968,10 +1963,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_polls_closed" = "Final results"; "lng_polls_votes_count#one" = "{count} vote"; "lng_polls_votes_count#other" = "{count} votes"; -"lng_polls_votes_count_short#one" = "{count_short} vote"; -"lng_polls_votes_count_short#other" = "{count_short} votes"; -"lng_polls_votes_count_demical#one" = "{count_decimal} vote"; -"lng_polls_votes_count_demical#other" = "{count_decimal} votes"; "lng_polls_votes_none" = "No votes"; "lng_polls_retract" = "Retract vote"; "lng_polls_stop" = "Stop poll"; diff --git a/Telegram/SourceFiles/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index 4a2e10bba..8a3ae1b24 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -790,7 +790,7 @@ ConfirmInviteBox::ConfirmInviteBox( const auto status = [&] { if (_participants.empty() || _participants.size() >= count) { if (count > 0) { - return lng_chat_status_members_demical(lt_count_decimal, count); + return lng_chat_status_members(lt_count_decimal, count); } else { return lang(_isChannel ? lng_channel_status diff --git a/Telegram/SourceFiles/boxes/peer_list_box.cpp b/Telegram/SourceFiles/boxes/peer_list_box.cpp index 8da8f1522..4e7cb0d4f 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_box.cpp @@ -382,7 +382,7 @@ void PeerListRow::refreshStatus() { if (!chat->amIn()) { setStatusText(lang(lng_chat_status_unaccessible)); } else if (chat->count > 0) { - setStatusText(lng_chat_status_members_demical(lt_count_decimal, chat->count)); + setStatusText(lng_chat_status_members(lt_count_decimal, chat->count)); } else { setStatusText(lang(lng_group_status)); } diff --git a/Telegram/SourceFiles/codegen/lang/generator.cpp b/Telegram/SourceFiles/codegen/lang/generator.cpp index 83b1579f9..9d4baa00e 100644 --- a/Telegram/SourceFiles/codegen/lang/generator.cpp +++ b/Telegram/SourceFiles/codegen/lang/generator.cpp @@ -110,7 +110,18 @@ constexpr auto kTagsCount = " << langpack_.tags.size() << ";\n\ header_->popNamespace().newline(); auto index = 0; for (auto &tag : langpack_.tags) { - header_->stream() << "enum lngtag_" << tag.tag << " { lt_" << tag.tag << " = " << index++ << " };\n"; + if (tag.tag == kPluralTags[0]) { + auto elements = QStringList(); + header_->stream() + << "enum lngtag_" << tag.tag << " : int { "; + for (auto i = 0; i != kPluralTags.size(); ++i) { + elements.push_back("lt_" + kPluralTags[i] + " = " + QString::number(index + i * 1000)); + } + header_->stream() << elements.join(", ") << " };\n"; + ++index; + } else { + header_->stream() << "enum lngtag_" << tag.tag << " : int { lt_" << tag.tag << " = " << index++ << " };\n"; + } } header_->stream() << "\ \n\ @@ -126,13 +137,6 @@ enum LangKey : int {\n"; QString lang(LangKey key);\n\ \n"; - // Plural Type Map. - std::map pluralType { - { kPluralTag, QString("None") }, - { kPluralShortTag, QString("Short") }, - { kPluralDecimalSeparationTag, QString("DecimalSeparation") } - }; - for (auto &entry : langpack_.entries) { auto isPlural = !entry.keyBase.isEmpty(); auto &key = entry.key; @@ -143,11 +147,11 @@ QString lang(LangKey key);\n\ auto nonPluralTagFound = false; for (auto &tagData : entry.tags) { auto &tag = tagData.tag; - auto isPluralTag = isPlural && (pluralType.find(tag) != pluralType.end()); - genericParams.push_back("lngtag_" + tag + ", " + (isPluralTag ? "float64 " : "const ResultString &") + tag + "__val"); + auto isPluralTag = isPlural && (tag == kPluralTags[0]); + genericParams.push_back("lngtag_" + tag + (isPluralTag ? " type" : "") + ", " + (isPluralTag ? "float64 " : "const ResultString &") + tag + "__val"); params.push_back("lngtag_" + tag + ", " + (isPluralTag ? "float64 " : "const QString &") + tag + "__val"); if (isPluralTag) { - plural = "\tauto plural = Lang::Plural(" + key + ", " + tag + "__val, Lang::PluralType::" + pluralType[tag] + ");\n"; + plural = "\tauto plural = Lang::Plural(" + key + ", " + tag + "__val, type);\n"; applyTags.push_back("\tresult = Lang::ReplaceTag::Call(std::move(result), lt_" + tag + ", Lang::StartReplacements::Call(std::move(plural.replacement)));\n"); } else { nonPluralTagFound = true; diff --git a/Telegram/SourceFiles/codegen/lang/parsed_file.cpp b/Telegram/SourceFiles/codegen/lang/parsed_file.cpp index 823aa977a..b7003a649 100644 --- a/Telegram/SourceFiles/codegen/lang/parsed_file.cpp +++ b/Telegram/SourceFiles/codegen/lang/parsed_file.cpp @@ -75,15 +75,11 @@ const std::array kPluralParts = { { "other", } }; -const QString kPluralTag = "count"; -const QString kPluralShortTag = kPluralTag + "_short"; -const QString kPluralDecimalSeparationTag = kPluralTag + "_decimal"; - -const std::set pluralTypeSet { - kPluralTag, - kPluralShortTag, - kPluralDecimalSeparationTag -}; +const std::array kPluralTags = { { + "count", + "count_short", + "count_decimal", +} }; QString ComputePluralKey(const QString &base, int index) { return base + "__plural" + QString::number(index); @@ -153,7 +149,7 @@ void ParsedFile::fillPluralTags() { } } logAssert(!tags.empty()); - logAssert(pluralTypeSet.find(tags.front().tag) != pluralTypeSet.end()); + logAssert(tags.front().tag == kPluralTags[0]); // Set this tags list to all plural variants. for (auto j = i; j != i + kPluralPartCount; ++j) { @@ -217,15 +213,6 @@ QString ParsedFile::extractTagData(const QString &tagText, LangPack *to) { logErrorBadString() << "duplicate found for tag '" << tagText.toStdString() << "'"; return QString(); } - //Don't use both plural tags in one string. - if (previousTag.tag.startsWith(kPluralTag) && tag.startsWith(kPluralTag)) { - logErrorBadString() << - "duplicate found for count tag '" << - previousTag.tag.toStdString() << - "' and '" << - tag.toStdString() << "'"; - return QString(); - } } auto index = 0; auto tagIndex = result_.tags.size(); @@ -326,20 +313,13 @@ void ParsedFile::addEntity(QString key, const QString &value) { realEntry.value = entry.value; // Add all new tags to the existing ones. - realEntry.tags = std::vector(0); + realEntry.tags = std::vector(1, LangPack::Tag{ kPluralTags[0] }); for (auto &tag : entry.tags) { if (std::find(realEntry.tags.begin(), realEntry.tags.end(), tag) == realEntry.tags.end()) { - if (pluralTypeSet.find(tag.tag) != pluralTypeSet.end()) { - realEntry.tags.insert(realEntry.tags.begin(), tag); - } else { - realEntry.tags.push_back(tag); - } + realEntry.tags.push_back(tag); } } - if (realEntry.tags.empty()) { - realEntry.tags.push_back(LangPack::Tag { kPluralTag }); - } } else { result_.entries.push_back(entry); for (auto &pluralEntry : tagsData.entries) { diff --git a/Telegram/SourceFiles/codegen/lang/parsed_file.h b/Telegram/SourceFiles/codegen/lang/parsed_file.h index e05c0a2ce..948eb247d 100644 --- a/Telegram/SourceFiles/codegen/lang/parsed_file.h +++ b/Telegram/SourceFiles/codegen/lang/parsed_file.h @@ -20,9 +20,10 @@ namespace lang { constexpr auto kPluralPartCount = 6; extern const std::array kPluralParts; -extern const QString kPluralTag; -extern const QString kPluralShortTag; -extern const QString kPluralDecimalSeparationTag; + +constexpr auto kPluralTagsCount = 3; +extern const std::array kPluralTags; + QString ComputePluralKey(const QString &base, int index); struct LangPack { diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 4fb83354a..7e37f7405 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -1099,7 +1099,7 @@ void HistoryMessage::setViewsCount(int32 count) { const auto was = views->_viewsWidth; views->_views = count; views->_viewsText = (views->_views > 0) - ? lng_channel_views(lt_count_short, views->_views) + ? Lang::FormatCountToShort(views->_views).string : QString(); views->_viewsWidth = views->_viewsText.isEmpty() ? 0 diff --git a/Telegram/SourceFiles/history/media/history_media_poll.cpp b/Telegram/SourceFiles/history/media/history_media_poll.cpp index ca767adaf..6c7ecd8d6 100644 --- a/Telegram/SourceFiles/history/media/history_media_poll.cpp +++ b/Telegram/SourceFiles/history/media/history_media_poll.cpp @@ -387,7 +387,7 @@ void HistoryPoll::updateTotalVotes() { _totalVotes = _poll->totalVoters; const auto string = !_totalVotes ? lang(lng_polls_votes_none) - : lng_polls_votes_count_short(lt_count_short, _totalVotes); + : lng_polls_votes_count(lt_count_short, _totalVotes); _totalVotesLabel.setText(st::msgDateTextStyle, string); } @@ -810,7 +810,7 @@ TextState HistoryPoll::textState(QPoint point, StateRequest request) const { using Flag = Text::StateRequest::Flag; if (request.flags & Flag::LookupCustomTooltip) { result.customTooltipText = answer.votes - ? lng_polls_votes_count_demical(lt_count_decimal, answer.votes) + ? lng_polls_votes_count(lt_count_decimal, answer.votes) : lang(lng_polls_votes_none); } } diff --git a/Telegram/SourceFiles/history/view/history_view_message.cpp b/Telegram/SourceFiles/history/view/history_view_message.cpp index d5bbbbc29..d2350158b 100644 --- a/Telegram/SourceFiles/history/view/history_view_message.cpp +++ b/Telegram/SourceFiles/history/view/history_view_message.cpp @@ -1798,7 +1798,7 @@ void Message::initTime() { } if (const auto views = item->Get()) { views->_viewsText = (views->_views > 0) - ? lng_channel_views(lt_count_short, views->_views) + ? Lang::FormatCountToShort(views->_views).string : QString(); views->_viewsWidth = views->_viewsText.isEmpty() ? 0 diff --git a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp index 34b9c5e18..5902f00e5 100644 --- a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp @@ -775,7 +775,7 @@ void TopBarWidget::updateOnlineDisplay() { } else if (chat->count <= 0) { text = lang(lng_group_status); } else { - text = lng_chat_status_members_demical(lt_count_decimal, chat->count); + text = lng_chat_status_members(lt_count_decimal, chat->count); } } else { const auto self = Auth().user(); @@ -788,11 +788,11 @@ void TopBarWidget::updateOnlineDisplay() { } } if (online > 0 && !onlyMe) { - auto membersCount = lng_chat_status_members_demical(lt_count_decimal, chat->participants.size()); + auto membersCount = lng_chat_status_members(lt_count_decimal, chat->participants.size()); auto onlineCount = lng_chat_status_online(lt_count, online); text = lng_chat_status_members_online(lt_members_count, membersCount, lt_online_count, onlineCount); } else if (chat->participants.size() > 0) { - text = lng_chat_status_members_demical(lt_count_decimal, chat->participants.size()); + text = lng_chat_status_members(lt_count_decimal, chat->participants.size()); } else { text = lang(lng_group_status); } @@ -814,16 +814,16 @@ void TopBarWidget::updateOnlineDisplay() { } } if (online && !onlyMe) { - auto membersCount = lng_chat_status_members_demical(lt_count_decimal, channel->membersCount()); + auto membersCount = lng_chat_status_members(lt_count_decimal, channel->membersCount()); auto onlineCount = lng_chat_status_online(lt_count, online); text = lng_chat_status_members_online(lt_members_count, membersCount, lt_online_count, onlineCount); } else if (channel->membersCount() > 0) { - text = lng_chat_status_members_demical(lt_count_decimal, channel->membersCount()); + text = lng_chat_status_members(lt_count_decimal, channel->membersCount()); } else { text = lang(lng_group_status); } } else if (channel->membersCount() > 0) { - text = lng_chat_status_members_demical(lt_count_decimal, channel->membersCount()); + text = lng_chat_status_members(lt_count_decimal, channel->membersCount()); } else { text = lang(channel->isMegagroup() ? lng_group_status : lng_channel_status); } diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 1af0f0f62..066d765d7 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -802,7 +802,7 @@ object_ptr SetupChannelMembers( auto membersText = MembersCountValue( channel ) | rpl::map([](int count) { - return lng_chat_status_members_demical(lt_count_decimal, count); + return lng_chat_status_members(lt_count_decimal, count); }); auto membersCallback = [controller, channel] { controller->showSection(Info::Memento( diff --git a/Telegram/SourceFiles/info/profile/info_profile_cover.cpp b/Telegram/SourceFiles/info/profile/info_profile_cover.cpp index dd7f44006..f4e5e36f9 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_cover.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_cover.cpp @@ -126,11 +126,11 @@ bool SectionToggle::checkRippleStartPosition(QPoint position) const { } auto MembersStatusText(int count) { - return lng_chat_status_members_demical(lt_count_decimal, count); + return lng_chat_status_members(lt_count_decimal, count); }; auto OnlineStatusText(int count) { - return lng_chat_status_online(lt_count, count); + return lng_chat_status_online(lt_count_decimal, count); }; auto ChatStatusText(int fullCount, int onlineCount, bool isGroup) { @@ -139,7 +139,7 @@ auto ChatStatusText(int fullCount, int onlineCount, bool isGroup) { lt_members_count, MembersStatusText(fullCount), lt_online_count, OnlineStatusText(onlineCount)); } else if (fullCount > 0) { - return lng_chat_status_members_demical(lt_count_decimal, fullCount); + return lng_chat_status_members(lt_count_decimal, fullCount); } return lang(isGroup ? lng_group_status diff --git a/Telegram/SourceFiles/info/profile/info_profile_members.cpp b/Telegram/SourceFiles/info/profile/info_profile_members.cpp index 2613740a0..cb0cf4853 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_members.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_members.cpp @@ -166,7 +166,7 @@ object_ptr Members::setupTitle() { MembersCountValue( _peer ) | rpl::map([](int count) { - return lng_chat_status_members_demical(lt_count_decimal, count); + return lng_chat_status_members(lt_count_decimal, count); }) | ToUpperValue(), st::infoBlockHeaderLabel); result->setAttribute(Qt::WA_TransparentForMouseEvents); diff --git a/Telegram/SourceFiles/lang/lang_tag.cpp b/Telegram/SourceFiles/lang/lang_tag.cpp index 0569cf9a2..2495e7429 100644 --- a/Telegram/SourceFiles/lang/lang_tag.cpp +++ b/Telegram/SourceFiles/lang/lang_tag.cpp @@ -914,48 +914,44 @@ int NonZeroPartToInt(QString value) { : (value.isEmpty() ? 0 : value.toInt()); } -inline QString FormatCountToShort(int64 &number) { +ShortenedCount FormatCountToShort(int64 number) { + auto result = ShortenedCount{ number }; const auto abs = std::abs(number); - auto result = QString(); const auto shorten = [&](int64 divider, char multiplier) { const auto sign = (number > 0) ? 1 : -1; const auto rounded = abs / (divider / 10); - result = QString::number(sign * rounded / 10); + result.string = QString::number(sign * rounded / 10); if (rounded % 10) { - result += '.' + QString::number(rounded % 10) + multiplier; + result.string += '.' + QString::number(rounded % 10) + multiplier; } else { - result += multiplier; + result.string += multiplier; } // Update given number. // E.g. 12345 will be 12000. - number = rounded * divider; + result.number = rounded * divider; }; if (abs >= 1'000'000) { shorten(1'000'000, 'M'); } else if (abs >= 10'000) { shorten(1'000, 'K'); } else { - result = QString::number(number); + result.string = QString::number(number); } return result; } PluralResult Plural( - ushort keyBase, - float64 value, - PluralType type) { - + ushort keyBase, + float64 value, + lngtag_count type) { // To correctly select a shift for PluralType::Short // we must first round the number. - int64 shortenedValue = 0; - auto shortenedNumberString = QString(); - if (type == PluralType::Short) { - shortenedValue = qRound(value); - shortenedNumberString = FormatCountToShort(shortenedValue); - } + const auto shortened = (type == lt_count_short) + ? FormatCountToShort(qRound(value)) + : ShortenedCount(); // Simplified. - const auto n = std::abs(shortenedValue ? float64(shortenedValue) : value); + const auto n = std::abs(shortened.number ? float64(shortened.number) : value); const auto i = qFloor(n); const auto integer = (qCeil(n) == i); const auto formatted = integer ? QString() : FormatDouble(n); @@ -979,9 +975,9 @@ PluralResult Plural( auto string = langpack.getValue(LangKey(keyBase + shift)); if (integer) { const auto round = qRound(value); - if (type == PluralType::Short) { - return { string, shortenedNumberString }; - } else if (type == PluralType::DecimalSeparation) { + if (type == lt_count_short) { + return { string, shortened.string }; + } else if (type == lt_count_decimal) { return { string, QString("%L1").arg(round) }; } return { string, QString::number(round) }; diff --git a/Telegram/SourceFiles/lang/lang_tag.h b/Telegram/SourceFiles/lang/lang_tag.h index 87e8ba88d..12fa99e65 100644 --- a/Telegram/SourceFiles/lang/lang_tag.h +++ b/Telegram/SourceFiles/lang/lang_tag.h @@ -7,17 +7,19 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +enum lngtag_count : int; + namespace Lang { constexpr auto kTagReplacementSize = 4; int FindTagReplacementPosition(const QString &original, ushort tag); -enum class PluralType { - None, - Short, - DecimalSeparation, +struct ShortenedCount { + int64 number = 0; + QString string; }; +ShortenedCount FormatCountToShort(int64 number); struct PluralResult { QString string; @@ -26,7 +28,7 @@ struct PluralResult { PluralResult Plural( ushort keyBase, float64 value, - PluralType type = PluralType::None); + lngtag_count type); void UpdatePluralRules(const QString &languageId); template