Line data Source code
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 719813 : struct bilingual_str { 17 : std::string original; 18 : std::string translated; 19 : 20 1993 : bilingual_str& operator+=(const bilingual_str& rhs) 21 : { 22 1993 : original += rhs.original; 23 1993 : translated += rhs.translated; 24 1993 : return *this; 25 : } 26 : 27 53672 : bool empty() const 28 : { 29 53672 : return original.empty(); 30 : } 31 : }; 32 : 33 43 : inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs) 34 : { 35 43 : lhs += rhs; 36 43 : return lhs; 37 : } 38 : 39 : /** Mark a bilingual_str as untranslated */ 40 1324 : inline bilingual_str Untranslated(std::string original) { return {original, original}; } 41 : 42 : namespace tinyformat { 43 : template <typename... Args> 44 1498 : bilingual_str format(const bilingual_str& fmt, const Args&... args) 45 : { 46 1498 : return bilingual_str{format(fmt.original, args...), format(fmt.translated, args...)}; 47 0 : } 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 10365 : inline bilingual_str _(const char* psz) 58 : { 59 10365 : return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz}; 60 0 : } 61 : 62 : #endif // BITCOIN_UTIL_TRANSLATION_H