Use tr:: instead of __generic.

This commit is contained in:
John Preston 2019-06-19 14:41:00 +02:00
parent 52e40cbc6e
commit d3ca6b96a1
9 changed files with 87 additions and 69 deletions

View File

@ -1500,8 +1500,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_mediaview_group_photo" = "Group Photo"; "lng_mediaview_group_photo" = "Group Photo";
"lng_mediaview_channel_photo" = "Channel Photo"; "lng_mediaview_channel_photo" = "Channel Photo";
"lng_mediaview_profile_photo" = "Profile Photo"; "lng_mediaview_profile_photo" = "Profile Photo";
"lng_mediaview_file_n_of_count" = "{file} {n} of {count}"; "lng_mediaview_file_n_of_amount" = "{file} {n} of {amount}";
"lng_mediaview_n_of_count" = "Photo {n} of {count}"; "lng_mediaview_n_of_amount" = "Photo {n} of {amount}";
"lng_mediaview_doc_image" = "File"; "lng_mediaview_doc_image" = "File";
"lng_mediaview_today" = "today at {time}"; "lng_mediaview_today" = "today at {time}";
"lng_mediaview_yesterday" = "yesterday at {time}"; "lng_mediaview_yesterday" = "yesterday at {time}";
@ -1590,7 +1590,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_call_box_status_today" = "{time}"; "lng_call_box_status_today" = "{time}";
"lng_call_box_status_yesterday" = "Yesterday at {time}"; "lng_call_box_status_yesterday" = "Yesterday at {time}";
"lng_call_box_status_date" = "{date} at {time}"; "lng_call_box_status_date" = "{date} at {time}";
"lng_call_box_status_group" = "({count}) {status}"; "lng_call_box_status_group" = "({amount}) {status}";
"lng_call_outgoing" = "Outgoing call"; "lng_call_outgoing" = "Outgoing call";
"lng_call_incoming" = "Incoming call"; "lng_call_incoming" = "Incoming call";
@ -2001,14 +2001,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_export_state_userpics" = "Profile pictures"; "lng_export_state_userpics" = "Profile pictures";
"lng_export_state_chats_list" = "Processing chats..."; "lng_export_state_chats_list" = "Processing chats...";
"lng_export_state_chats" = "Chats"; "lng_export_state_chats" = "Chats";
"lng_export_state_progress" = "{count} / {total}"; "lng_export_state_ready_progress" = "{ready} / {total}";
"lng_export_progress" = "You can close this window now. Please don't quit Telegram until the data export is completed."; "lng_export_progress" = "You can close this window now. Please don't quit Telegram until the data export is completed.";
"lng_export_stop" = "Stop"; "lng_export_stop" = "Stop";
"lng_export_sure_stop" = "Are you sure you want to stop exporting your data?\n\nIf you do, you'll need to start over."; "lng_export_sure_stop" = "Are you sure you want to stop exporting your data?\n\nIf you do, you'll need to start over.";
"lng_export_about_done" = "Your data was successfully exported."; "lng_export_about_done" = "Your data was successfully exported.";
"lng_export_done" = "Show my data"; "lng_export_done" = "Show my data";
"lng_export_finished" = "Data export completed."; "lng_export_finished" = "Data export completed.";
"lng_export_total_files" = "Total files: {count}."; "lng_export_total_amount" = "Total files: {amount}.";
"lng_export_total_size" = "Total size: {size}."; "lng_export_total_size" = "Total size: {size}.";
"lng_export_folder" = "Choose export folder"; "lng_export_folder" = "Choose export folder";
"lng_export_invalid" = "Sorry, you have started a new data export, so this data export is now cancelled."; "lng_export_invalid" = "Sorry, you have started a new data export, so this data export is now cancelled.";

View File

@ -171,7 +171,14 @@ void BoxController::Row::refreshStatus() {
} }
return lng_call_box_status_date(lt_date, langDayOfMonthFull(_date), lt_time, time); return lng_call_box_status_date(lt_date, langDayOfMonthFull(_date), lt_time, time);
}; };
setCustomStatus((_items.size() > 1) ? lng_call_box_status_group(lt_count, QString::number(_items.size()), lt_status, text()) : text()); setCustomStatus((_items.size() > 1)
? tr::lng_call_box_status_group(
tr::now,
lt_amount,
QString::number(_items.size()),
lt_status,
text())
: text());
} }
BoxController::Row::Type BoxController::Row::ComputeType( BoxController::Row::Type BoxController::Row::ComputeType(

View File

@ -107,9 +107,9 @@ bool Generator::writeHeader() {
writeHeaderForwardDeclarations(); writeHeaderForwardDeclarations();
writeHeaderTagTypes(); writeHeaderTagTypes();
writeHeaderKeyType(); writeHeaderKeyType();
writeHeaderTaggedMethods();
writeHeaderInterface(); writeHeaderInterface();
writeHeaderReactiveInterface(); writeHeaderReactiveInterface();
writeHeaderTaggedMethods();
return header_->finalize(); return header_->finalize();
} }
@ -161,34 +161,20 @@ void Generator::writeHeaderTaggedMethods() {
for (auto &entry : langpack_.entries) { for (auto &entry : langpack_.entries) {
auto isPlural = !entry.keyBase.isEmpty(); auto isPlural = !entry.keyBase.isEmpty();
auto &key = entry.key; auto &key = entry.key;
auto genericParams = QStringList();
auto params = QStringList(); auto params = QStringList();
auto applyTags = QStringList(); auto args = QStringList();
auto plural = QString();
for (auto &tagData : entry.tags) { for (auto &tagData : entry.tags) {
auto &tag = tagData.tag; auto &tag = tagData.tag;
auto isPluralTag = isPlural && (tag == kPluralTags[0]); 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 ? " type" : "") + ", " + (isPluralTag ? "float64 " : "const QString &") + tag + "__val");
params.push_back("lngtag_" + tag + ", " + (isPluralTag ? "float64 " : "const QString &") + tag + "__val"); args.push_back((isPluralTag ? "type" : ("lt_" + tag)) + ", " + tag + "__val");
if (isPluralTag) {
plural = "\tauto plural = Lang::Plural(" + key + ", " + tag + "__val, type);\n";
applyTags.push_back("\tresult = Lang::ReplaceTag<ResultString>::Call(std::move(result), lt_" + tag + ", Lang::StartReplacements<ResultString>::Call(std::move(plural.replacement)));\n");
} else {
applyTags.push_back("\tresult = Lang::ReplaceTag<ResultString>::Call(std::move(result), lt_" + tag + ", " + tag + "__val);\n");
}
} }
if (!entry.tags.empty() && (!isPlural || key == ComputePluralKey(entry.keyBase, 0))) { if (!entry.tags.empty() && (!isPlural || key == ComputePluralKey(entry.keyBase, 0))) {
auto initialString = isPlural ? ("lang(LangKey(" + key + " + plural.keyShift))") : ("lang(" + getFullKey(entry) + ")"); auto initialString = isPlural ? ("lang(LangKey(" + key + " + plural.keyShift))") : ("lang(" + getFullKey(entry) + ")");
header_->stream() << "\ header_->stream() << "\
template <typename ResultString>\n\ inline QString " << (isPlural ? entry.keyBase : key) << "(" << params.join(QString(", ")) << ") {\n\
ResultString " << (isPlural ? entry.keyBase : key) << "__generic(" << genericParams.join(QString(", ")) << ") {\n\ return tr::" << (isPlural ? entry.keyBase : key) << "(tr::now, " << args.join(QString(", ")) << ");\n\
" << plural << "\ }\n";
auto result = Lang::StartReplacements<ResultString>::Call(" << initialString << ");\n\
" << applyTags.join(QString()) << "\
return result;\n\
}\n\
inline constexpr auto " << (isPlural ? entry.keyBase : key) << " = &" << (isPlural ? entry.keyBase : key) << "__generic<QString>;\n\
\n";
} }
} }
} }
@ -196,7 +182,6 @@ inline constexpr auto " << (isPlural ? entry.keyBase : key) << " = &" << (isPlur
void Generator::writeHeaderInterface() { void Generator::writeHeaderInterface() {
header_->pushNamespace("Lang").stream() << "\ header_->pushNamespace("Lang").stream() << "\
\n\ \n\
const char *GetKeyName(LangKey key);\n\
ushort GetTagIndex(QLatin1String tag);\n\ ushort GetTagIndex(QLatin1String tag);\n\
LangKey GetKeyIndex(QLatin1String key);\n\ LangKey GetKeyIndex(QLatin1String key);\n\
bool IsTagReplaced(LangKey key, ushort tag);\n\ bool IsTagReplaced(LangKey key, ushort tag);\n\
@ -209,13 +194,17 @@ QString GetOriginalValue(LangKey key);\n\
void Generator::writeHeaderTagValueLookup() { void Generator::writeHeaderTagValueLookup() {
header_->pushNamespace("details").stream() << "\ header_->pushNamespace("details").stream() << "\
\n\ \n\
template <typename Tag> struct TagValue;\n\ template <typename Tag>\n\
struct TagData;\n\
\n\
template <typename Tag>\n\
inline constexpr ushort TagValue() {\n\
return TagData<Tag>::value;\n\
}\n\
\n"; \n";
for (auto &tag : langpack_.tags) { for (auto &tag : langpack_.tags) {
if (tag.tag != kPluralTags[0]) { header_->stream() << "template <> struct TagData<lngtag_" << tag.tag << "> : std::integral_constant<ushort, ushort(lt_" << tag.tag << ")> {};\n";
header_->stream() << "template <> struct TagValue<lngtag_" << tag.tag << "> : std::integral_constant<lngtag_" << tag.tag <<", lt_" << tag.tag << "> {};\n";
}
} }
header_->popNamespace(); header_->popNamespace();
@ -329,14 +318,6 @@ bool Generator::writeSource() {
source_ = std::make_unique<common::CppFile>(basePath_ + ".cpp", project_); source_ = std::make_unique<common::CppFile>(basePath_ + ".cpp", project_);
source_->include("lang/lang_keys.h").pushNamespace("Lang").pushNamespace().stream() << "\ source_->include("lang/lang_keys.h").pushNamespace("Lang").pushNamespace().stream() << "\
const char *KeyNames[kLangKeysCount] = {\n\
\n";
for (auto &entry : langpack_.entries) {
source_->stream() << "\"" << entry.key << "\",\n";
}
source_->stream() << "\
\n\
};\n\
\n\ \n\
QChar DefaultData[] = {"; QChar DefaultData[] = {";
auto count = 0; auto count = 0;
@ -381,10 +362,6 @@ int Offsets[] = {";
source_->stream() << " };\n"; source_->stream() << " };\n";
source_->popNamespace().stream() << "\ source_->popNamespace().stream() << "\
\n\ \n\
const char *GetKeyName(LangKey key) {\n\
return (key < 0 || key >= kLangKeysCount) ? \"\" : KeyNames[key];\n\
}\n\
\n\
ushort GetTagIndex(QLatin1String tag) {\n\ ushort GetTagIndex(QLatin1String tag) {\n\
auto size = tag.size();\n\ auto size = tag.size();\n\
auto data = tag.data();\n"; auto data = tag.data();\n";

View File

@ -322,10 +322,16 @@ void ParsedFile::addEntity(QString key, const QString &value) {
} }
} else { } else {
result_.entries.push_back(entry); result_.entries.push_back(entry);
for (auto &pluralEntry : tagsData.entries) { for (auto &tag : entry.tags) {
const auto plural = std::find(std::begin(kPluralTags), std::end(kPluralTags), tag.tag);
if (plural != std::end(kPluralTags)) {
logErrorBadString() << "plural tag '" << tag.tag.toStdString() << "' used in non-plural key '" << key.toStdString() << "'";
}
}
for (auto &tagEntry : tagsData.entries) {
auto taggedEntry = LangPack::Entry(); auto taggedEntry = LangPack::Entry();
taggedEntry.key = key + "__" + pluralEntry.key; taggedEntry.key = key + "__" + tagEntry.key;
taggedEntry.value = pluralEntry.value; taggedEntry.value = tagEntry.value;
result_.entries.push_back(taggedEntry); result_.entries.push_back(taggedEntry);
} }
} }

View File

@ -115,7 +115,7 @@ Content ContentFromState(const ProcessingState &state) {
default: Unexpected("Step in ContentFromState."); default: Unexpected("Step in ContentFromState.");
} }
while (result.rows.size() < 3) { while (result.rows.size() < 3) {
result.rows.push_back(Content::Row()); result.rows.emplace_back();
} }
return result; return result;
} }
@ -124,17 +124,23 @@ Content ContentFromState(const FinishedState &state) {
auto result = Content(); auto result = Content();
result.rows.push_back({ result.rows.push_back({
Content::kDoneId, Content::kDoneId,
lang(lng_export_finished), tr::lng_export_finished(tr::now),
QString(), QString(),
1. }); 1. });
result.rows.push_back({ result.rows.push_back({
Content::kDoneId, Content::kDoneId,
lng_export_total_files(lt_count, QString::number(state.filesCount)), tr::lng_export_total_amount(
tr::now,
lt_amount,
QString::number(state.filesCount)),
QString(), QString(),
1. }); 1. });
result.rows.push_back({ result.rows.push_back({
Content::kDoneId, Content::kDoneId,
lng_export_total_size(lt_size, formatSizeText(state.bytesCount)), tr::lng_export_total_size(
tr::now,
lt_size,
formatSizeText(state.bytesCount)),
QString(), QString(),
1. }); 1. });
return result; return result;

View File

@ -484,20 +484,23 @@ bool TopBar::computeCanDelete() const {
} }
Ui::StringWithNumbers TopBar::generateSelectedText() const { Ui::StringWithNumbers TopBar::generateSelectedText() const {
using Data = Ui::StringWithNumbers;
using Type = Storage::SharedMediaType; using Type = Storage::SharedMediaType;
const auto phrase = [&] { const auto phrase = [&] {
switch (_selectedItems.type) { switch (_selectedItems.type) {
case Type::Photo: return lng_media_selected_photo__generic<Data>; case Type::Photo: return tr::lng_media_selected_photo;
case Type::Video: return lng_media_selected_video__generic<Data>; case Type::Video: return tr::lng_media_selected_video;
case Type::File: return lng_media_selected_file__generic<Data>; case Type::File: return tr::lng_media_selected_file;
case Type::MusicFile: return lng_media_selected_song__generic<Data>; case Type::MusicFile: return tr::lng_media_selected_song;
case Type::Link: return lng_media_selected_link__generic<Data>; case Type::Link: return tr::lng_media_selected_link;
case Type::RoundVoiceFile: return lng_media_selected_audio__generic<Data>; case Type::RoundVoiceFile: return tr::lng_media_selected_audio;
} }
Unexpected("Type in TopBar::generateSelectedText()"); Unexpected("Type in TopBar::generateSelectedText()");
}(); }();
return phrase(lt_count, _selectedItems.list.size()); return phrase(
tr::now,
lt_count,
_selectedItems.list.size(),
Ui::StringWithNumbers::FromString);
} }
bool TopBar::selectionMode() const { bool TopBar::selectionMode() const {

View File

@ -17,11 +17,12 @@ namespace details {
inline constexpr auto kPluralCount = 6; inline constexpr auto kPluralCount = 6;
template <typename Tag>
inline constexpr ushort TagValue();
QString Current(LangKey key); QString Current(LangKey key);
rpl::producer<QString> Viewer(LangKey key); rpl::producer<QString> Viewer(LangKey key);
template <typename Tag> struct TagValue;
template <int Index, typename Type, typename Tuple> template <int Index, typename Type, typename Tuple>
Type ReplaceUnwrapTuple(Type accumulated, const Tuple &tuple) { Type ReplaceUnwrapTuple(Type accumulated, const Tuple &tuple) {
return accumulated; return accumulated;
@ -63,7 +64,7 @@ struct ReplaceUnwrap<Tag, Tags...> {
return ReplaceUnwrap<Tags...>::template Call( return ReplaceUnwrap<Tags...>::template Call(
ReplaceTag<Type>::Call( ReplaceTag<Type>::Call(
std::move(accumulated), std::move(accumulated),
TagValue<Tag>::value, TagValue<Tag>(),
value), value),
values...); values...);
} }
@ -80,7 +81,7 @@ struct Producer {
Viewer(base), Viewer(base),
std::move(values)... std::move(values)...
) | rpl::map([p = std::move(p)](auto tuple) { ) | rpl::map([p = std::move(p)](auto tuple) {
return ReplaceUnwrapTuple<1>(p(std::get<0>(tuple)), tuple, TagValue<Tags>::value...); return ReplaceUnwrapTuple<1>(p(std::get<0>(tuple)), tuple, TagValue<Tags>()...);
}); });
} }
@ -149,11 +150,11 @@ struct Producer<lngtag_count, Tags...> {
return ReplaceUnwrapTuple<7>( return ReplaceUnwrapTuple<7>(
ReplaceTag<T>::Call( ReplaceTag<T>::Call(
p(select()), p(select()),
type, TagValue<lngtag_count>(),
StartReplacements<T>::Call( StartReplacements<T>::Call(
std::move(plural.replacement))), std::move(plural.replacement))),
tuple, tuple,
TagValue<Tags>::value...); TagValue<Tags>()...);
}); });
} }
@ -161,7 +162,7 @@ struct Producer<lngtag_count, Tags...> {
typename P, typename P,
typename T = decltype(std::declval<P>()(QString())), typename T = decltype(std::declval<P>()(QString())),
typename ...Values> typename ...Values>
static T Current( static T Current(
LangKey base, LangKey base,
P p, P p,
lngtag_count type, lngtag_count type,
@ -171,7 +172,7 @@ struct Producer<lngtag_count, Tags...> {
return ReplaceUnwrap<Tags...>::template Call( return ReplaceUnwrap<Tags...>::template Call(
ReplaceTag<T>::Call( ReplaceTag<T>::Call(
p(Lang::details::Current(LangKey(base + plural.keyShift))), p(Lang::details::Current(LangKey(base + plural.keyShift))),
type, TagValue<lngtag_count>(),
StartReplacements<T>::Call( StartReplacements<T>::Call(
std::move(plural.replacement))), std::move(plural.replacement))),
values...); values...);

View File

@ -3763,9 +3763,23 @@ void OverlayWidget::updateHeader() {
auto count = _fullCount ? *_fullCount : -1; auto count = _fullCount ? *_fullCount : -1;
if (index >= 0 && index < count && count > 1) { if (index >= 0 && index < count && count > 1) {
if (_doc) { if (_doc) {
_headerText = lng_mediaview_file_n_of_count(lt_file, _doc->filename().isEmpty() ? lang(lng_mediaview_doc_image) : _doc->filename(), lt_n, QString::number(index + 1), lt_count, QString::number(count)); _headerText = tr::lng_mediaview_file_n_of_amount(
tr::now,
lt_file,
(_doc->filename().isEmpty()
? tr::lng_mediaview_doc_image(tr::now)
: _doc->filename()),
lt_n,
QString::number(index + 1),
lt_amount,
QString::number(count));
} else { } else {
_headerText = lng_mediaview_n_of_count(lt_n, QString::number(index + 1), lt_count, QString::number(count)); _headerText = tr::lng_mediaview_n_of_amount(
tr::now,
lt_n,
QString::number(index + 1),
lt_amount,
QString::number(count));
} }
} else { } else {
if (_doc) { if (_doc) {

View File

@ -64,6 +64,10 @@ private:
}; };
struct StringWithNumbers { struct StringWithNumbers {
static StringWithNumbers FromString(const QString &text) {
return { text };
}
QString text; QString text;
int offset = -1; int offset = -1;
int length = 0; int length = 0;