Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What's the Q_DECLARE_TR_FUNCTIONS equivalent for namespaces?

What's the Q_DECLARE_TR_FUNCTIONS equivalent for namespaces?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Justin Sayne
    wrote on last edited by
    #1

    Assuming I want to change this:

    class Utils
    {
      Q_DECLARE_TR_FUNCTIONS( Utils );
    
      static void foo1();
      static void foo2();
      static void foo3();
    };
    

    into what is really more reasonable:

    namespace Utils
    {
      // What's the equivalent here to declare a translation context for my utility namespace?
      // Q_DECLARE_TR_FUNCTIONS( Utils ); ???
    
      void foo1();
      void foo2();
      void foo3();
    }
    
    kshegunovK 1 Reply Last reply
    0
    • J Justin Sayne

      Assuming I want to change this:

      class Utils
      {
        Q_DECLARE_TR_FUNCTIONS( Utils );
      
        static void foo1();
        static void foo2();
        static void foo3();
      };
      

      into what is really more reasonable:

      namespace Utils
      {
        // What's the equivalent here to declare a translation context for my utility namespace?
        // Q_DECLARE_TR_FUNCTIONS( Utils ); ???
      
        void foo1();
        void foo2();
        void foo3();
      }
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Justin-Sayne
      Hello,
      Doesn't it work to simply use QObject::tr in this case? As far as I can tell Q_DECLARE_TR_FUNCTIONS only aliases the aforementioned function with a proper context ...

      PS:
      The Q_DECLARE_TR_FUNCTIONS macro is defined as:

      #define Q_DECLARE_TR_FUNCTIONS(context) \
      public: \
          static inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \
              { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
          QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context) \
      private:
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @Justin-Sayne
        Hello,
        Doesn't it work to simply use QObject::tr in this case? As far as I can tell Q_DECLARE_TR_FUNCTIONS only aliases the aforementioned function with a proper context ...

        PS:
        The Q_DECLARE_TR_FUNCTIONS macro is defined as:

        #define Q_DECLARE_TR_FUNCTIONS(context) \
        public: \
            static inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \
                { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
            QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context) \
        private:
        

        Kind regards.

        J Offline
        J Offline
        Justin Sayne
        wrote on last edited by
        #3

        @kshegunov
        Yes, but that's what I mean. By using contexts, this will also show up nicely ordered in QtLinguist later on, right? If I lose the ability to specify contexts for all namespaces, won't they all end up in global space later on and making translation a lot more cluttered than using a class with static utility methods and keeping the ability to wrap it inside a translation context?

        kshegunovK 1 Reply Last reply
        0
        • J Justin Sayne

          @kshegunov
          Yes, but that's what I mean. By using contexts, this will also show up nicely ordered in QtLinguist later on, right? If I lose the ability to specify contexts for all namespaces, won't they all end up in global space later on and making translation a lot more cluttered than using a class with static utility methods and keeping the ability to wrap it inside a translation context?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Justin-Sayne said:

          Yes, but that's what I mean. By using contexts, this will also show up nicely ordered in QtLinguist later on, right? If I lose the ability to specify contexts for all namespaces, won't they all end up in global space later on and making translation a lot more cluttered than using a class with static utility methods and keeping the ability to wrap it inside a translation context?

          I guess so, but you could alias the function in a similar fashion. For example you could have a tr() function in the namespace that specifies the context as the namespace name. Or you could even make a macro for it (it's basically the same only the access specifiers are stripped):

          #define Q_DECLARE_NAMESPACE_TR(context) \
              inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \
              { return QCoreApplication::translate(#context, sourceText, disambiguation, n); }
          

          Then use it in your namespace:

          namespace MyNamespace
          {
              Q_DECLARE_NAMESPACE_TR(MyNamespace)
          
              int someFunction();
          }
          

          Kind regards.

          Read and abide by the Qt Code of Conduct

          J S 2 Replies Last reply
          2
          • kshegunovK kshegunov

            @Justin-Sayne said:

            Yes, but that's what I mean. By using contexts, this will also show up nicely ordered in QtLinguist later on, right? If I lose the ability to specify contexts for all namespaces, won't they all end up in global space later on and making translation a lot more cluttered than using a class with static utility methods and keeping the ability to wrap it inside a translation context?

            I guess so, but you could alias the function in a similar fashion. For example you could have a tr() function in the namespace that specifies the context as the namespace name. Or you could even make a macro for it (it's basically the same only the access specifiers are stripped):

            #define Q_DECLARE_NAMESPACE_TR(context) \
                inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \
                { return QCoreApplication::translate(#context, sourceText, disambiguation, n); }
            

            Then use it in your namespace:

            namespace MyNamespace
            {
                Q_DECLARE_NAMESPACE_TR(MyNamespace)
            
                int someFunction();
            }
            

            Kind regards.

            J Offline
            J Offline
            Justin Sayne
            wrote on last edited by
            #5

            @kshegunov
            Yes, that's actually a great solution. Should have thought of it myself. :D

            1 Reply Last reply
            0
            • kshegunovK kshegunov

              @Justin-Sayne said:

              Yes, but that's what I mean. By using contexts, this will also show up nicely ordered in QtLinguist later on, right? If I lose the ability to specify contexts for all namespaces, won't they all end up in global space later on and making translation a lot more cluttered than using a class with static utility methods and keeping the ability to wrap it inside a translation context?

              I guess so, but you could alias the function in a similar fashion. For example you could have a tr() function in the namespace that specifies the context as the namespace name. Or you could even make a macro for it (it's basically the same only the access specifiers are stripped):

              #define Q_DECLARE_NAMESPACE_TR(context) \
                  inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \
                  { return QCoreApplication::translate(#context, sourceText, disambiguation, n); }
              

              Then use it in your namespace:

              namespace MyNamespace
              {
                  Q_DECLARE_NAMESPACE_TR(MyNamespace)
              
                  int someFunction();
              }
              

              Kind regards.

              S Offline
              S Offline
              Stuhalsky Alex
              wrote on last edited by
              #6

              @kshegunov
              Important addition.
              If there is a struct|class MyType inside this namespace MyNamespace in which Q_DECLARE_NAMESPACE_TR(MyNamespace) macro used, all direct call of tr() function inside MyType methods will deduct context as MyNamespace::MyType. Not just MyNamespace. That's why translation may not work.

              Don't forget to call tr() inside this namespace like MyNamespace::tr() to avoid such context issue.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved