Abdelrazak Younes wrote:
Georg Baum wrote:
Abdelrazak Younes wrote:

This last line in particular looks suspicious. Why do we set environment
variable at each translation request?

Because the environment variable determines what language gettext will use, and we don't know what other message was translated before. To my knowledge
no library call of gettext exists that takes the language as a function
argument rather than an environment variable.

[...]
Unless you really want to spend a lot of time yo learn all aspects about
gettext I suggest that you leave this alone.

OK, thanks for the explanation. I am not pretending that I will solve everything, don't be afraid ;-). We do have an internal in source gettext. Couldn't we just support that and live all other version aside? Maybe this would simplify the code a bit, dunno.

One other thing that may be changed also is the way we load po files. It seems that there is some caching done inside gettext but it is disabled as soon as we change the po file. This is explained here:

http://www.gnu.org/software/gettext/manual/html_mono/gettext.html#SEC152

Maybe there's something wrong in the way we load the po files? Just asking to you knowledgeable people...

At this link they say something interesting WRT changing language:

http://www.gnu.org/software/gettext/manual/html_mono/gettext.html#SEC155

I don't see any use of this '_nl_msg_cat_cntr' variable in the code. Maybe that's a solution. Did you tried that in the past?


 10.5 Being a gettext grok

To fully exploit the functionality of the GNU gettext library it is surely helpful to read the source code. But for those who don't want to spend that much time in reading the (sometimes complicated) code here is a list comments:

    * Changing the language at runtime

For interactive programs it might be useful to offer a selection of the used language at runtime. To understand how to do this one need to know how the used language is determined while executing the gettext function. The method which is presented here only works correctly with the GNU implementation of the gettext functions.

In the function dcgettext at every call the current setting of the highest priority environment variable is determined and used. Highest priority means here the following list with decreasing priority:

         1. LANGUAGE
         2. LC_ALL
         3. LC_xxx, according to selected locale
         4. LANG

Afterwards the path is constructed using the found value and the translation file is loaded if available.

What is now when the value for, say, LANGUAGE changes. According to the process explained above the new value of this variable is found as soon as the dcgettext function is called. But this also means the (perhaps) different message catalog file is loaded. In other words: the used language is changed.

But there is one little hook. The code for gcc-2.7.0 and up provides some optimization. This optimization normally prevents the calling of the dcgettext function as long as no new catalog is loaded. But if dcgettext is not called the program also cannot find the LANGUAGE variable be changed (see section 10.2.7 Optimization of the *gettext functions). A solution for this is very easy. Include the following code in the language switching function.

        

  /* Change language.  */
  setenv ("LANGUAGE", "fr", 1);

  /* Make change known.  */
  {
    extern int  _nl_msg_cat_cntr;
    ++_nl_msg_cat_cntr;
  }


Abdel.



Reply via email to