GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
locale.c
Go to the documentation of this file.
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 
16 #include <grass/config.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <locale.h>
20 #include <grass/glocale.h>
21 #include <grass/gis.h>
22 
23 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
24 #define NO_NLS_UNUSED
25 #else
26 #define NO_NLS_UNUSED UNUSED
27 #endif
28 
29 void G_init_locale(void)
30 {
31  static int initialized;
32 
33  if (G_is_initialized(&initialized))
34  return;
35 
36  setlocale(LC_CTYPE, "");
37 
38 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
39 #ifdef LC_MESSAGES
40  setlocale(LC_MESSAGES, "");
41 #endif
42  const char *gisbase = getenv("GISBASE");
43 
44  if (gisbase && *gisbase) {
45  char localedir[GPATH_MAX];
46 
47  strcpy(localedir, gisbase);
48  strcat(localedir, "/locale");
49 
50  bindtextdomain("grasslibs", localedir);
51  bindtextdomain("grassmods", localedir);
52  }
53 #endif
54 
55  G_initialize_done(&initialized);
56 }
57 
58 /**
59  * \brief Gets localized text.
60  *
61  * \param[in] package
62  * \param[in] msgid
63  * \retval char * Pointer to string
64  */
65 
66 char *G_gettext(const char *package NO_NLS_UNUSED, const char *msgid)
67 {
68 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
69  G_init_locale();
70 
71  return dgettext(package, msgid);
72 #else
73  return (char *)msgid;
74 #endif
75 }
76 
77 /**
78  * \brief Gets localized text with correct plural forms.
79  *
80  * \param[in] package
81  * \param[in] msgids A singular version of string
82  * \param[in] msgidp A plural version of string
83  * \param[in] n The number
84  * \retval char * Pointer to string
85  */
86 
87 char *G_ngettext(const char *package NO_NLS_UNUSED, const char *msgids,
88  const char *msgidp, unsigned long int n)
89 {
90 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
91  G_init_locale();
92 
93  return dngettext(package, msgids, msgidp, n);
94 #else
95  return n == 1 ? (char *)msgids : (char *)msgidp;
96 #endif
97 }
int G_is_initialized(int *)
Definition: counter.c:60
void G_initialize_done(int *)
Definition: counter.c:77
#define GPATH_MAX
Definition: gis.h:194
#define NO_NLS_UNUSED
Definition: locale.c:24
void G_init_locale(void)
Definition: locale.c:29
char * G_ngettext(const char *package NO_NLS_UNUSED, const char *msgids, const char *msgidp, unsigned long int n)
Gets localized text with correct plural forms.
Definition: locale.c:87
char * G_gettext(const char *package NO_NLS_UNUSED, const char *msgid)
Gets localized text.
Definition: locale.c:66
#define strcpy
Definition: parson.c:62