tdesktop/Telegram/SourceFiles/lang/lang_keys.cpp

236 lines
5.9 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_file_parser.h"
2020-09-30 09:11:44 +00:00
#include "ui/integration.h"
2019-06-19 15:09:03 +00:00
namespace {
template <typename WithYear, typename WithoutYear>
inline QString langDateMaybeWithYear(
QDate date,
WithYear withYear,
WithoutYear withoutYear) {
const auto month = date.month();
2019-06-19 15:09:03 +00:00
if (month <= 0 || month > 12) {
2020-09-30 09:11:44 +00:00
return u"MONTH_ERR"_q;
2019-06-19 15:09:03 +00:00
};
const auto year = date.year();
const auto current = QDate::currentDate();
const auto currentYear = current.year();
const auto currentMonth = current.month();
2019-06-19 15:09:03 +00:00
if (year != currentYear) {
const auto yearIsMuchGreater = [](int year, int otherYear) {
2019-06-19 15:09:03 +00:00
return (year > otherYear + 1);
};
const auto monthIsMuchGreater = [](
int year,
int month,
int otherYear,
int otherMonth) {
2019-06-19 15:09:03 +00:00
return (year == otherYear + 1) && (month + 12 > otherMonth + 3);
};
if (false
|| yearIsMuchGreater(year, currentYear)
|| yearIsMuchGreater(currentYear, year)
|| monthIsMuchGreater(year, month, currentYear, currentMonth)
|| monthIsMuchGreater(currentYear, currentMonth, year, month)) {
return withYear(month, year);
}
2015-02-16 15:30:32 +00:00
}
2019-06-19 15:09:03 +00:00
return withoutYear(month, year);
}
tr::phrase<> Month(int index) {
switch (index) {
case 1: return tr::lng_month1;
case 2: return tr::lng_month2;
case 3: return tr::lng_month3;
case 4: return tr::lng_month4;
case 5: return tr::lng_month5;
case 6: return tr::lng_month6;
case 7: return tr::lng_month7;
case 8: return tr::lng_month8;
case 9: return tr::lng_month9;
case 10: return tr::lng_month10;
case 11: return tr::lng_month11;
case 12: return tr::lng_month12;
}
Unexpected("Index in MonthSmall.");
}
tr::phrase<> MonthSmall(int index) {
switch (index) {
case 1: return tr::lng_month1_small;
case 2: return tr::lng_month2_small;
case 3: return tr::lng_month3_small;
case 4: return tr::lng_month4_small;
case 5: return tr::lng_month5_small;
case 6: return tr::lng_month6_small;
case 7: return tr::lng_month7_small;
case 8: return tr::lng_month8_small;
case 9: return tr::lng_month9_small;
case 10: return tr::lng_month10_small;
case 11: return tr::lng_month11_small;
case 12: return tr::lng_month12_small;
}
Unexpected("Index in MonthSmall.");
}
tr::phrase<> MonthDay(int index) {
switch (index) {
case 1: return tr::lng_month_day1;
case 2: return tr::lng_month_day2;
case 3: return tr::lng_month_day3;
case 4: return tr::lng_month_day4;
case 5: return tr::lng_month_day5;
case 6: return tr::lng_month_day6;
case 7: return tr::lng_month_day7;
case 8: return tr::lng_month_day8;
case 9: return tr::lng_month_day9;
case 10: return tr::lng_month_day10;
case 11: return tr::lng_month_day11;
case 12: return tr::lng_month_day12;
}
Unexpected("Index in MonthDay.");
}
tr::phrase<> Weekday(int index) {
switch (index) {
case 1: return tr::lng_weekday1;
case 2: return tr::lng_weekday2;
case 3: return tr::lng_weekday3;
case 4: return tr::lng_weekday4;
case 5: return tr::lng_weekday5;
case 6: return tr::lng_weekday6;
case 7: return tr::lng_weekday7;
}
Unexpected("Index in Weekday.");
}
} // namespace
bool langFirstNameGoesSecond() {
const auto kFirstName = QChar(0x0001);
const auto kLastName = QChar(0x0002);
const auto fullname = tr::lng_full_name(
tr::now,
lt_first_name,
QString(1, kFirstName),
lt_last_name,
QString(1, kLastName));
return fullname.indexOf(kLastName) < fullname.indexOf(kFirstName);
}
QString langDayOfMonth(const QDate &date) {
auto day = date.day();
return langDateMaybeWithYear(date, [&](int month, int year) {
return tr::lng_month_day_year(
tr::now,
lt_month,
MonthSmall(month)(tr::now),
lt_day,
QString::number(day),
lt_year,
QString::number(year));
2019-06-19 15:09:03 +00:00
}, [day](int month, int year) {
return tr::lng_month_day(
tr::now,
lt_month,
MonthSmall(month)(tr::now),
lt_day,
QString::number(day));
2019-06-19 15:09:03 +00:00
});
}
QString langDayOfMonthFull(const QDate &date) {
auto day = date.day();
return langDateMaybeWithYear(date, [day](int month, int year) {
return tr::lng_month_day_year(
tr::now,
lt_month,
MonthDay(month)(tr::now),
lt_day,
QString::number(day),
lt_year,
QString::number(year));
2019-06-19 15:09:03 +00:00
}, [day](int month, int year) {
return tr::lng_month_day(
tr::now,
lt_month,
MonthDay(month)(tr::now),
lt_day,
QString::number(day));
2019-06-19 15:09:03 +00:00
});
}
QString langMonthOfYear(int month, int year) {
return (month > 0 && month <= 12)
? tr::lng_month_year(
tr::now,
lt_month,
MonthSmall(month)(tr::now),
lt_year,
QString::number(year))
2020-09-30 09:11:44 +00:00
: u"MONTH_ERR"_q;
2019-06-19 15:09:03 +00:00
}
QString langMonth(const QDate &date) {
return langDateMaybeWithYear(date, [](int month, int year) {
return langMonthOfYear(month, year);
}, [](int month, int year) {
return MonthSmall(month)(tr::now);
});
}
QString langMonthOfYearFull(int month, int year) {
return (month > 0 && month <= 12)
? tr::lng_month_year(
tr::now,
lt_month,
Month(month)(tr::now),
lt_year,
QString::number(year))
2020-09-30 09:11:44 +00:00
: u"MONTH_ERR"_q;
2019-06-19 15:09:03 +00:00
}
QString langMonthFull(const QDate &date) {
return langDateMaybeWithYear(date, [](int month, int year) {
return langMonthOfYearFull(month, year);
}, [](int month, int year) {
return Month(month)(tr::now);
});
}
QString langDayOfWeek(int index) {
2020-09-30 09:11:44 +00:00
return (index > 0 && index <= 7) ? Weekday(index)(tr::now) : u"DAY_ERR"_q;
}
QString langDateTime(const QDateTime &date) {
return tr::lng_mediaview_date_time(
tr::now,
lt_date,
langDayOfMonth(date.date()),
lt_time,
QLocale().toString(
date.time(),
Ui::Integration::Instance().timeFormat()));
2020-09-30 09:11:44 +00:00
}
QString langDateTimeFull(const QDateTime &date) {
return tr::lng_mediaview_date_time(
tr::now,
lt_date,
langDayOfMonthFull(date.date()),
lt_time,
QLocale().toString(
date.time(),
Ui::Integration::Instance().timeFormat()));
}