intl.c revision 169689
111126Sjulian/* Message translation utilities.
2103722Sphk   Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
311126Sjulian
411126SjulianThis file is part of GCC.
511126Sjulian
611126SjulianGCC is free software; you can redistribute it and/or modify it under
711126Sjulianthe terms of the GNU General Public License as published by the Free
811126SjulianSoftware Foundation; either version 2, or (at your option) any later
911126Sjulianversion.
1011126Sjulian
1111126SjulianGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1211126SjulianWARRANTY; without even the implied warranty of MERCHANTABILITY or
1311126SjulianFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14103722Sphkfor more details.
15103722Sphk
1611126SjulianYou should have received a copy of the GNU General Public License
17103722Sphkalong with GCC; see the file COPYING.  If not, write to the Free
1811126SjulianSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1911126Sjulian02110-1301, USA.  */
2011126Sjulian
2111126Sjulian#include "config.h"
2211126Sjulian#include "system.h"
2311126Sjulian#include "coretypes.h"
2411126Sjulian#include "tm.h"
2511126Sjulian#include "intl.h"
2611126Sjulian
27116182Sobrien#ifdef HAVE_LANGINFO_CODESET
28116182Sobrien#include <langinfo.h>
29116182Sobrien#endif
3011126Sjulian
3148936Sphk/* Opening quotation mark for diagnostics.  */
3283366Sjulianconst char *open_quote = "'";
33178991Skib
34111179Sphk/* Closing quotation mark for diagnostics.  */
3590737Sgreenconst char *close_quote = "'";
3690737Sgreen
3736735Sdfr#ifdef ENABLE_NLS
3848936Sphk
3911126Sjulian/* Initialize the translation library for GCC.  This performs the
4012954Sjulian   appropriate sequence of calls - setlocale, bindtextdomain,
4148936Sphk   textdomain.  LC_CTYPE determines the character set used by the
42120514Sphk   terminal, so it has be set to output messages correctly.  */
43171181Skib
4465374Sphkvoid
45147982Srwatsongcc_init_libintl (void)
46171181Skib{
4749535Sphk#ifdef HAVE_LC_MESSAGES
4811126Sjulian  setlocale (LC_CTYPE, "");
49149144Sphk  setlocale (LC_MESSAGES, "");
50193275Sjhb#else
51149144Sphk  setlocale (LC_ALL, "");
52131996Sphk#endif
5348936Sphk
54150342Sphk  (void) bindtextdomain ("gcc", LOCALEDIR);
55142242Sphk  (void) textdomain ("gcc");
56171188Skib
57171188Skib  /* Opening quotation mark.  */
58209106Skib  open_quote = _("`");
59209106Skib
60171181Skib  /* Closing quotation mark.  */
61171181Skib  close_quote = _("'");
62170950Skib
63170950Skib  if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
64177301Skib    {
65201145Santoine#if defined HAVE_LANGINFO_CODESET
66170950Skib      const char *encoding;
67135600Sphk#endif
68135600Sphk      /* Untranslated quotes that it may be possible to replace with
69126082Sphk	 U+2018 and U+2019; but otherwise use "'" instead of "`" as
70151450Sjhb	 opening quote.  */
71126082Sphk      open_quote = "'";
72126082Sphk#if defined HAVE_LANGINFO_CODESET
73126082Sphk      encoding = nl_langinfo (CODESET);
74177301Skib      if (encoding != NULL
75177301Skib	  && (!strcasecmp (encoding, "utf-8")
76177301Skib	      || !strcasecmp (encoding, "utf8")))
77177301Skib	{
78177301Skib	  open_quote = "\xe2\x80\x98";
79177301Skib	  close_quote = "\xe2\x80\x99";
80170950Skib	}
81170950Skib#endif
82170950Skib    }
83177301Skib}
84177301Skib
85170950Skib#if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
86177301Skib#include <wchar.h>
87170950Skib
88170950Skib/* Returns the width in columns of MSGSTR, which came from gettext.
89177301Skib   This is for indenting subsequent output.  */
90177301Skib
91177301Skibsize_t
92177301Skibgcc_gettext_width (const char *msgstr)
93177301Skib{
94177301Skib  size_t nwcs = mbstowcs (0, msgstr, 0);
95177301Skib  wchar_t *wmsgstr = alloca ((nwcs + 1) * sizeof (wchar_t));
96177301Skib
97177301Skib  mbstowcs (wmsgstr, msgstr, nwcs + 1);
98177301Skib  return wcswidth (wmsgstr, nwcs);
99177301Skib}
100177301Skib
101177301Skib#else  /* no wcswidth */
102177301Skib
103170950Skib/* We don't have any way of knowing how wide the string is.  Guess
104170950Skib   the length of the string.  */
105177301Skib
106177301Skibsize_t
107177301Skibgcc_gettext_width (const char *msgstr)
108177301Skib{
109170950Skib  return strlen (msgstr);
110170950Skib}
111170950Skib
112170950Skib#endif
113170950Skib
114170950Skib#endif /* ENABLE_NLS */
115170950Skib