mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
Extend bilingual_str support for tinyformat
Previous bilingual_str tinyformat::format accepted bilingual format strings, but not bilingual arguments. Extend it to accept both. This is useful when embedding one translated string inside another translated string, for example: `strprintf(_("Error: %s"), message)` which would fail previously if `message` was a bilingual_str.
This commit is contained in:
parent
c361df90b9
commit
3db2874bd7
4 changed files with 37 additions and 1 deletions
|
@ -147,6 +147,7 @@ BITCOIN_TESTS =\
|
||||||
test/timedata_tests.cpp \
|
test/timedata_tests.cpp \
|
||||||
test/torcontrol_tests.cpp \
|
test/torcontrol_tests.cpp \
|
||||||
test/transaction_tests.cpp \
|
test/transaction_tests.cpp \
|
||||||
|
test/translation_tests.cpp \
|
||||||
test/txindex_tests.cpp \
|
test/txindex_tests.cpp \
|
||||||
test/txpackage_tests.cpp \
|
test/txpackage_tests.cpp \
|
||||||
test/txreconciliation_tests.cpp \
|
test/txreconciliation_tests.cpp \
|
||||||
|
|
21
src/test/translation_tests.cpp
Normal file
21
src/test/translation_tests.cpp
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright (c) 2023 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <tinyformat.h>
|
||||||
|
#include <util/translation.h>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(translation_tests)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(translation_namedparams)
|
||||||
|
{
|
||||||
|
bilingual_str arg{"original", "translated"};
|
||||||
|
bilingual_str format{"original [%s]", "translated [%s]"};
|
||||||
|
bilingual_str result{strprintf(format, arg)};
|
||||||
|
BOOST_CHECK_EQUAL(result.original, "original [original]");
|
||||||
|
BOOST_CHECK_EQUAL(result.translated, "translated [translated]");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
|
@ -47,11 +47,24 @@ inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
|
||||||
/** Mark a bilingual_str as untranslated */
|
/** Mark a bilingual_str as untranslated */
|
||||||
inline bilingual_str Untranslated(std::string original) { return {original, original}; }
|
inline bilingual_str Untranslated(std::string original) { return {original, original}; }
|
||||||
|
|
||||||
|
// Provide an overload of tinyformat::format which can take bilingual_str arguments.
|
||||||
namespace tinyformat {
|
namespace tinyformat {
|
||||||
|
inline std::string TranslateArg(const bilingual_str& arg, bool translated)
|
||||||
|
{
|
||||||
|
return translated ? arg.translated : arg.original;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline T const& TranslateArg(const T& arg, bool translated)
|
||||||
|
{
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
bilingual_str format(const bilingual_str& fmt, const Args&... args)
|
bilingual_str format(const bilingual_str& fmt, const Args&... args)
|
||||||
{
|
{
|
||||||
return bilingual_str{format(fmt.original, args...), format(fmt.translated, args...)};
|
return bilingual_str{format(fmt.original, TranslateArg(args, false)...),
|
||||||
|
format(fmt.translated, TranslateArg(args, true)...)};
|
||||||
}
|
}
|
||||||
} // namespace tinyformat
|
} // namespace tinyformat
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ FALSE_POSITIVES = [
|
||||||
("src/index/base.cpp", "FatalError(const char* fmt, const Args&... args)"),
|
("src/index/base.cpp", "FatalError(const char* fmt, const Args&... args)"),
|
||||||
("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args)"),
|
("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args)"),
|
||||||
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
|
("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
|
||||||
|
("src/test/translation_tests.cpp", "strprintf(format, arg)"),
|
||||||
("src/validationinterface.cpp", "LogPrint(BCLog::VALIDATION, fmt \"\\n\", __VA_ARGS__)"),
|
("src/validationinterface.cpp", "LogPrint(BCLog::VALIDATION, fmt \"\\n\", __VA_ARGS__)"),
|
||||||
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
|
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
|
||||||
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
|
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
|
||||||
|
|
Loading…
Add table
Reference in a new issue