intl.c revision 285830
1139825Simp/* Message translation utilities.
241502Swpaul   Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
341502Swpaul
441502SwpaulThis file is part of GCC.
541502Swpaul
641502SwpaulGCC is free software; you can redistribute it and/or modify it under
741502Swpaulthe terms of the GNU General Public License as published by the Free
841502SwpaulSoftware Foundation; either version 2, or (at your option) any later
941502Swpaulversion.
1041502Swpaul
1141502SwpaulGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1241502SwpaulWARRANTY; without even the implied warranty of MERCHANTABILITY or
1341502SwpaulFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1441502Swpaulfor more details.
1541502Swpaul
1641502SwpaulYou should have received a copy of the GNU General Public License
1741502Swpaulalong with GCC; see the file COPYING.  If not, write to the Free
1841502SwpaulSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1941502Swpaul02110-1301, USA.  */
2041502Swpaul
2141502Swpaul#include "config.h"
2241502Swpaul#include "system.h"
2341502Swpaul#include "coretypes.h"
2441502Swpaul#include "tm.h"
2541502Swpaul#include "intl.h"
2641502Swpaul
2741502Swpaul#ifdef HAVE_LANGINFO_CODESET
2841502Swpaul#include <langinfo.h>
2941502Swpaul#endif
3041502Swpaul
3141502Swpaul/* Opening quotation mark for diagnostics.  */
3241502Swpaulconst char *open_quote = "'";
33122678Sobrien
34122678Sobrien/* Closing quotation mark for diagnostics.  */
35122678Sobrienconst char *close_quote = "'";
3641502Swpaul
3741502Swpaul#ifdef ENABLE_NLS
3841502Swpaul
3941502Swpaul/* Initialize the translation library for GCC.  This performs the
4041502Swpaul   appropriate sequence of calls - setlocale, bindtextdomain,
4141502Swpaul   textdomain.  LC_CTYPE determines the character set used by the
4241502Swpaul   terminal, so it has be set to output messages correctly.  */
4341502Swpaul
4441502Swpaulvoid
4541502Swpaulgcc_init_libintl (void)
4641502Swpaul{
4741502Swpaul#ifdef HAVE_LC_MESSAGES
4841502Swpaul  setlocale (LC_CTYPE, "");
4941502Swpaul  setlocale (LC_MESSAGES, "");
5041502Swpaul#else
5141502Swpaul  setlocale (LC_ALL, "");
5241502Swpaul#endif
5341502Swpaul
5441502Swpaul  (void) bindtextdomain ("gcc", LOCALEDIR);
5541502Swpaul  (void) textdomain ("gcc");
5641502Swpaul
5741502Swpaul  /* Opening quotation mark.  */
5841502Swpaul  open_quote = _("`");
5941502Swpaul
6041502Swpaul  /* Closing quotation mark.  */
6141502Swpaul  close_quote = _("'");
6241502Swpaul
6341502Swpaul  if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
6441502Swpaul    {
6541502Swpaul#if defined HAVE_LANGINFO_CODESET
6641502Swpaul      const char *encoding;
6741502Swpaul#endif
6841502Swpaul      /* Untranslated quotes that it may be possible to replace with
6941502Swpaul	 U+2018 and U+2019; but otherwise use "'" instead of "`" as
7041502Swpaul	 opening quote.  */
7141502Swpaul      open_quote = "'";
7241502Swpaul#if defined HAVE_LANGINFO_CODESET
7341502Swpaul      encoding = nl_langinfo (CODESET);
7441502Swpaul      if (encoding != NULL
7541502Swpaul	  && (!strcasecmp (encoding, "utf-8")
7641502Swpaul	      || !strcasecmp (encoding, "utf8")))
7741502Swpaul	{
7841502Swpaul	  open_quote = "\xe2\x80\x98";
7941502Swpaul	  close_quote = "\xe2\x80\x99";
8041502Swpaul	}
8141502Swpaul#endif
8241502Swpaul    }
8341502Swpaul}
8441502Swpaul
8541502Swpaul#if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
8641502Swpaul#include <wchar.h>
8741502Swpaul
8841502Swpaul/* Returns the width in columns of MSGSTR, which came from gettext.
8941502Swpaul   This is for indenting subsequent output.  */
9041502Swpaul
91129878Sphksize_t
9241502Swpaulgcc_gettext_width (const char *msgstr)
9341502Swpaul{
9450675Swpaul  size_t nwcs = mbstowcs (0, msgstr, 0);
9541502Swpaul  wchar_t *wmsgstr = alloca ((nwcs + 1) * sizeof (wchar_t));
9641502Swpaul
9741502Swpaul  mbstowcs (wmsgstr, msgstr, nwcs + 1);
9841502Swpaul  return wcswidth (wmsgstr, nwcs);
9941502Swpaul}
10041502Swpaul
101147256Sbrooks#else  /* no wcswidth */
10241502Swpaul
10341502Swpaul/* We don't have any way of knowing how wide the string is.  Guess
10441502Swpaul   the length of the string.  */
10541502Swpaul
10641502Swpaulsize_t
10741502Swpaulgcc_gettext_width (const char *msgstr)
10849611Swpaul{
10949611Swpaul  return strlen (msgstr);
11049611Swpaul}
11141502Swpaul
112119288Simp#endif
113119288Simp
11441502Swpaul#endif /* ENABLE_NLS */
11550675Swpaul