Branch data Line data Source code
# 1 : : // Copyright (c) 2019-2020 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 : 915 : { # 22 : 915 : original += rhs.original; # 23 : 915 : translated += rhs.translated; # 24 : 915 : return *this; # 25 : 915 : } # 26 : : # 27 : : bool empty() const # 28 : 76813 : { # 29 : 76813 : return original.empty(); # 30 : 76813 : } # 31 : : }; # 32 : : # 33 : : inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs) # 34 : 63 : { # 35 : 63 : lhs += rhs; # 36 : 63 : return lhs; # 37 : 63 : } # 38 : : # 39 : : /** Mark a bilingual_str as untranslated */ # 40 : 2241 : inline bilingual_str Untranslated(std::string original) { return {original, original}; } # 41 : : # 42 : : namespace tinyformat { # 43 : : template <typename... Args> # 44 : : bilingual_str format(const bilingual_str& fmt, const Args&... args) # 45 : 780 : { # 46 : 780 : return bilingual_str{format(fmt.original, args...), format(fmt.translated, args...)}; # 47 : 780 : } # 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 : : */ # 57 : : inline bilingual_str _(const char* psz) # 58 : 10540 : { # 59 [ - + ]: 10540 : return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz}; # 60 : 10540 : } # 61 : : # 62 : : #endif // BITCOIN_UTIL_TRANSLATION_H