Bitcoin Core  26.99.0
P2P Digital Currency
translation.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 
10 #include <functional>
11 #include <string>
12 
18 struct bilingual_str {
19  std::string original;
20  std::string translated;
21 
23  {
24  original += rhs.original;
25  translated += rhs.translated;
26  return *this;
27  }
28 
29  bool empty() const
30  {
31  return original.empty();
32  }
33 
34  void clear()
35  {
36  original.clear();
37  translated.clear();
38  }
39 };
40 
42 {
43  lhs += rhs;
44  return lhs;
45 }
46 
48 inline bilingual_str Untranslated(std::string original) { return {original, original}; }
49 
50 // Provide an overload of tinyformat::format which can take bilingual_str arguments.
51 namespace tinyformat {
52 template <typename... Args>
53 bilingual_str format(const bilingual_str& fmt, const Args&... args)
54 {
55  const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
56  if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
57  return translated ? arg.translated : arg.original;
58  } else {
59  return arg;
60  }
61  }};
62  return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...),
63  tfm::format(fmt.translated, translate_arg(args, true)...)};
64 }
65 } // namespace tinyformat
66 
68 const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
69 
74 inline bilingual_str _(const char* psz)
75 {
76  return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
77 }
78 
79 #endif // BITCOIN_UTIL_TRANSLATION_H
ArgsManager & args
Definition: bitcoind.cpp:268
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060
bilingual_str format(const bilingual_str &fmt, const Args &... args)
Definition: translation.h:53
Bilingual messages:
Definition: translation.h:18
void clear()
Definition: translation.h:34
bool empty() const
Definition: translation.h:29
std::string translated
Definition: translation.h:20
std::string original
Definition: translation.h:19
bilingual_str & operator+=(const bilingual_str &rhs)
Definition: translation.h:22
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate a message to the native language of the user.
Definition: bitcoin-cli.cpp:53
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:74
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
Definition: translation.h:41
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:48