xref: /dragonfly/contrib/gcc-4.7/gcc/intl.c (revision ae071d8d)
1 /* Message translation utilities.
2    Copyright (C) 2001, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4 
5 This file is part of GCC.
6 
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.
11 
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.
16 
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/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "intl.h"
25 
26 #ifdef HAVE_LANGINFO_CODESET
27 #include <langinfo.h>
28 #endif
29 
30 /* Opening quotation mark for diagnostics.  */
31 const char *open_quote = "'";
32 
33 /* Closing quotation mark for diagnostics.  */
34 const char *close_quote = "'";
35 
36 /* The name of the locale encoding.  */
37 const char *locale_encoding = NULL;
38 
39 /* Whether the locale is using UTF-8.  */
40 bool locale_utf8 = false;
41 
42 #ifdef ENABLE_NLS
43 
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.  */
48 
49 void
50 gcc_init_libintl (void)
51 {
52 #ifdef HAVE_LC_MESSAGES
53   setlocale (LC_CTYPE, "");
54   setlocale (LC_MESSAGES, "");
55 #else
56   setlocale (LC_ALL, "");
57 #endif
58 
59   (void) bindtextdomain ("gcc", LOCALEDIR);
60   (void) textdomain ("gcc");
61 
62   /* Opening quotation mark.  */
63   open_quote = _("`");
64 
65   /* Closing quotation mark.  */
66   close_quote = _("'");
67 
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
75 
76   if (!strcmp (open_quote, "`") && !strcmp (close_quote, "'"))
77     {
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)
84 	{
85 	  open_quote = "\xe2\x80\x98";
86 	  close_quote = "\xe2\x80\x99";
87 	}
88 #endif
89     }
90 }
91 
92 #if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
93 #include <wchar.h>
94 
95 /* Returns the width in columns of MSGSTR, which came from gettext.
96    This is for indenting subsequent output.  */
97 
98 size_t
99 gcc_gettext_width (const char *msgstr)
100 {
101   size_t nwcs = mbstowcs (0, msgstr, 0);
102   wchar_t *wmsgstr = XALLOCAVEC (wchar_t, nwcs + 1);
103 
104   mbstowcs (wmsgstr, msgstr, nwcs + 1);
105   return wcswidth (wmsgstr, nwcs);
106 }
107 
108 #else  /* no wcswidth */
109 
110 /* We don't have any way of knowing how wide the string is.  Guess
111    the length of the string.  */
112 
113 size_t
114 gcc_gettext_width (const char *msgstr)
115 {
116   return strlen (msgstr);
117 }
118 
119 #endif
120 
121 #endif /* ENABLE_NLS */
122 
123 #ifndef ENABLE_NLS
124 
125 const char *
126 fake_ngettext (const char *singular, const char *plural, unsigned long n)
127 {
128   if (n == 1UL)
129     return singular;
130 
131   return plural;
132 }
133 
134 #endif
135 
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.  */
139 
140 char *
141 get_spaces (const char *str)
142 {
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;
148 }
149 
150 
151 
152