GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
locale.c
Go to the documentation of this file.
1 
2 /*!
3  * \file lib/gis/locale.c
4  *
5  * \brief GIS Library - Functions to handle locale.
6  *
7  * (C) 2001-2014 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public License
10  * (>=v2). Read the file COPYING that comes with GRASS for details.
11  *
12  * \author GRASS GIS Development Team
13  *
14  * \date 2004-2008
15  */
16 
17 #include <grass/config.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <locale.h>
21 #include <grass/glocale.h>
22 #include <grass/gis.h>
23 
24 void G_init_locale(void)
25 {
26  static int initialized;
27 
28  if (G_is_initialized(&initialized))
29  return;
30 
31  setlocale(LC_CTYPE, "");
32 
33 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
34 #ifdef LC_MESSAGES
35  setlocale(LC_MESSAGES, "");
36 #endif
37  const char *gisbase = getenv("GISBASE");
38  if (gisbase && *gisbase) {
39  char localedir[GPATH_MAX];
40 
41  strcpy(localedir, gisbase);
42  strcat(localedir, "/locale");
43 
44  bindtextdomain("grasslibs", localedir);
45  bindtextdomain("grassmods", localedir);
46  }
47 #endif
48 
49  G_initialize_done(&initialized);
50 }
51 
52 
53 /**
54  * \brief Gets localized text.
55  *
56  * \param[in] package
57  * \param[in] msgid
58  * \retval char * Pointer to string
59  */
60 
61 char *G_gettext(const char *package, const char *msgid)
62 {
63 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
64  G_init_locale();
65 
66  return dgettext(package, msgid);
67 #else
68  return (char *)msgid;
69 #endif
70 }
71 
72 /**
73  * \brief Gets localized text with correct plural forms.
74  *
75  * \param[in] package
76  * \param[in] msgids A singular version of string
77  * \param[in] msgidp A plural version of string
78  * \param[in] n The number
79  * \retval char * Pointer to string
80  */
81 
82 char *G_ngettext(const char *package, const char *msgids, const char *msgidp, unsigned long int n)
83 {
84 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
85  G_init_locale();
86 
87  return dngettext(package, msgids, msgidp, n);
88 #else
89  return n == 1 ? (char *)msgids : (char *)msgidp;
90 #endif
91 }
char * G_gettext(const char *package, const char *msgid)
Gets localized text.
Definition: locale.c:61
char * G_ngettext(const char *package, const char *msgids, const char *msgidp, unsigned long int n)
Gets localized text with correct plural forms.
Definition: locale.c:82
void G_initialize_done(int *)
Definition: counter.c:76
#define GPATH_MAX
Definition: gis.h:180
void G_init_locale(void)
Definition: locale.c:24
int G_is_initialized(int *)
Definition: counter.c:59
char * getenv()