intl.c 3.5 KB

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