Bitcoin ABC  0.29.2
P2P Digital Currency
translation.h
Go to the documentation of this file.
1 // Copyright (c) 2019 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 
17 struct bilingual_str {
18  std::string original;
19  std::string translated;
20 
22  original += rhs.original;
23  translated += rhs.translated;
24  return *this;
25  }
26 
27  bool empty() const { return original.empty(); }
28 };
29 
31  lhs += rhs;
32  return lhs;
33 }
34 
36 inline bilingual_str Untranslated(std::string original) {
37  return {original, original};
38 }
39 
40 // Provide an overload of tinyformat::format which can take bilingual_str
41 // arguments.
42 namespace tinyformat {
43 // clang-format off
44 template <typename... Args>
45 bilingual_str format(const bilingual_str &fmt, const Args &...args) {
46  const auto translate_arg{
47  [](const auto &arg, [[maybe_unused]] bool translated) -> const auto & {
48  if constexpr (std::is_same_v<decltype(arg), const bilingual_str &>) {
49  return translated ? arg.translated : arg.original;
50  } else {
51  return arg;
52  }
53  }};
54  return bilingual_str{
55  tfm::format(fmt.original, translate_arg(args, false)...),
56  tfm::format(fmt.translated, translate_arg(args, true)...)};
57 }
58 // clang-format on
59 } // namespace tinyformat
60 
62 const extern std::function<std::string(const char *)> G_TRANSLATION_FUN;
63 
68 inline bilingual_str _(const char *psz) {
69  return bilingual_str{psz,
70  G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
71 }
72 
73 #endif // BITCOIN_UTIL_TRANSLATION_H
Definition: fs.h:241
bilingual_str format(const bilingual_str &fmt, const Args &...args)
Definition: translation.h:45
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:1112
Bilingual messages:
Definition: translation.h:17
bool empty() const
Definition: translation.h:27
std::string translated
Definition: translation.h:19
std::string original
Definition: translation.h:18
bilingual_str & operator+=(const bilingual_str &rhs)
Definition: translation.h:21
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate a message to the native language of the user.
Definition: bitcoin-cli.cpp:43
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
Definition: translation.h:30
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36