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 #include <functional>
10 
11 /**
12  * Bilingual messages:
13  *   - in GUI: user's native language + untranslated (i.e. English)
14  *   - in log and stderr: untranslated only
15  */
16 struct bilingual_str {
17     std::string original;
18     std::string translated;
19 
20     bilingual_str& operator+=(const bilingual_str& rhs)
21     {
22         original += rhs.original;
23         translated += rhs.translated;
24         return *this;
25     }
26 
emptybilingual_str27     bool empty() const
28     {
29         return original.empty();
30     }
31 };
32 
33 inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
34 {
35     lhs += rhs;
36     return lhs;
37 }
38 
39 /** Mark a bilingual_str as untranslated */
Untranslated(std::string original)40 inline bilingual_str Untranslated(std::string original) { return {original, original}; }
41 
42 namespace tinyformat {
43 template <typename... Args>
format(const bilingual_str & fmt,const Args &...args)44 bilingual_str format(const bilingual_str& fmt, const Args&... args)
45 {
46     return bilingual_str{format(fmt.original, args...), format(fmt.translated, args...)};
47 }
48 } // namespace tinyformat
49 
50 /** Translate a message to the native language of the user. */
51 const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
52 
53 /**
54  * Translation function.
55  * If no translation function is set, simply return the input.
56  */
_(const char * psz)57 inline bilingual_str _(const char* psz)
58 {
59     return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
60 }
61 
62 #endif // BITCOIN_UTIL_TRANSLATION_H
63