localeutil.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <string>
  19. #include <iostream>
  20. #include <cstring>
  21. #include "localeutil.h"
  22. namespace KC {
  23. locale_t createUTF8Locale()
  24. {
  25. locale_t loc;
  26. // this trick only works on newer distro's
  27. loc = createlocale(LC_CTYPE, "C.UTF-8");
  28. if (loc)
  29. return loc;
  30. std::string new_locale;
  31. char *cur_locale = setlocale(LC_CTYPE, NULL);
  32. char *dot = strchr(cur_locale, '.');
  33. if (dot) {
  34. if (strcmp(dot+1, "UTF-8") == 0 || strcmp(dot+1, "utf8") == 0) {
  35. loc = createlocale(LC_CTYPE, cur_locale);
  36. goto exit;
  37. }
  38. // strip current charset selector, to be replaced
  39. *dot = '\0';
  40. }
  41. new_locale = std::string(cur_locale) + ".UTF-8";
  42. loc = createlocale(LC_CTYPE, new_locale.c_str());
  43. if (loc)
  44. return loc;
  45. loc = createlocale(LC_CTYPE, "en_US.UTF-8");
  46. exit:
  47. // too bad, but I don't want to return an unusable object
  48. if (!loc)
  49. loc = createlocale(LC_CTYPE, "C");
  50. return loc;
  51. }
  52. /**
  53. * Initializes the locale to the current language, forced in UTF-8.
  54. *
  55. * @param[in] bOutput Print errors during init to stderr
  56. * @param[out] lpstrLocale Last locale trying to set (optional)
  57. * @retval true successfully initialized
  58. * @retval false error during initialization
  59. */
  60. bool forceUTF8Locale(bool bOutput, std::string *lpstrLastSetLocale)
  61. {
  62. std::string new_locale;
  63. char *old_locale = setlocale(LC_CTYPE, "");
  64. if (!old_locale) {
  65. if (bOutput)
  66. std::cerr << "Unable to initialize locale" << std::endl;
  67. return false;
  68. }
  69. char *dot = strchr(old_locale, '.');
  70. if (dot) {
  71. *dot = '\0';
  72. if (strcmp(dot+1, "UTF-8") == 0 || strcmp(dot+1, "utf8") == 0) {
  73. if (lpstrLastSetLocale)
  74. *lpstrLastSetLocale = old_locale;
  75. return true; // no need to force anything
  76. }
  77. }
  78. if (bOutput) {
  79. std::cerr << "Warning: Terminal locale not UTF-8, but UTF-8 locale is being forced." << std::endl;
  80. std::cerr << " Screen output may not be correctly printed." << std::endl;
  81. }
  82. new_locale = std::string(old_locale) + ".UTF-8";
  83. if (lpstrLastSetLocale)
  84. *lpstrLastSetLocale = new_locale;
  85. old_locale = setlocale(LC_CTYPE, new_locale.c_str());
  86. if (!old_locale) {
  87. new_locale = "en_US.UTF-8";
  88. if (lpstrLastSetLocale)
  89. *lpstrLastSetLocale = new_locale;
  90. old_locale = setlocale(LC_CTYPE, new_locale.c_str());
  91. }
  92. if (!old_locale) {
  93. if (bOutput)
  94. std::cerr << "Unable to set locale '" << new_locale << "'" << std::endl;
  95. return false;
  96. }
  97. return true;
  98. }
  99. } /* namespace */