1 /* Message translation utilities.
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #ifdef HAVE_LANGINFO_CODESET
29 /* Opening quotation mark for diagnostics. */
30 const char *open_quote
= "'";
32 /* Closing quotation mark for diagnostics. */
33 const char *close_quote
= "'";
35 /* The name of the locale encoding. */
36 const char *locale_encoding
= NULL
;
38 /* Whether the locale is using UTF-8. */
39 bool locale_utf8
= false;
43 /* Initialize the translation library for GCC. This performs the
44 appropriate sequence of calls - setlocale, bindtextdomain,
45 textdomain. LC_CTYPE determines the character set used by the
46 terminal, so it has be set to output messages correctly. */
49 gcc_init_libintl (void)
51 #ifdef HAVE_LC_MESSAGES
52 setlocale (LC_CTYPE
, "");
53 setlocale (LC_MESSAGES
, "");
55 setlocale (LC_ALL
, "");
58 (void) bindtextdomain ("gcc", LOCALEDIR
);
59 (void) textdomain ("gcc");
61 /* Opening quotation mark. */
64 /* Closing quotation mark. */
67 #if defined HAVE_LANGINFO_CODESET
68 locale_encoding
= nl_langinfo (CODESET
);
69 if (locale_encoding
!= NULL
70 && (!strcasecmp (locale_encoding
, "utf-8")
71 || !strcasecmp (locale_encoding
, "utf8")))
75 if (!strcmp (open_quote
, "`") && !strcmp (close_quote
, "'"))
77 /* Untranslated quotes that it may be possible to replace with
78 U+2018 and U+2019; but otherwise use "'" instead of "`" as
81 #if defined HAVE_LANGINFO_CODESET
84 open_quote
= "\xe2\x80\x98";
85 close_quote
= "\xe2\x80\x99";
91 #if defined HAVE_WCHAR_H && defined HAVE_WORKING_MBSTOWCS && defined HAVE_WCSWIDTH
94 /* Returns the width in columns of MSGSTR, which came from gettext.
95 This is for indenting subsequent output. */
98 gcc_gettext_width (const char *msgstr
)
100 size_t nwcs
= mbstowcs (0, msgstr
, 0);
101 wchar_t *wmsgstr
= XALLOCAVEC (wchar_t, nwcs
+ 1);
103 mbstowcs (wmsgstr
, msgstr
, nwcs
+ 1);
104 return wcswidth (wmsgstr
, nwcs
);
107 #else /* no wcswidth */
109 /* We don't have any way of knowing how wide the string is. Guess
110 the length of the string. */
113 gcc_gettext_width (const char *msgstr
)
115 return strlen (msgstr
);
120 #endif /* ENABLE_NLS */
125 fake_ngettext (const char *singular
, const char *plural
, unsigned long n
)
135 /* Return the indent for successive lines, using the width of
136 the STR. STR must have been translated already. The string
137 must be freed by the caller. */
140 get_spaces (const char *str
)
142 size_t len
= gcc_gettext_width (str
);
143 char *spaces
= XNEWVEC (char, len
+ 1);
144 memset (spaces
, ' ', len
);