gettext: Optimized gettext

1 
1 11.2.7 Optimization of the *gettext functions
1 ---------------------------------------------
1 
1    At this point of the discussion we should talk about an advantage of
1 the GNU ‘gettext’ implementation.  Some readers might have pointed out
1 that an internationalized program might have a poor performance if some
1 string has to be translated in an inner loop.  While this is unavoidable
1 when the string varies from one run of the loop to the other it is
1 simply a waste of time when the string is always the same.  Take the
1 following example:
1 
1      {
1        while (…)
1          {
1            puts (gettext ("Hello world"));
1          }
1      }
1 
1 When the locale selection does not change between two runs the resulting
1 string is always the same.  One way to use this is:
1 
1      {
1        str = gettext ("Hello world");
1        while (…)
1          {
1            puts (str);
1          }
1      }
1 
1 But this solution is not usable in all situation (e.g. when the locale
1 selection changes) nor does it lead to legible code.
1 
1    For this reason, GNU ‘gettext’ caches previous translation results.
1 When the same translation is requested twice, with no new message
1 catalogs being loaded in between, ‘gettext’ will, the second time, find
1 the result through a single cache lookup.
1