LCOV - code coverage report
Current view: top level - src/util - translation.h (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 15 17 88.2 %
Date: 2026-06-25 07:23:51 Functions: 18 53 34.0 %

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

Generated by: LCOV version 1.16