locale.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * \file locale.c
  3. *
  4. * \brief GIS Library - Functions to handle locale.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 2004-2008
  14. */
  15. #include <grass/config.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <locale.h>
  19. #include <grass/glocale.h>
  20. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  21. static char *locale_dir(void)
  22. {
  23. static char localedir[4096];
  24. const char *gisbase;
  25. if (*localedir)
  26. return localedir;
  27. gisbase = getenv("GISBASE");
  28. if (!gisbase || !*gisbase)
  29. return "";
  30. strcpy(localedir, gisbase);
  31. strcat(localedir, "/locale");
  32. return localedir;
  33. }
  34. #endif
  35. /**
  36. * \brief Gets localized text.
  37. *
  38. * \param[in] package
  39. * \param[in] msgid
  40. * \retval char * Pointer to string
  41. */
  42. char *G_gettext(const char *package, const char *msgid)
  43. {
  44. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  45. static char now_bound[4096];
  46. static int initialized;
  47. if (!initialized) {
  48. setlocale(LC_CTYPE, "");
  49. setlocale(LC_MESSAGES, "");
  50. initialized = 1;
  51. }
  52. if (strcmp(now_bound, package) != 0) {
  53. strcpy(now_bound, package);
  54. bindtextdomain(package, locale_dir());
  55. }
  56. return dgettext(package, msgid);
  57. #else
  58. return (char *)msgid;
  59. #endif
  60. }