[Top][All Lists]

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

Re: LANG and @documentlanguage

From: Bruno Haible
Subject: Re: LANG and @documentlanguage
Date: Tue, 2 Dec 2003 12:58:42 +0100
User-agent: KMail/1.5

Eli Zaretskii wrote:

> The problem is to implement this, and do that correctly.  AFAIR, the
> gettext library ... doesn't let you mix
> languages freely.  They require that you set the language and then
> translate the strings according to that language.  By contrast, what
> we need is to support 2 languages at once: one to print messages meant
> for the consumption of the user who runs makeinfo, the other for the
> fixed strings we put into the output document.


> Any ideas as to how to implement this cleanly are welcome.

This example is taken from the GNU gettext sources. It has also to cope
with an encoding issue: The locale's encoding and the output document's
encoding may be different.

/* Return the title format string.  */
static const char *
get_title ()
  /* This is tricky.  We want the translation in the given locale specified by
     the command line, not the current locale.  But we want it in the encoding
     that we put into the header entry, not the encoding of that locale.
     We could avoid the use of OUTPUT_CHARSET by using a separate message
     catalog and bind_textdomain_codeset(), but that doesn't seem worth the
     trouble for one single message.  */
  const char *encoding;
  const char *tmp;
  char *old_LC_ALL;
  char *old_LANGUAGE;
  char *old_OUTPUT_CHARSET;
  const char *msgid;
  const char *english;
  const char *result;

  encoding = canonical_locale_charset ();

  /* First, the English title.  */
  english = xasprintf ("%s translations for %%s package",
                       englishname_of_language ());

  /* Save LC_ALL, LANGUAGE, OUTPUT_CHARSET environment variables.  */

  tmp = getenv ("LC_ALL");
  old_LC_ALL = (tmp != NULL ? xstrdup (tmp) : NULL);

  tmp = getenv ("LANGUAGE");
  old_LANGUAGE = (tmp != NULL ? xstrdup (tmp) : NULL);

  tmp = getenv ("OUTPUT_CHARSET");
  old_OUTPUT_CHARSET = (tmp != NULL ? xstrdup (tmp) : NULL);

  xsetenv ("LC_ALL", locale, 1);
  unsetenv ("LANGUAGE");
  xsetenv ("OUTPUT_CHARSET", encoding, 1);

  if (setlocale (LC_ALL, "") == NULL)
    /* Nonexistent locale.  Use the English title.  */
    result = english;
      /* Fetch the translation.  */
      /* TRANSLATORS: "English" needs to be replaced by your language.
         For example in it.po write "Traduzioni italiani ...",
         *not* "Traduzioni inglesi ...".  */
      result = gettext ("English translations for %s package");

  /* Restore LC_ALL, LANGUAGE, OUTPUT_CHARSET environment variables.  */

  if (old_LC_ALL != NULL)
    xsetenv ("LC_ALL", old_LC_ALL, 1), free (old_LC_ALL);
    unsetenv ("LC_ALL");

  if (old_LANGUAGE != NULL)
    xsetenv ("LANGUAGE", old_LANGUAGE, 1), free (old_LANGUAGE);
    unsetenv ("LANGUAGE");

    xsetenv ("OUTPUT_CHARSET", old_OUTPUT_CHARSET, 1), free 
    unsetenv ("OUTPUT_CHARSET");

  setlocale (LC_ALL, "");

  return result;

The functions xsetenv() and unsetenv() are available through gnulib.


reply via email to

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