Bitcoin Core 28.99.0
P2P Digital Currency
translation.h
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_TRANSLATION_H
6#define BITCOIN_UTIL_TRANSLATION_H
7
8#include <tinyformat.h>
9#include <util/string.h>
10
11#include <cassert>
12#include <functional>
13#include <string>
14
16using TranslateFn = std::function<std::string(const char*)>;
18
25 std::string original;
26 std::string translated;
27
29 {
30 original += rhs.original;
32 return *this;
33 }
34
35 bool empty() const
36 {
37 return original.empty();
38 }
39
40 void clear()
41 {
42 original.clear();
43 translated.clear();
44 }
45};
46
48{
49 lhs += rhs;
50 return lhs;
51}
52
53namespace util {
56 const char* const original;
58
59 consteval TranslatedLiteral(const char* str, const TranslateFn* fn = &G_TRANSLATION_FUN) : original{str}, translate_fn{fn} { assert(original); }
60 operator std::string() const { return translate_fn && *translate_fn ? (*translate_fn)(original) : original; }
61 operator bilingual_str() const { return {original, std::string{*this}}; }
62};
63
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; }
70
71template <unsigned num_params>
76};
77} // namespace util
78
79consteval auto _(util::TranslatedLiteral str) { return str; }
80
82inline bilingual_str Untranslated(std::string original) { return {original, original}; }
83
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{
89 const auto original_arg{[](const auto& arg) -> const auto& {
90 if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
91 return arg.original;
92 } else if constexpr (std::is_same_v<decltype(arg), const util::TranslatedLiteral&>) {
93 return arg.original;
94 } else {
95 return arg;
96 }
97 }};
98 const auto translated_arg{[](const auto& arg) -> const auto& {
99 if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
100 return arg.translated;
101 } else {
102 return arg;
103 }
104 }};
105 return bilingual_str{tfm::format(fmt.original, original_arg(args)...),
106 tfm::format(RuntimeFormat{std::string{fmt.lit}}, translated_arg(args)...)};
107}
108} // namespace tinyformat
109
110#endif // BITCOIN_UTIL_TRANSLATION_H
ArgsManager & args
Definition: bitcoind.cpp:277
#define T(expected, seed, data)
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
bilingual_str format(util::BilingualFmt< sizeof...(Args)> fmt, const Args &... args)
Definition: translation.h:87
std::ostream & operator<<(std::ostream &os, const TranslatedLiteral &lit)
Definition: translation.h:65
T operator+(const T &lhs, const TranslatedLiteral &rhs)
Definition: translation.h:67
Bilingual messages:
Definition: translation.h:24
void clear()
Definition: translation.h:40
bool empty() const
Definition: translation.h:35
std::string translated
Definition: translation.h:26
std::string original
Definition: translation.h:25
bilingual_str & operator+=(const bilingual_str &rhs)
Definition: translation.h:28
consteval BilingualFmt(TranslatedLiteral l)
Definition: translation.h:75
const ConstevalFormatString< num_params > original
Definition: translation.h:73
TranslatedLiteral lit
Definition: translation.h:74
A wrapper for a compile-time partially validated format string.
Definition: string.h:92
Compile-time literal string that can be translated with an optional translation function.
Definition: translation.h:55
const TranslateFn * translate_fn
Definition: translation.h:57
consteval TranslatedLiteral(const char *str, const TranslateFn *fn=&G_TRANSLATION_FUN)
Definition: translation.h:59
const char *const original
Definition: translation.h:56
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
Definition: translation.h:16
const TranslateFn G_TRANSLATION_FUN
Translate string to current locale using Qt.
Definition: bitcoin-cli.cpp:53
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
Definition: translation.h:47
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
assert(!tx.IsCoinBase())