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
10#include <functional>
11#include <string>
12
14const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
15
22 std::string original;
23 std::string translated;
24
26 {
27 original += rhs.original;
29 return *this;
30 }
31
32 bool empty() const
33 {
34 return original.empty();
35 }
36
37 void clear()
38 {
39 original.clear();
40 translated.clear();
41 }
42};
43
45{
46 lhs += rhs;
47 return lhs;
48}
49
51inline bilingual_str Untranslated(std::string original) { return {original, original}; }
52
53// Provide an overload of tinyformat::format which can take bilingual_str arguments.
54namespace tinyformat {
55template <typename... Args>
56bilingual_str format(const bilingual_str& fmt, const Args&... args)
57{
58 const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
59 if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
60 return translated ? arg.translated : arg.original;
61 } else {
62 return arg;
63 }
64 }};
65 return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...),
66 tfm::format(fmt.translated, translate_arg(args, true)...)};
67}
68} // namespace tinyformat
69
71 const char* const lit;
72 consteval ConstevalStringLiteral(const char* str) : lit{str} {}
73 consteval ConstevalStringLiteral(std::nullptr_t) = delete;
74};
75
81{
82 return bilingual_str{str.lit, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(str.lit) : str.lit};
83}
84
85#endif // BITCOIN_UTIL_TRANSLATION_H
ArgsManager & args
Definition: bitcoind.cpp:277
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:1072
bilingual_str format(const bilingual_str &fmt, const Args &... args)
Definition: translation.h:56
const char *const lit
Definition: translation.h:71
consteval ConstevalStringLiteral(const char *str)
Definition: translation.h:72
consteval ConstevalStringLiteral(std::nullptr_t)=delete
Bilingual messages:
Definition: translation.h:21
void clear()
Definition: translation.h:37
bool empty() const
Definition: translation.h:32
std::string translated
Definition: translation.h:23
std::string original
Definition: translation.h:22
bilingual_str & operator+=(const bilingual_str &rhs)
Definition: translation.h:25
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 _(ConstevalStringLiteral str)
Translation function.
Definition: translation.h:80
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
Definition: translation.h:44
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:51