2011-11-29 Dodji Seketeli <dodji@redhat.com>
[official-gcc.git] / gcc / intl.c
blobc13ab8e63d8a3898e0231535b86bcfdad9e31bf2
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 "intl.h"
26 #ifdef HAVE_LANGINFO_CODESET
27 #include <langinfo.h>
28 #endif
30 /* Opening quotation mark for diagnostics. */
31 const char *open_quote = "'";
33 /* Closing quotation mark for diagnostics. */
34 const char *close_quote = "'";
36 /* The name of the locale encoding. */
37 const char *locale_encoding = NULL;
39 /* Whether the locale is using UTF-8. */
40 bool locale_utf8 = false;
42 #ifdef ENABLE_NLS
44 /* Initialize the translation library for GCC. This performs the
45 appropriate sequence of calls - setlocale, bindtextdomain,
46 textdomain. LC_CTYPE determines the character set used by the
47 terminal, so it has be set to output messages correctly. */
49 void
50 gcc_init_libintl (void)
52 #ifdef HAVE_LC_MESSAGES
53 setlocale (LC_CTYPE, "");
54 setlocale (LC_MESSAGES, "");
55 #else
56 setlocale (LC_ALL, "");
57 #endif
59 (void) bindtextdomain ("gcc", LOCALEDIR);
60 (void) textdomain ("gcc");
62 /* Opening quotation mark. */
63 open_quote = _("`");
65 /* Closing quotation mark. */
66 close_quote = _("'");
68 #if defined HAVE_LANGINFO_CODESET
69 locale_encoding = nl_langinfo (CODESET);
70 if (locale_encoding != NULL
71 && (!strcasecmp (locale_encoding, "utf-8")
72 || !strcasecmp (locale_encoding, "utf8")))
73 locale_utf8 = true;
74 #endif
76 if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
78 /* Untranslated quotes that it may be possible to replace with
79 U+2018 and U+2019; but otherwise use "'" instead of "`" as
80 opening quote. */
81 open_quote = "'";
82 #if defined HAVE_LANGINFO_CODESET
83 if (locale_utf8)
85 open_quote = "\xe2\x80\x98";
86 close_quote = "\xe2\x80\x99";
88 #endif
92 #if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
93 #include <wchar.h>
95 /* Returns the width in columns of MSGSTR, which came from gettext.
96 This is for indenting subsequent output. */
98 size_t
99 gcc_gettext_width (const char *msgstr)
101 size_t nwcs = mbstowcs (0, msgstr, 0);
102 wchar_t *wmsgstr = XALLOCAVEC (wchar_t, nwcs + 1);
104 mbstowcs (wmsgstr, msgstr, nwcs + 1);
105 return wcswidth (wmsgstr, nwcs);
108 #else /* no wcswidth */
110 /* We don't have any way of knowing how wide the string is. Guess
111 the length of the string. */
113 size_t
114 gcc_gettext_width (const char *msgstr)
116 return strlen (msgstr);
119 #endif
121 #endif /* ENABLE_NLS */
123 #ifndef ENABLE_NLS
125 const char *
126 fake_ngettext (const char *singular, const char *plural, unsigned long n)
128 if (n == 1UL)
129 return singular;
131 return plural;
134 #endif
136 /* Return the indent for successive lines, using the width of
137 the STR. STR must have been translated already. The string
138 must be freed by the caller. */
140 char *
141 get_spaces (const char *str)
143 size_t len = gcc_gettext_width (str);
144 char *spaces = XNEWVEC(char, len + 1);
145 memset (spaces, ' ', len);
146 spaces[len] = '\0';
147 return spaces;