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