gettext: gettext grok

1 
1 11.5 Being a ‘gettext’ grok
1 ===========================
1 
1    * NOTE: * This documentation section is outdated and needs to be
1 revised.
1 
1    To fully exploit the functionality of the GNU ‘gettext’ library it is
1 surely helpful to read the source code.  But for those who don’t want to
1 spend that much time in reading the (sometimes complicated) code here is
1 a list comments:
1 
1    • Changing the language at runtime
1 
1      For interactive programs it might be useful to offer a selection of
1      the used language at runtime.  To understand how to do this one
1      need to know how the used language is determined while executing
1      the ‘gettext’ function.  The method which is presented here only
1      works correctly with the GNU implementation of the ‘gettext’
1      functions.
1 
1      In the function ‘dcgettext’ at every call the current setting of
1      the highest priority environment variable is determined and used.
1      Highest priority means here the following list with decreasing
1      priority:
1 
1        1. ‘LANGUAGE’
1        2. ‘LC_ALL’
1        3. ‘LC_xxx’, according to selected locale category
1        4. ‘LANG’
1 
1      Afterwards the path is constructed using the found value and the
1      translation file is loaded if available.
1 
1      What happens now when the value for, say, ‘LANGUAGE’ changes?
1      According to the process explained above the new value of this
1      variable is found as soon as the ‘dcgettext’ function is called.
1      But this also means the (perhaps) different message catalog file is
1      loaded.  In other words: the used language is changed.
1 
1      But there is one little hook.  The code for gcc-2.7.0 and up
1      provides some optimization.  This optimization normally prevents
1      the calling of the ‘dcgettext’ function as long as no new catalog
1      is loaded.  But if ‘dcgettext’ is not called the program also
11      cannot find the ‘LANGUAGE’ variable be changed (⇒Optimized
      gettext).  A solution for this is very easy.  Include the
1      following code in the language switching function.
1 
1             /* Change language.  */
1             setenv ("LANGUAGE", "fr", 1);
1 
1             /* Make change known.  */
1             {
1               extern int  _nl_msg_cat_cntr;
1               ++_nl_msg_cat_cntr;
1             }
1 
1      The variable ‘_nl_msg_cat_cntr’ is defined in ‘loadmsgcat.c’.  You
1      don’t need to know what this is for.  But it can be used to detect
1      whether a ‘gettext’ implementation is GNU gettext and not non-GNU
1      system’s native gettext implementation.
1