Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_TRANSLATION_H6#define BITCOIN_UTIL_TRANSLATION_H8#include <tinyformat.h>9#include <util/check.h>10#include <util/string.h>12#include <functional>13#include <string>15/** Translate a message to the native language of the user. */16using TranslateFn = std::function<std::string(const char*)>;17const extern TranslateFn G_TRANSLATION_FUN;19/**20* Bilingual messages:21* - in GUI: user's native language + untranslated (i.e. English)22* - in log and stderr: untranslated only23*/24struct bilingual_str {25std::string original;26std::string translated;28bilingual_str& operator+=(const bilingual_str& rhs)29{30original += rhs.original;31translated += rhs.translated;32return *this;33}35bool empty() const36{37return original.empty();38}40void clear()41{42original.clear();43translated.clear();44}45};47inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)48{49lhs += rhs;50return lhs;51}53namespace util {54//! Compile-time literal string that can be translated with an optional translation function.55struct TranslatedLiteral {56const char* const original;57const TranslateFn* translate_fn;59consteval TranslatedLiteral(const char* str, const TranslateFn* fn = &G_TRANSLATION_FUN) : original{str}, translate_fn{fn} { assert(original); }60operator std::string() const { return translate_fn && *translate_fn ? (*translate_fn)(original) : original; }61operator bilingual_str() const { return {original, std::string{*this}}; }62};64// TranslatedLiteral operators for formatting and adding to strings.65inline std::ostream& operator<<(std::ostream& os, const TranslatedLiteral& lit) { return os << std::string{lit}; }66template<typename T>67T operator+(const T& lhs, const TranslatedLiteral& rhs) { return lhs + static_cast<T>(rhs); }68template<typename T>69T operator+(const TranslatedLiteral& lhs, const T& rhs) { return static_cast<T>(lhs) + rhs; }71template <unsigned num_params>72struct BilingualFmt {73const ConstevalFormatString<num_params> original;74TranslatedLiteral lit;75consteval BilingualFmt(TranslatedLiteral l) : original{l.original}, lit{l} {}76};77} // namespace util79consteval auto _(util::TranslatedLiteral str) { return str; }81/** Mark a bilingual_str as untranslated */82inline bilingual_str Untranslated(std::string original) { return {original, original}; }84// Provide an overload of tinyformat::format for BilingualFmt format strings and bilingual_str or TranslatedLiteral args.85namespace tinyformat {86template <typename... Args>87bilingual_str format(util::BilingualFmt<sizeof...(Args)> fmt, const Args&... args)88{89const auto original_arg{[](const auto& arg) -> const auto& {90if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {91return arg.original;92} else if constexpr (std::is_same_v<decltype(arg), const util::TranslatedLiteral&>) {93return arg.original;94} else {95return arg;96}97}};98const auto translated_arg{[](const auto& arg) -> const auto& {99if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {100return arg.translated;101} else {102return arg;103}104}};105return bilingual_str{tfm::format(fmt.original, original_arg(args)...),106tfm::format(RuntimeFormat{std::string{fmt.lit}}, translated_arg(args)...)};107}108} // namespace tinyformat110#endif // BITCOIN_UTIL_TRANSLATION_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.