* config/darwin-c.c: Remove c-tree.h include.
[official-gcc.git] / gcc / intl.c
blobf75be1f2144f7674d7001417ecf08a5ef2198647
1 /* Message translation utilities.
2 Copyright (C) 2001, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "intl.h"
27 #ifdef HAVE_LANGINFO_CODESET
28 #include <langinfo.h>
29 #endif
31 /* Opening quotation mark for diagnostics. */
32 const char *open_quote = "'";
34 /* Closing quotation mark for diagnostics. */
35 const char *close_quote = "'";
37 /* The name of the locale encoding. */
38 const char *locale_encoding = NULL;
40 /* Whether the locale is using UTF-8. */
41 bool locale_utf8 = false;
43 #ifdef ENABLE_NLS
45 /* Initialize the translation library for GCC. This performs the
46 appropriate sequence of calls - setlocale, bindtextdomain,
47 textdomain. LC_CTYPE determines the character set used by the
48 terminal, so it has be set to output messages correctly. */
50 void
51 gcc_init_libintl (void)
53 #ifdef HAVE_LC_MESSAGES
54 setlocale (LC_CTYPE, "");
55 setlocale (LC_MESSAGES, "");
56 #else
57 setlocale (LC_ALL, "");
58 #endif
60 (void) bindtextdomain ("gcc", LOCALEDIR);
61 (void) textdomain ("gcc");
63 /* Opening quotation mark. */
64 open_quote = _("`");
66 /* Closing quotation mark. */
67 close_quote = _("'");
69 #if defined HAVE_LANGINFO_CODESET
70 locale_encoding = nl_langinfo (CODESET);
71 if (locale_encoding != NULL
72 && (!strcasecmp (locale_encoding, "utf-8")
73 || !strcasecmp (locale_encoding, "utf8")))
74 locale_utf8 = true;
75 #endif
77 if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
79 /* Untranslated quotes that it may be possible to replace with
80 U+2018 and U+2019; but otherwise use "'" instead of "`" as
81 opening quote. */
82 open_quote = "'";
83 #if defined HAVE_LANGINFO_CODESET
84 if (locale_utf8)
86 open_quote = "\xe2\x80\x98";
87 close_quote = "\xe2\x80\x99";
89 #endif
93 #if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
94 #include <wchar.h>
96 /* Returns the width in columns of MSGSTR, which came from gettext.
97 This is for indenting subsequent output. */
99 size_t
100 gcc_gettext_width (const char *msgstr)
102 size_t nwcs = mbstowcs (0, msgstr, 0);
103 wchar_t *wmsgstr = XALLOCAVEC (wchar_t, nwcs + 1);
105 mbstowcs (wmsgstr, msgstr, nwcs + 1);
106 return wcswidth (wmsgstr, nwcs);
109 #else /* no wcswidth */
111 /* We don't have any way of knowing how wide the string is. Guess
112 the length of the string. */
114 size_t
115 gcc_gettext_width (const char *msgstr)
117 return strlen (msgstr);
120 #endif
122 #endif /* ENABLE_NLS */
124 #ifndef ENABLE_NLS
126 const char *
127 fake_ngettext (const char *singular, const char *plural, unsigned long n)
129 if (n == 1UL)
130 return singular;
132 return plural;
135 #endif
137 /* Return the indent for successive lines, using the width of
138 the STR. STR must have been translated already. The string
139 must be freed by the caller. */
141 char *
142 get_spaces (const char *str)
144 size_t len = gcc_gettext_width (str);
145 char *spaces = XNEWVEC(char, len + 1);
146 memset (spaces, ' ', len);
147 spaces[len] = '\0';
148 return spaces;