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 
G_init_locale(void)24 void G_init_locale(void)
25 {
26     static int initialized;
27     const char *gisbase;
28 
29     if (G_is_initialized(&initialized))
30 	return;
31 
32     setlocale(LC_CTYPE, "");
33 
34 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
35 #ifdef LC_MESSAGES
36     setlocale(LC_MESSAGES, "");
37 #endif
38 
39     gisbase = getenv("GISBASE");
40     if (gisbase && *gisbase) {
41 	char localedir[GPATH_MAX];
42 
43 	strcpy(localedir, gisbase);
44 	strcat(localedir, "/locale");
45 
46 	bindtextdomain("grasslibs", localedir);
47 	bindtextdomain("grassmods", localedir);
48     }
49 #endif
50 
51     G_initialize_done(&initialized);
52 }
53 
54 
55 /**
56  * \brief Gets localized text.
57  *
58  * \param[in] package
59  * \param[in] msgid
60  * \retval char * Pointer to string
61  */
62 
G_gettext(const char * package,const char * msgid)63 char *G_gettext(const char *package, const char *msgid)
64 {
65 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
66     G_init_locale();
67 
68     return dgettext(package, msgid);
69 #else
70     return (char *)msgid;
71 #endif
72 }
73 
74 /**
75  * \brief Gets localized text with correct plural forms.
76  *
77  * \param[in] package
78  * \param[in] msgids A singular version of string
79  * \param[in] msgidp A plural version of string
80  * \param[in] n The number
81  * \retval char * Pointer to string
82  */
83 
G_ngettext(const char * package,const char * msgids,const char * msgidp,unsigned long int n)84 char *G_ngettext(const char *package, const char *msgids, const char *msgidp, unsigned long int n)
85 {
86 #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
87     G_init_locale();
88 
89     return dngettext(package, msgids, msgidp, n);
90 #else
91     return n == 1 ? (char *)msgids : (char *)msgidp;
92 #endif
93 }
94