190075Sobrien/* Message translation utilities.
2169689Skan   Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
352284Sobrien
490075SobrienThis file is part of GCC.
590075Sobrien
690075SobrienGCC is free software; you can redistribute it and/or modify it under
790075Sobrienthe terms of the GNU General Public License as published by the Free
890075SobrienSoftware Foundation; either version 2, or (at your option) any later
990075Sobrienversion.
1090075Sobrien
1190075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1290075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1390075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1490075Sobrienfor more details.
1590075Sobrien
1690075SobrienYou should have received a copy of the GNU General Public License
1790075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
18169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19169689Skan02110-1301, USA.  */
2090075Sobrien
2190075Sobrien#include "config.h"
2290075Sobrien#include "system.h"
23132718Skan#include "coretypes.h"
24132718Skan#include "tm.h"
2552284Sobrien#include "intl.h"
2652284Sobrien
27169689Skan#ifdef HAVE_LANGINFO_CODESET
28169689Skan#include <langinfo.h>
29169689Skan#endif
30169689Skan
31169689Skan/* Opening quotation mark for diagnostics.  */
32169689Skanconst char *open_quote = "'";
33169689Skan
34169689Skan/* Closing quotation mark for diagnostics.  */
35169689Skanconst char *close_quote = "'";
36169689Skan
3790075Sobrien#ifdef ENABLE_NLS
3890075Sobrien
3990075Sobrien/* Initialize the translation library for GCC.  This performs the
4090075Sobrien   appropriate sequence of calls - setlocale, bindtextdomain,
4190075Sobrien   textdomain.  LC_CTYPE determines the character set used by the
4290075Sobrien   terminal, so it has be set to output messages correctly.  */
4390075Sobrien
4490075Sobrienvoid
45132718Skangcc_init_libintl (void)
4690075Sobrien{
4790075Sobrien#ifdef HAVE_LC_MESSAGES
4890075Sobrien  setlocale (LC_CTYPE, "");
4990075Sobrien  setlocale (LC_MESSAGES, "");
5090075Sobrien#else
5190075Sobrien  setlocale (LC_ALL, "");
5290075Sobrien#endif
5390075Sobrien
5490075Sobrien  (void) bindtextdomain ("gcc", LOCALEDIR);
5590075Sobrien  (void) textdomain ("gcc");
56169689Skan
57169689Skan  /* Opening quotation mark.  */
58169689Skan  open_quote = _("`");
59169689Skan
60169689Skan  /* Closing quotation mark.  */
61169689Skan  close_quote = _("'");
62169689Skan
63169689Skan  if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
64169689Skan    {
65169689Skan#if defined HAVE_LANGINFO_CODESET
66169689Skan      const char *encoding;
67169689Skan#endif
68169689Skan      /* Untranslated quotes that it may be possible to replace with
69169689Skan	 U+2018 and U+2019; but otherwise use "'" instead of "`" as
70169689Skan	 opening quote.  */
71169689Skan      open_quote = "'";
72169689Skan#if defined HAVE_LANGINFO_CODESET
73169689Skan      encoding = nl_langinfo (CODESET);
74169689Skan      if (encoding != NULL
75169689Skan	  && (!strcasecmp (encoding, "utf-8")
76169689Skan	      || !strcasecmp (encoding, "utf8")))
77169689Skan	{
78169689Skan	  open_quote = "\xe2\x80\x98";
79169689Skan	  close_quote = "\xe2\x80\x99";
80169689Skan	}
81169689Skan#endif
82169689Skan    }
8390075Sobrien}
8490075Sobrien
85132718Skan#if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
86132718Skan#include <wchar.h>
87132718Skan
88132718Skan/* Returns the width in columns of MSGSTR, which came from gettext.
89132718Skan   This is for indenting subsequent output.  */
90132718Skan
91132718Skansize_t
92132718Skangcc_gettext_width (const char *msgstr)
93132718Skan{
94132718Skan  size_t nwcs = mbstowcs (0, msgstr, 0);
95132718Skan  wchar_t *wmsgstr = alloca ((nwcs + 1) * sizeof (wchar_t));
96132718Skan
97132718Skan  mbstowcs (wmsgstr, msgstr, nwcs + 1);
98132718Skan  return wcswidth (wmsgstr, nwcs);
99132718Skan}
100132718Skan
101132718Skan#else  /* no wcswidth */
102132718Skan
103132718Skan/* We don't have any way of knowing how wide the string is.  Guess
104132718Skan   the length of the string.  */
105132718Skan
106132718Skansize_t
107132718Skangcc_gettext_width (const char *msgstr)
108132718Skan{
109132718Skan  return strlen (msgstr);
110132718Skan}
111132718Skan
11290075Sobrien#endif
113132718Skan
114132718Skan#endif /* ENABLE_NLS */
115