LCOV - code coverage report
Current view: top level - src/util - translation.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 19 19
Test Date: 2024-08-28 04:44:32 Functions: 22.7 % 44 10
Branches: 12.0 % 242 29

             Branch data     Line data    Source code
       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                 :             : 
      13                 :             : /**
      14                 :             :  * Bilingual messages:
      15                 :             :  *   - in GUI: user's native language + untranslated (i.e. English)
      16                 :             :  *   - in log and stderr: untranslated only
      17                 :             :  */
      18   [ +  +  #  #  :       22030 : struct bilingual_str {
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  -  -  -  
          -  +  +  -  -  
          -  -  -  -  #  
           #  #  # ][ +  
          -  +  -  +  -  
           +  - ][ +  -  
             +  -  +  - ]
           [ +  -  +  -  
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
             -  -  -  -  
           - ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
                   +  - ]
           [ #  #  #  # ]
      19                 :             :     std::string original;
      20                 :             :     std::string translated;
      21                 :             : 
      22                 :           1 :     bilingual_str& operator+=(const bilingual_str& rhs)
      23                 :             :     {
      24                 :           1 :         original += rhs.original;
      25                 :           1 :         translated += rhs.translated;
      26                 :           1 :         return *this;
      27                 :             :     }
      28                 :             : 
      29                 :        1521 :     bool empty() const
      30                 :             :     {
      31   [ +  -  #  #  :        1521 :         return original.empty();
                   #  # ]
           [ +  +  +  - ]
           [ -  -  -  +  
                   -  + ]
      32                 :             :     }
      33                 :             : 
      34                 :             :     void clear()
      35                 :             :     {
      36                 :             :         original.clear();
      37                 :             :         translated.clear();
      38                 :             :     }
      39                 :             : };
      40                 :             : 
      41                 :           1 : inline bilingual_str operator+(bilingual_str lhs, const bilingual_str& rhs)
      42                 :             : {
      43   [ +  -  #  #  :           1 :     lhs += rhs;
          #  #  #  #  #  
                      # ]
           [ #  #  #  # ]
           [ -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      44                 :           1 :     return lhs;
      45                 :             : }
      46                 :             : 
      47                 :             : /** Mark a bilingual_str as untranslated */
      48         [ +  - ]:        7851 : inline bilingual_str Untranslated(std::string original) { return {original, original}; }
      49                 :             : 
      50                 :             : // Provide an overload of tinyformat::format which can take bilingual_str arguments.
      51                 :             : namespace tinyformat {
      52                 :             : template <typename... Args>
      53                 :          55 : bilingual_str format(const bilingual_str& fmt, const Args&... args)
      54                 :             : {
      55                 :          26 :     const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
      56                 :             :         if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
      57                 :          26 :             return translated ? arg.translated : arg.original;
      58                 :             :         } else {
      59                 :             :             return arg;
      60                 :             :         }
      61                 :             :     }};
      62                 :          55 :     return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...),
      63                 :          55 :                          tfm::format(fmt.translated, translate_arg(args, true)...)};
      64         [ +  - ]:          55 : }
      65                 :             : } // namespace tinyformat
      66                 :             : 
      67                 :             : /** Translate a message to the native language of the user. */
      68                 :             : const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
      69                 :             : 
      70                 :             : struct ConstevalStringLiteral {
      71                 :             :     const char* const lit;
      72                 :             :     consteval ConstevalStringLiteral(const char* str) : lit{str} {}
      73                 :             :     consteval ConstevalStringLiteral(std::nullptr_t) = delete;
      74                 :             : };
      75                 :             : 
      76                 :             : /**
      77                 :             :  * Translation function.
      78                 :             :  * If no translation function is set, simply return the input.
      79                 :             :  */
      80                 :         867 : inline bilingual_str _(ConstevalStringLiteral str)
      81                 :             : {
      82   [ -  +  -  -  :         867 :     return bilingual_str{str.lit, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(str.lit) : str.lit};
                   +  - ]
      83                 :             : }
      84                 :             : 
      85                 :             : #endif // BITCOIN_UTIL_TRANSLATION_H
        

Generated by: LCOV version 2.0-1