Skip to content

Commit 86ea584

Browse files
MarcoFalkeryanofsky
andcommitted
refactor: Delay translation of Untranslated() or _() literals
This is required for a future commit that requires Untranslated() and _() to be consteval for format literals. Co-Authored-By: Ryan Ofsky <[email protected]>
1 parent 71d3826 commit 86ea584

File tree

8 files changed

+39
-33
lines changed

8 files changed

+39
-33
lines changed

src/banman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void BanMan::LoadBanlist()
3030
{
3131
LOCK(m_banned_mutex);
3232

33-
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);
33+
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translate());
3434

3535
const auto start{SteadyClock::now()};
3636
if (m_ban_db.Read(m_banned)) {

src/clientversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ std::string LicenseInfo()
9595
strprintf(_("The source code is available from %s."), URL_SOURCE_CODE).translated +
9696
"\n" +
9797
"\n" +
98-
_("This is experimental software.").translated + "\n" +
98+
_("This is experimental software.").translate() + "\n" +
9999
strprintf(_("Distributed under the MIT software license, see the accompanying file %s or %s"), "COPYING", "<https://opensource.org/licenses/MIT>").translated +
100100
"\n";
101101
}

src/init.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,19 +1256,19 @@ static ChainstateLoadResult InitAndLoadChainstate(
12561256
_("Error reading from database, shutting down."),
12571257
"", CClientUIInterface::MSG_ERROR);
12581258
};
1259-
uiInterface.InitMessage(_("Loading block index…").translated);
1259+
uiInterface.InitMessage(_("Loading block index…").translate());
12601260
const auto load_block_index_start_time{SteadyClock::now()};
12611261
auto catch_exceptions = [](auto&& f) {
12621262
try {
12631263
return f();
12641264
} catch (const std::exception& e) {
12651265
LogError("%s\n", e.what());
1266-
return std::make_tuple(node::ChainstateLoadStatus::FAILURE, _("Error opening block database"));
1266+
return std::make_tuple(node::ChainstateLoadStatus::FAILURE, bilingual_str(_("Error opening block database")));
12671267
}
12681268
};
12691269
auto [status, error] = catch_exceptions([&] { return LoadChainstate(chainman, cache_sizes, options); });
12701270
if (status == node::ChainstateLoadStatus::SUCCESS) {
1271-
uiInterface.InitMessage(_("Verifying blocks…").translated);
1271+
uiInterface.InitMessage(_("Verifying blocks…").translate());
12721272
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
12731273
LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
12741274
MIN_BLOCKS_TO_KEEP);
@@ -1431,7 +1431,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14311431

14321432
// Initialize addrman
14331433
assert(!node.addrman);
1434-
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
1434+
uiInterface.InitMessage(_("Loading P2P addresses…").translate());
14351435
auto addrman{LoadAddrman(*node.netgroupman, args)};
14361436
if (!addrman) return InitError(util::ErrorString(addrman));
14371437
node.addrman = std::move(*addrman);
@@ -1717,7 +1717,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17171717
if (chainman.m_blockman.m_blockfiles_indexed) {
17181718
LOCK(cs_main);
17191719
for (Chainstate* chainstate : chainman.GetAll()) {
1720-
uiInterface.InitMessage(_("Pruning blockstore…").translated);
1720+
uiInterface.InitMessage(_("Pruning blockstore…").translate());
17211721
chainstate->PruneAndFlush();
17221722
}
17231723
}
@@ -2014,7 +2014,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20142014
// ChainstateManager's active tip.
20152015
SetRPCWarmupFinished();
20162016

2017-
uiInterface.InitMessage(_("Done loading").translated);
2017+
uiInterface.InitMessage(_("Done loading").translate());
20182018

20192019
for (const auto& client : node.chain_clients) {
20202020
client->start(scheduler);

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3260,7 +3260,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
32603260
}
32613261

32623262
if (m_client_interface) {
3263-
m_client_interface->InitMessage(_("Starting network threads…").translated);
3263+
m_client_interface->InitMessage(_("Starting network threads…").translate());
32643264
}
32653265

32663266
fAddressesInitialized = true;

src/util/translation.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <tinyformat.h>
99

10+
#include <cassert>
1011
#include <functional>
1112
#include <string>
1213

@@ -47,6 +48,26 @@ inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
4748
return lhs;
4849
}
4950

51+
namespace util {
52+
/**
53+
* Translation function.
54+
* If no translation function is set, simply return the input.
55+
*/
56+
inline std::string translate(const char* lit)
57+
{
58+
return G_TRANSLATION_FUN ? G_TRANSLATION_FUN(lit) : lit;
59+
}
60+
61+
struct ConstEvalTranslated {
62+
const char* const lit;
63+
consteval ConstEvalTranslated(const char* str) : lit{str} { assert(lit); }
64+
std::string translate() const { return util::translate(lit); }
65+
operator bilingual_str() const { return {lit, translate()}; }
66+
};
67+
} // namespace util
68+
69+
consteval auto _(util::ConstEvalTranslated str) { return str; }
70+
5071
/** Mark a bilingual_str as untranslated */
5172
inline bilingual_str Untranslated(std::string original) { return {original, original}; }
5273

@@ -67,19 +88,4 @@ bilingual_str format(const bilingual_str& fmt, const Args&... args)
6788
}
6889
} // namespace tinyformat
6990

70-
struct ConstevalStringLiteral {
71-
const char* const lit;
72-
consteval ConstevalStringLiteral(const char* str) : lit{str} {}
73-
consteval ConstevalStringLiteral(std::nullptr_t) = delete;
74-
};
75-
76-
/**
77-
* Translation function.
78-
* If no translation function is set, simply return the input.
79-
*/
80-
inline bilingual_str _(ConstevalStringLiteral str)
81-
{
82-
return bilingual_str{str.lit, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(str.lit) : str.lit};
83-
}
84-
8591
#endif // BITCOIN_UTIL_TRANSLATION_H

src/wallet/load.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool VerifyWallets(WalletContext& context)
5252

5353
LogPrintf("Using wallet directory %s\n", fs::PathToString(GetWalletDir()));
5454

55-
chain.initMessage(_("Verifying wallet(s)…").translated);
55+
chain.initMessage(_("Verifying wallet(s)…").translate());
5656

5757
// For backwards compatibility if an unnamed top level wallet exists in the
5858
// wallets directory, include it in the default list of wallets to load.
@@ -135,7 +135,7 @@ bool LoadWallets(WalletContext& context)
135135
if (!database && status == DatabaseStatus::FAILED_NOT_FOUND) {
136136
continue;
137137
}
138-
chain.initMessage(_("Loading wallet…").translated);
138+
chain.initMessage(_("Loading wallet…").translate());
139139
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings) : nullptr;
140140
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
141141
if (!pwallet) {

src/wallet/rpc/backup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ RPCHelpMan importwallet()
534534

535535
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
536536
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
537-
pwallet->chain().showProgress(strprintf("%s %s", pwallet->GetDisplayName(), _("Importing…").translated), 0, false); // show progress dialog in GUI
537+
pwallet->chain().showProgress(strprintf("%s %s", pwallet->GetDisplayName(), _("Importing…").translate()), 0, false); // show progress dialog in GUI
538538
std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
539539
std::vector<std::pair<CScript, int64_t>> scripts;
540540
while (file.good()) {

src/wallet/wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ std::shared_ptr<CWallet> LoadWalletInternal(WalletContext& context, const std::s
280280
return nullptr;
281281
}
282282

283-
context.chain->initMessage(_("Loading wallet…").translated);
283+
context.chain->initMessage(_("Loading wallet…").translate());
284284
std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings);
285285
if (!wallet) {
286286
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
@@ -430,7 +430,7 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
430430
}
431431

432432
// Make the wallet
433-
context.chain->initMessage(_("Loading wallet…").translated);
433+
context.chain->initMessage(_("Loading wallet…").translate());
434434
std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), wallet_creation_flags, error, warnings);
435435
if (!wallet) {
436436
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
@@ -1891,7 +1891,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
18911891
fast_rescan_filter ? "fast variant using block filters" : "slow variant inspecting all blocks");
18921892

18931893
fAbortRescan = false;
1894-
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translated), 0); // show rescan progress in GUI as dialog or on splashscreen, if rescan required on startup (e.g. due to corruption)
1894+
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translate()), 0); // show rescan progress in GUI as dialog or on splashscreen, if rescan required on startup (e.g. due to corruption)
18951895
uint256 tip_hash = WITH_LOCK(cs_wallet, return GetLastBlockHash());
18961896
uint256 end_hash = tip_hash;
18971897
if (max_height) chain().findAncestorByHeight(tip_hash, *max_height, FoundBlock().hash(end_hash));
@@ -1906,7 +1906,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
19061906
m_scanning_progress = 0;
19071907
}
19081908
if (block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
1909-
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translated), std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
1909+
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translate()), std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
19101910
}
19111911

19121912
bool next_interval = reserver.now() >= current_time + INTERVAL_TIME;
@@ -2003,7 +2003,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
20032003
WalletLogPrintf("Scanning current mempool transactions.\n");
20042004
WITH_LOCK(cs_wallet, chain().requestMempoolTransactions(*this));
20052005
}
2006-
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translated), 100); // hide progress dialog in GUI
2006+
ShowProgress(strprintf("%s %s", GetDisplayName(), _("Rescanning…").translate()), 100); // hide progress dialog in GUI
20072007
if (block_height && fAbortRescan) {
20082008
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", block_height, progress_current);
20092009
result.status = ScanResult::USER_ABORT;
@@ -3336,7 +3336,7 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
33363336
}
33373337
}
33383338

3339-
chain.initMessage(_("Rescanning…").translated);
3339+
chain.initMessage(_("Rescanning…").translate());
33403340
walletInstance->WalletLogPrintf("Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height);
33413341

33423342
{

0 commit comments

Comments
 (0)