bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: does setlocale work fine in Win32?


From: Bruno Haible
Subject: Re: does setlocale work fine in Win32?
Date: Sun, 22 Feb 2004 14:45:21 +0100
User-agent: KMail/1.5

Leonardo Kausilas wrote:
> >     I'm working with GetText on Windows and I'm needing to do a call to
> > "setlocale" from my main program in order to set the locale from where the
> > messages' catalog is loaded. ...
> >     What is the correct way to set the locale from inside the
> > application in Windows?

For applications that are ported from/to Unix, the correct way is

    if (my_setenv ("LC_ALL", "address@hidden"))
      do_error_handling();

where my_setenv is defined roughly like this:

int my_setenv (const char * name, const char * value) {
  size_t namelen = strlen(name);
  size_t valuelen = (value==NULL ? 0 : strlen(value));
#if defined _WIN32
  /* On Woe32, each process has two copies of the environment variables,
     one managed by the OS and one managed by the C library. We set
     the value in both locations, so that other software that looks in
     one place or the other is guaranteed to see the value. Even if it's
     a bit slow. See also
     <http://article.gmane.org/gmane.comp.gnu.mingw.user/8272>
     <http://article.gmane.org/gmane.comp.gnu.mingw.user/8273>
     <http://www.cygwin.com/ml/cygwin/1999-04/msg00478.html> */
  if (!SetEnvironmentVariableA(name,value))
    return -1; 
#endif
#if defined(HAVE_PUTENV)
  char* buffer = (char*)malloc(namelen+1+valuelen+1);
  if (!buffer)
    return -1; /* no need to set errno = ENOMEM */
  memcpy(buffer,name,namelen);
  if (value != NULL) {
    buffer[namelen] = '=';
    memcpy(buffer+namelen+1,value,valuelen);
    buffer[namelen+1+valuelen] = 0;
  } else
    buffer[namelen] = 0;
  return putenv(buffer);
#elif defined(HAVE_SETENV)
  return setenv(name,value,1);
#else
  /* Uh oh, neither putenv() nor setenv() ... */
  return -1;
#endif
}

For Woe32-only applications this should work as well:

    if (!SetThreadLocale (...))
      do_error_handling();

> > I'm trying to set the LANG
> > environment variable to the corresponding locale. 
> >     If I do this from the Operating System, it works ok. But if I do it
> > from inside the executable with the "intl.dll" implicity linked it doesn't
> > work because it seems the a Windows DLL has its own copy of the
> > environment (if the DLL is loaded with LoadLibrary after setting the
> > environment variable, it works alright).

I don't believe that Woe32 DLLs have their own copies of "the environment".
If you want to argue on that, you need to be very convincing. You did not
even say whether you mean the getenv() visible environment or the
GetEnvironmentVariable() visible environment. Please consider
http://www.haible.de/bruno/gettext-FAQ.html#windows_setenv

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]