locale.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*!
  2. * \file lib/gis/locale.c
  3. *
  4. * \brief GIS Library - Functions to handle locale.
  5. *
  6. * (C) 2001-2014 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. #include <grass/gis.h>
  21. void G_init_locale(void)
  22. {
  23. static int initialized;
  24. if (G_is_initialized(&initialized))
  25. return;
  26. setlocale(LC_CTYPE, "");
  27. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  28. #ifdef LC_MESSAGES
  29. setlocale(LC_MESSAGES, "");
  30. #endif
  31. const char *gisbase = getenv("GISBASE");
  32. if (gisbase && *gisbase) {
  33. char localedir[GPATH_MAX];
  34. strcpy(localedir, gisbase);
  35. strcat(localedir, "/locale");
  36. bindtextdomain("grasslibs", localedir);
  37. bindtextdomain("grassmods", localedir);
  38. }
  39. #endif
  40. G_initialize_done(&initialized);
  41. }
  42. /**
  43. * \brief Gets localized text.
  44. *
  45. * \param[in] package
  46. * \param[in] msgid
  47. * \retval char * Pointer to string
  48. */
  49. char *G_gettext(const char *package, const char *msgid)
  50. {
  51. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  52. G_init_locale();
  53. return dgettext(package, msgid);
  54. #else
  55. return (char *)msgid;
  56. #endif
  57. }
  58. /**
  59. * \brief Gets localized text with correct plural forms.
  60. *
  61. * \param[in] package
  62. * \param[in] msgids A singular version of string
  63. * \param[in] msgidp A plural version of string
  64. * \param[in] n The number
  65. * \retval char * Pointer to string
  66. */
  67. char *G_ngettext(const char *package, const char *msgids, const char *msgidp, unsigned long int n)
  68. {
  69. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  70. G_init_locale();
  71. return dngettext(package, msgids, msgidp, n);
  72. #else
  73. return n == 1 ? (char *)msgids : (char *)msgidp;
  74. #endif
  75. }