2020-09-30 11:32:02 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
#include "ui/text/format_values.h"
|
|
|
|
|
|
|
|
#include "lang/lang_keys.h"
|
2021-08-29 16:32:20 +00:00
|
|
|
#include "countries/countries_instance.h"
|
2020-09-30 11:32:02 +00:00
|
|
|
|
2021-07-26 06:32:16 +00:00
|
|
|
#include <QRegularExpression>
|
2020-09-30 11:32:02 +00:00
|
|
|
#include <QtCore/QLocale>
|
2021-03-30 14:49:41 +00:00
|
|
|
#include <locale>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-09-30 11:32:02 +00:00
|
|
|
namespace Ui {
|
2020-11-17 03:38:39 +00:00
|
|
|
namespace {
|
2020-09-30 11:32:02 +00:00
|
|
|
|
2021-03-30 15:24:14 +00:00
|
|
|
[[nodiscard]] QString FormatTextWithReadyAndTotal(
|
2020-11-17 03:38:39 +00:00
|
|
|
tr::phrase<lngtag_ready, lngtag_total, lngtag_mb> phrase,
|
|
|
|
qint64 ready,
|
|
|
|
qint64 total) {
|
2020-09-30 11:32:02 +00:00
|
|
|
QString readyStr, totalStr, mb;
|
|
|
|
if (total >= 1024 * 1024) { // more than 1 mb
|
2020-11-17 03:38:39 +00:00
|
|
|
const qint64 readyTenthMb = (ready * 10 / (1024 * 1024));
|
|
|
|
const qint64 totalTenthMb = (total * 10 / (1024 * 1024));
|
|
|
|
readyStr = QString::number(readyTenthMb / 10)
|
|
|
|
+ '.'
|
|
|
|
+ QString::number(readyTenthMb % 10);
|
|
|
|
totalStr = QString::number(totalTenthMb / 10)
|
|
|
|
+ '.'
|
|
|
|
+ QString::number(totalTenthMb % 10);
|
2020-09-30 11:32:02 +00:00
|
|
|
mb = u"MB"_q;
|
|
|
|
} else if (total >= 1024) {
|
|
|
|
qint64 readyKb = (ready / 1024), totalKb = (total / 1024);
|
|
|
|
readyStr = QString::number(readyKb);
|
|
|
|
totalStr = QString::number(totalKb);
|
|
|
|
mb = u"KB"_q;
|
|
|
|
} else {
|
|
|
|
readyStr = QString::number(ready);
|
|
|
|
totalStr = QString::number(total);
|
|
|
|
mb = u"B"_q;
|
|
|
|
}
|
2020-11-17 03:38:39 +00:00
|
|
|
return phrase(tr::now, lt_ready, readyStr, lt_total, totalStr, lt_mb, mb);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
QString FormatSizeText(qint64 size) {
|
|
|
|
if (size >= 1024 * 1024) { // more than 1 mb
|
|
|
|
const qint64 sizeTenthMb = (size * 10 / (1024 * 1024));
|
|
|
|
return QString::number(sizeTenthMb / 10)
|
|
|
|
+ '.'
|
|
|
|
+ QString::number(sizeTenthMb % 10) + u" MB"_q;
|
|
|
|
}
|
|
|
|
if (size >= 1024) {
|
|
|
|
const qint64 sizeTenthKb = (size * 10 / 1024);
|
|
|
|
return QString::number(sizeTenthKb / 10)
|
|
|
|
+ '.'
|
|
|
|
+ QString::number(sizeTenthKb % 10) + u" KB"_q;
|
|
|
|
}
|
|
|
|
return QString::number(size) + u" B"_q;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatDownloadText(qint64 ready, qint64 total) {
|
|
|
|
return FormatTextWithReadyAndTotal(
|
|
|
|
tr::lng_save_downloaded,
|
|
|
|
ready,
|
|
|
|
total);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatProgressText(qint64 ready, qint64 total) {
|
|
|
|
return FormatTextWithReadyAndTotal(
|
|
|
|
tr::lng_media_save_progress,
|
|
|
|
ready,
|
|
|
|
total);
|
|
|
|
}
|
|
|
|
|
2021-10-07 05:37:42 +00:00
|
|
|
QString FormatDateTime(
|
|
|
|
QDateTime date,
|
|
|
|
QString dateFormat,
|
|
|
|
QString timeFormat) {
|
2020-11-17 03:38:39 +00:00
|
|
|
const auto now = QDateTime::currentDateTime();
|
|
|
|
if (date.date() == now.date()) {
|
|
|
|
return tr::lng_mediaview_today(
|
|
|
|
tr::now,
|
|
|
|
lt_time,
|
2022-10-17 03:51:59 +00:00
|
|
|
QLocale().toString(date.time(), timeFormat));
|
2020-11-17 03:38:39 +00:00
|
|
|
} else if (date.date().addDays(1) == now.date()) {
|
|
|
|
return tr::lng_mediaview_yesterday(
|
|
|
|
tr::now,
|
|
|
|
lt_time,
|
2022-10-17 03:51:59 +00:00
|
|
|
QLocale().toString(date.time(), timeFormat));
|
2020-11-17 03:38:39 +00:00
|
|
|
} else {
|
|
|
|
return tr::lng_mediaview_date_time(
|
|
|
|
tr::now,
|
|
|
|
lt_date,
|
2022-10-17 03:51:59 +00:00
|
|
|
QLocale().toString(date.date(), dateFormat),
|
2020-11-17 03:38:39 +00:00
|
|
|
lt_time,
|
2022-10-17 03:51:59 +00:00
|
|
|
QLocale().toString(date.time(), timeFormat));
|
2020-11-17 03:38:39 +00:00
|
|
|
}
|
2020-09-30 11:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatDurationText(qint64 duration) {
|
|
|
|
qint64 hours = (duration / 3600), minutes = (duration % 3600) / 60, seconds = duration % 60;
|
|
|
|
return (hours ? QString::number(hours) + ':' : QString()) + (minutes >= 10 ? QString() : QString('0')) + QString::number(minutes) + ':' + (seconds >= 10 ? QString() : QString('0')) + QString::number(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatDurationWords(qint64 duration) {
|
|
|
|
if (duration > 59) {
|
|
|
|
auto minutes = (duration / 60);
|
|
|
|
auto minutesCount = tr::lng_duration_minsec_minutes(tr::now, lt_count, minutes);
|
|
|
|
auto seconds = (duration % 60);
|
|
|
|
auto secondsCount = tr::lng_duration_minsec_seconds(tr::now, lt_count, seconds);
|
|
|
|
return tr::lng_duration_minutes_seconds(tr::now, lt_minutes_count, minutesCount, lt_seconds_count, secondsCount);
|
|
|
|
}
|
2022-03-31 16:45:11 +00:00
|
|
|
return tr::lng_seconds(tr::now, lt_count, duration);
|
2020-09-30 11:32:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 04:19:26 +00:00
|
|
|
QString FormatDurationWordsSlowmode(qint64 duration) {
|
|
|
|
if (duration > 59) {
|
|
|
|
auto minutes = (duration / 60);
|
|
|
|
auto minutesCount = tr::lng_duration_minsec_minutes(tr::now, lt_count, minutes);
|
|
|
|
auto seconds = (duration % 60);
|
|
|
|
auto secondsCount = tr::lng_duration_minsec_seconds(tr::now, lt_count, seconds);
|
|
|
|
return tr::lng_duration_minutes_seconds(tr::now, lt_minutes_count, minutesCount, lt_seconds_count, secondsCount);
|
|
|
|
}
|
|
|
|
return tr::lng_slowmode_seconds(tr::now, lt_count, duration);
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:32:02 +00:00
|
|
|
QString FormatDurationAndSizeText(qint64 duration, qint64 size) {
|
|
|
|
return tr::lng_duration_and_size(tr::now, lt_duration, FormatDurationText(duration), lt_size, FormatSizeText(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatGifAndSizeText(qint64 size) {
|
|
|
|
return tr::lng_duration_and_size(tr::now, lt_duration, u"GIF"_q, lt_size, FormatSizeText(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatPlayedText(qint64 played, qint64 duration) {
|
|
|
|
return tr::lng_duration_played(tr::now, lt_played, FormatDurationText(played), lt_duration, FormatDurationText(duration));
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:46:48 +00:00
|
|
|
QString FillAmountAndCurrency(
|
|
|
|
int64 amount,
|
|
|
|
const QString ¤cy,
|
|
|
|
bool forceStripDotZero) {
|
2021-04-19 06:52:14 +00:00
|
|
|
// std::abs doesn't work on that one :/
|
|
|
|
Expects(amount != std::numeric_limits<int64>::min());
|
|
|
|
|
2021-04-01 09:27:39 +00:00
|
|
|
const auto rule = LookupCurrencyRule(currency);
|
|
|
|
|
|
|
|
const auto prefix = (amount < 0)
|
|
|
|
? QString::fromUtf8("\xe2\x88\x92")
|
|
|
|
: QString();
|
|
|
|
const auto value = std::abs(amount) / std::pow(10., rule.exponent);
|
|
|
|
const auto name = (*rule.international)
|
|
|
|
? QString::fromUtf8(rule.international)
|
|
|
|
: currency;
|
|
|
|
auto result = prefix;
|
|
|
|
if (rule.left) {
|
|
|
|
result.append(name);
|
|
|
|
if (rule.space) result.append(' ');
|
|
|
|
}
|
2021-04-02 11:46:48 +00:00
|
|
|
const auto precision = ((!rule.stripDotZero && !forceStripDotZero)
|
|
|
|
|| std::floor(value) != value)
|
2021-04-01 09:27:39 +00:00
|
|
|
? rule.exponent
|
|
|
|
: 0;
|
|
|
|
result.append(FormatWithSeparators(
|
|
|
|
value,
|
|
|
|
precision,
|
|
|
|
rule.decimal,
|
|
|
|
rule.thousands));
|
|
|
|
if (!rule.left) {
|
|
|
|
if (rule.space) result.append(' ');
|
|
|
|
result.append(name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyRule LookupCurrencyRule(const QString ¤cy) {
|
|
|
|
static const auto kRules = std::vector<std::pair<QString, CurrencyRule>>{
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"AED"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"AFN"_q, {} },
|
|
|
|
{ u"ALL"_q, { "", '.', ',', false } },
|
|
|
|
{ u"AMD"_q, { "", ',', '.', false, true } },
|
|
|
|
{ u"ARS"_q, { "", '.', ',', true, true } },
|
|
|
|
{ u"AUD"_q, { "AU$" } },
|
|
|
|
{ u"AZN"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"BAM"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"BDT"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"BGN"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"BND"_q, { "", '.', ',', } },
|
|
|
|
{ u"BOB"_q, { "", '.', ',', true, true } },
|
|
|
|
{ u"BRL"_q, { "R$", '.', ',', true, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"BHD"_q, { "", ',', '.', true, true, 3 } },
|
|
|
|
{ u"BYR"_q, { "", ' ', ',', false, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"CAD"_q, { "CA$" } },
|
|
|
|
{ u"CHF"_q, { "", '\'', '.', false, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"CLP"_q, { "", '.', ',', true, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"CNY"_q, { "\x43\x4E\xC2\xA5" } },
|
|
|
|
{ u"COP"_q, { "", '.', ',', true, true } },
|
|
|
|
{ u"CRC"_q, { "", '.', ',', } },
|
|
|
|
{ u"CZK"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"DKK"_q, { "", '\0', ',', false, true } },
|
|
|
|
{ u"DOP"_q, {} },
|
|
|
|
{ u"DZD"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"EGP"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"EUR"_q, { "\xE2\x82\xAC", ' ', ',', false, true } },
|
|
|
|
{ u"GBP"_q, { "\xC2\xA3" } },
|
|
|
|
{ u"GEL"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"GTQ"_q, {} },
|
|
|
|
{ u"HKD"_q, { "HK$" } },
|
|
|
|
{ u"HNL"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"HRK"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"HUF"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"IDR"_q, { "", '.', ',', } },
|
|
|
|
{ u"ILS"_q, { "\xE2\x82\xAA", ',', '.', true, true } },
|
|
|
|
{ u"INR"_q, { "\xE2\x82\xB9" } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"ISK"_q, { "", '.', ',', false, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"JMD"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"JPY"_q, { "\xC2\xA5", ',', '.', true, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"KES"_q, {} },
|
|
|
|
{ u"KGS"_q, { "", ' ', '-', false, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"KRW"_q, { "\xE2\x82\xA9", ',', '.', true, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"KZT"_q, { "", ' ', '-', } },
|
|
|
|
{ u"LBP"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"LKR"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"MAD"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"MDL"_q, { "", ',', '.', false, true } },
|
|
|
|
{ u"MNT"_q, { "", ' ', ',', } },
|
|
|
|
{ u"MUR"_q, {} },
|
|
|
|
{ u"MVR"_q, { "", ',', '.', false, true } },
|
|
|
|
{ u"MXN"_q, { "MX$" } },
|
|
|
|
{ u"MYR"_q, {} },
|
|
|
|
{ u"MZN"_q, {} },
|
|
|
|
{ u"NGN"_q, {} },
|
|
|
|
{ u"NIO"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"NOK"_q, { "", ' ', ',', true, true } },
|
|
|
|
{ u"NPR"_q, {} },
|
|
|
|
{ u"NZD"_q, { "NZ$" } },
|
|
|
|
{ u"PAB"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"PEN"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"PHP"_q, {} },
|
|
|
|
{ u"PKR"_q, {} },
|
|
|
|
{ u"PLN"_q, { "", ' ', ',', false, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"PYG"_q, { "", '.', ',', true, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"QAR"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"RON"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"RSD"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"RUB"_q, { "", ' ', ',', false, true } },
|
|
|
|
{ u"SAR"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"SEK"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"SGD"_q, {} },
|
|
|
|
{ u"THB"_q, { "\xE0\xB8\xBF" } },
|
|
|
|
{ u"TJS"_q, { "", ' ', ';', false, true } },
|
|
|
|
{ u"TRY"_q, { "", '.', ',', false, true } },
|
|
|
|
{ u"TTD"_q, {} },
|
|
|
|
{ u"TWD"_q, { "NT$" } },
|
|
|
|
{ u"TZS"_q, {} },
|
|
|
|
{ u"UAH"_q, { "", ' ', ',', false } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"UGX"_q, { "", ',', '.', true, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"USD"_q, { "$" } },
|
|
|
|
{ u"UYU"_q, { "", '.', ',', true, true } },
|
|
|
|
{ u"UZS"_q, { "", ' ', ',', false, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"VND"_q, { "\xE2\x82\xAB", '.', ',', false, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"YER"_q, { "", ',', '.', true, true } },
|
|
|
|
{ u"ZAR"_q, { "", ',', '.', true, true } },
|
2021-04-01 09:27:39 +00:00
|
|
|
{ u"IRR"_q, { "", ',', '/', false, true, 2, true } },
|
|
|
|
{ u"IQD"_q, { "", ',', '.', true, true, 3 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
{ u"VEF"_q, { "", '.', ',', true, true } },
|
|
|
|
{ u"SYP"_q, { "", ',', '.', true, true } },
|
|
|
|
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"VUV"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"WST"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"XAF"_q, { "FCFA", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"XCD"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"XOF"_q, { "CFA", ' ', ',', false, false, 0 } },
|
|
|
|
//{ u"XPF"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"ZMW"_q, {} },
|
|
|
|
//{ u"ANG"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"RWF"_q, { "", ' ', ',', true, true, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"PGK"_q, {} },
|
|
|
|
//{ u"TOP"_q, {} },
|
|
|
|
//{ u"SBD"_q, {} },
|
|
|
|
//{ u"SCR"_q, {} },
|
|
|
|
//{ u"SHP"_q, {} },
|
|
|
|
//{ u"SLL"_q, {} },
|
|
|
|
//{ u"SOS"_q, {} },
|
|
|
|
//{ u"SRD"_q, {} },
|
|
|
|
//{ u"STD"_q, {} },
|
|
|
|
//{ u"SVC"_q, {} },
|
|
|
|
//{ u"SZL"_q, {} },
|
|
|
|
//{ u"AOA"_q, {} },
|
|
|
|
//{ u"AWG"_q, {} },
|
|
|
|
//{ u"BBD"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"BIF"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"BMD"_q, {} },
|
|
|
|
//{ u"BSD"_q, {} },
|
|
|
|
//{ u"BWP"_q, {} },
|
|
|
|
//{ u"BZD"_q, {} },
|
|
|
|
//{ u"CDF"_q, { "", ',', '.', false } },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"CVE"_q, { "", ',', '.', true, false, 0 } },
|
|
|
|
//{ u"DJF"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"ETB"_q, {} },
|
|
|
|
//{ u"FJD"_q, {} },
|
|
|
|
//{ u"FKP"_q, {} },
|
|
|
|
//{ u"GIP"_q, {} },
|
|
|
|
//{ u"GMD"_q, { "", ',', '.', false } },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"GNF"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"GYD"_q, {} },
|
|
|
|
//{ u"HTG"_q, {} },
|
|
|
|
//{ u"KHR"_q, { "", ',', '.', false } },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"KMF"_q, { "", ',', '.', false, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"KYD"_q, {} },
|
|
|
|
//{ u"LAK"_q, { "", ',', '.', false } },
|
|
|
|
//{ u"LRD"_q, {} },
|
|
|
|
//{ u"LSL"_q, { "", ',', '.', false } },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"MGA"_q, { "", ',', '.', true, false, 0 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
//{ u"MKD"_q, { "", '.', ',', false, true } },
|
|
|
|
//{ u"MOP"_q, {} },
|
|
|
|
//{ u"MWK"_q, {} },
|
|
|
|
//{ u"NAD"_q, {} },
|
2021-04-01 09:27:39 +00:00
|
|
|
//{ u"CLF"_q, { "", ',', '.', true, false, 4 } },
|
|
|
|
//{ u"JOD"_q, { "", ',', '.', true, false, 3 } },
|
|
|
|
//{ u"KWD"_q, { "", ',', '.', true, false, 3 } },
|
|
|
|
//{ u"LYD"_q, { "", ',', '.', true, false, 3 } },
|
|
|
|
//{ u"OMR"_q, { "", ',', '.', true, false, 3 } },
|
|
|
|
//{ u"TND"_q, { "", ',', '.', true, false, 3 } },
|
|
|
|
//{ u"UYI"_q, { "", ',', '.', true, false, 0 } },
|
|
|
|
//{ u"MRO"_q, { "", ',', '.', true, false, 1 } },
|
2021-03-30 14:49:41 +00:00
|
|
|
};
|
|
|
|
static const auto kRulesMap = [] {
|
|
|
|
// flat_multi_map_pair_type lacks some required constructors :(
|
2021-04-01 09:27:39 +00:00
|
|
|
auto &&list = kRules | ranges::views::transform([](auto &&pair) {
|
|
|
|
return base::flat_multi_map_pair_type<QString, CurrencyRule>(
|
2021-03-30 14:49:41 +00:00
|
|
|
pair.first,
|
|
|
|
pair.second);
|
|
|
|
});
|
2021-04-01 09:27:39 +00:00
|
|
|
return base::flat_map<QString, CurrencyRule>(begin(list), end(list));
|
2021-03-30 14:49:41 +00:00
|
|
|
}();
|
2021-04-01 09:27:39 +00:00
|
|
|
const auto i = kRulesMap.find(currency);
|
|
|
|
return (i != end(kRulesMap)) ? i->second : CurrencyRule{};
|
|
|
|
}
|
2021-03-30 17:18:39 +00:00
|
|
|
|
2021-04-01 09:27:39 +00:00
|
|
|
[[nodiscard]] QString FormatWithSeparators(
|
|
|
|
double amount,
|
|
|
|
int precision,
|
|
|
|
char decimal,
|
|
|
|
char thousands) {
|
|
|
|
Expects(decimal != 0);
|
2021-03-30 17:18:39 +00:00
|
|
|
|
2021-04-01 09:27:39 +00:00
|
|
|
// Thanks https://stackoverflow.com/a/5058949
|
|
|
|
struct FormattingHelper : std::numpunct<char> {
|
|
|
|
FormattingHelper(char decimal, char thousands)
|
|
|
|
: decimal(decimal)
|
|
|
|
, thousands(thousands) {
|
|
|
|
}
|
|
|
|
|
|
|
|
char do_decimal_point() const override { return decimal; }
|
|
|
|
char do_thousands_sep() const override { return thousands; }
|
|
|
|
|
|
|
|
char decimal = '.';
|
|
|
|
char thousands = ',';
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stream = std::ostringstream();
|
|
|
|
stream.imbue(std::locale(
|
|
|
|
stream.getloc(),
|
|
|
|
new FormattingHelper(decimal, thousands ? thousands : '?')));
|
|
|
|
stream.precision(precision);
|
|
|
|
stream << std::fixed << amount;
|
|
|
|
auto result = QString::fromStdString(stream.str());
|
|
|
|
if (!thousands) {
|
|
|
|
result.replace('?', QString());
|
2021-03-30 14:49:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
2020-09-30 11:32:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 19:15:11 +00:00
|
|
|
QString FormatImageSizeText(const QSize &size) {
|
|
|
|
return QString::number(size.width())
|
|
|
|
+ QChar(215)
|
|
|
|
+ QString::number(size.height());
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:32:20 +00:00
|
|
|
QString FormatPhone(const QString &phone) {
|
2021-07-26 06:32:16 +00:00
|
|
|
if (phone.isEmpty()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
if (phone.at(0) == '0') {
|
|
|
|
return phone;
|
|
|
|
}
|
2021-08-29 16:32:20 +00:00
|
|
|
return Countries::Instance().format({ .phone = phone }).formatted;
|
2021-07-26 06:32:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:59:35 +00:00
|
|
|
QString FormatTTL(float64 ttl) {
|
|
|
|
return (ttl <= 3600 * 23)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_hours(tr::now, lt_count, int(ttl / 3600))
|
2022-03-31 12:59:35 +00:00
|
|
|
: (ttl <= (86400) * 6)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_days(tr::now, lt_count, int(ttl / (86400)))
|
2022-03-31 12:59:35 +00:00
|
|
|
: (ttl <= (86400 * 7) * 3)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_weeks(tr::now, lt_count, int(ttl / (86400 * 7)))
|
|
|
|
: (ttl <= (86400 * 31) * 11)
|
|
|
|
? tr::lng_months({}, lt_count, int(ttl / (86400 * 31)))
|
|
|
|
: tr::lng_years({}, lt_count, std::round(ttl / (86400 * 365)));
|
2022-03-31 12:59:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 20:12:38 +00:00
|
|
|
QString FormatTTLTiny(float64 ttl) {
|
|
|
|
return (ttl <= 3600 * 9)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_hours_tiny(tr::now, lt_count, int(ttl / 3600))
|
2022-03-28 20:12:38 +00:00
|
|
|
: (ttl <= (86400) * 6)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_days_tiny(tr::now, lt_count, int(ttl / (86400)))
|
2022-03-28 20:12:38 +00:00
|
|
|
: (ttl <= (86400 * 7) * 3)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_weeks_tiny(tr::now, lt_count, int(ttl / (86400 * 7)))
|
|
|
|
: (ttl <= (86400 * 31) * 11)
|
|
|
|
? tr::lng_months_tiny({}, lt_count, int(ttl / (86400 * 31)))
|
2022-05-04 14:18:14 +00:00
|
|
|
: (ttl <= 86400 * 366)
|
|
|
|
? tr::lng_years_tiny({}, lt_count, std::round(ttl / (86400 * 365)))
|
|
|
|
: QString();
|
2022-03-28 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:07:17 +00:00
|
|
|
QString FormatMuteFor(float64 sec) {
|
|
|
|
return (sec <= 60)
|
|
|
|
? tr::lng_seconds(tr::now, lt_count, sec)
|
|
|
|
: (sec <= 60 * 59)
|
2022-04-19 13:50:04 +00:00
|
|
|
? tr::lng_minutes(tr::now, lt_count, int(sec / 60))
|
2022-04-01 16:07:17 +00:00
|
|
|
: FormatTTL(sec);
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:57:36 +00:00
|
|
|
QString FormatMuteForTiny(float64 sec) {
|
|
|
|
return (sec <= 60)
|
|
|
|
? QString()
|
|
|
|
: (sec <= 60 * 59)
|
2022-05-02 17:52:40 +00:00
|
|
|
? tr::lng_minutes_tiny(tr::now, lt_count, std::round(sec / 60))
|
2022-03-30 19:57:36 +00:00
|
|
|
: (sec <= 3600 * 23)
|
2022-05-02 17:52:40 +00:00
|
|
|
? tr::lng_hours_tiny(tr::now, lt_count, std::round(sec / 3600))
|
2022-03-30 19:57:36 +00:00
|
|
|
: (sec <= 86400 * 6)
|
2022-05-02 17:52:40 +00:00
|
|
|
? tr::lng_days_tiny(tr::now, lt_count, std::round(sec / 86400))
|
2022-03-30 19:57:36 +00:00
|
|
|
: (sec <= (86400 * 7) * 3)
|
2022-05-02 17:52:40 +00:00
|
|
|
? tr::lng_weeks_tiny(tr::now, lt_count, std::round(sec / (86400 * 7)))
|
|
|
|
: (sec <= (86400 * 31) * 11)
|
|
|
|
? tr::lng_months_tiny({}, lt_count, std::round(sec / (86400 * 31)))
|
2022-05-04 14:18:14 +00:00
|
|
|
: (sec <= 86400 * 366)
|
|
|
|
? tr::lng_years_tiny({}, lt_count, std::round(sec / (86400 * 365)))
|
|
|
|
: QString();
|
2022-03-30 19:57:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 16:24:34 +00:00
|
|
|
QString FormatResetCloudPasswordIn(float64 sec) {
|
|
|
|
return (sec >= 3600) ? FormatTTL(sec) : FormatDurationText(sec);
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:32:02 +00:00
|
|
|
} // namespace Ui
|