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