1 /* locale.c - Miscellaneous internationalization functions. */
3 /* Copyright (C) 1996 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
36 /* The current locale when the program begins */
37 static char *default_locale;
39 /* The current domain for textdomain(3). */
40 static char *default_domain;
41 static char *default_dir;
43 /* tracks the value of LC_ALL; used to override values for other locale
47 /* Set the value of default_locale and make the current locale the
48 system default locale. This should be called very early in main(). */
52 #if defined (HAVE_SETLOCALE)
53 default_locale = setlocale (LC_ALL, "");
55 default_locale = savestring (default_locale);
56 #endif /* HAVE_SETLOCALE */
59 /* Set default values for LC_CTYPE, LC_COLLATE, and LC_MESSAGES if they
60 are not specified in the environment, but LANG or LC_ALL is. This
61 should be called from main() after parsing the environment. */
63 set_default_locale_vars ()
67 #if defined (HAVE_SETLOCALE)
68 val = get_string_value ("LC_CTYPE");
69 if (val == 0 && lc_all && *lc_all)
70 setlocale (LC_CTYPE, lc_all);
72 # if defined (LC_COLLATE)
73 val = get_string_value ("LC_COLLATE");
74 if (val == 0 && lc_all && *lc_all)
75 setlocale (LC_COLLATE, lc_all);
76 # endif /* LC_COLLATE */
78 # if defined (LC_MESSAGES)
79 val = get_string_value ("LC_MESSAGES");
80 if (val == 0 && lc_all && *lc_all)
81 setlocale (LC_MESSAGES, lc_all);
82 # endif /* LC_MESSAGES */
84 #endif /* HAVE_SETLOCALE */
86 val = get_string_value ("TEXTDOMAIN");
89 FREE (default_domain);
90 default_domain = savestring (val);
91 textdomain (default_domain);
94 val = get_string_value ("TEXTDOMAINDIR");
98 default_dir = savestring (val);
99 bindtextdomain (default_domain, default_dir);
103 /* Set one of the locale categories (specified by VAR) to VALUE. Returns 1
104 if successful, 0 otherwise. */
106 set_locale_var (var, value)
109 if (var[0] == 'T' && var[10] == 0) /* TEXTDOMAIN */
111 FREE (default_domain);
112 default_domain = value ? savestring (value) : (char *)NULL;
113 textdomain (default_domain);
116 else if (var[0] == 'T') /* TEXTDOMAINDIR */
119 default_dir = value ? savestring (value) : (char *)NULL;
120 bindtextdomain (default_domain, default_dir);
124 /* var[0] == 'L' && var[1] == 'C' && var[2] == '_' */
126 else if (var[3] == 'A') /* LC_ALL */
129 lc_all = value ? savestring (value) : savestring (default_locale);
130 #if defined (HAVE_SETLOCALE)
131 return (setlocale (LC_ALL, value) != 0);
137 #if defined (HAVE_SETLOCALE)
138 else if (var[3] == 'C' && var[4] == 'T') /* LC_CTYPE */
140 if (lc_all == 0 || *lc_all == '\0')
141 return (setlocale (LC_CTYPE, value) != 0);
143 else if (var[3] == 'C' && var[4] == 'O') /* LC_COLLATE */
145 # if defined (LC_COLLATE)
146 if (lc_all == 0 || *lc_all == '\0')
147 return (setlocale (LC_COLLATE, value) != 0);
148 # endif /* LC_COLLATE */
150 else if (var[3] == 'M' && var[4] == 'E') /* LC_MESSAGES */
152 # if defined (LC_MESSAGES)
153 if (lc_all == 0 || *lc_all == '\0')
154 return (setlocale (LC_MESSAGES, value) != 0);
155 # endif /* LC_MESSAGES */
157 #endif /* HAVE_SETLOCALE */
162 /* Called when LANG is assigned a value. Sets LC_ALL if that has not
165 set_lang (var, value)
168 return ((lc_all == 0) ? set_locale_var ("LC_ALL", value) : 0);
171 /* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE) */
181 locale = get_string_value (var);
183 locale = default_locale;
188 /* Translate the contents of STRING, a $"..." quoted string, according
189 to the current locale. In the `C' or `POSIX' locale, or if gettext()
190 is not available, the passed string is returned unchanged. The
191 length of the translated string is returned in LENP, if non-null. */
193 localetrans (string, len, lenp)
198 #if defined (HAVE_GETTEXT)
203 /* Don't try to translate null strings. */
204 if (string == 0 || *string == 0)
208 return ((char *)NULL);
211 t = xmalloc (len + 1);
213 locale = get_locale_var ("LC_MESSAGES");
215 /* If we don't have setlocale() or the current locale is `C' or `POSIX',
216 just return the string. If we don't have gettext(), there's no use
217 doing anything else. */
218 #if defined (HAVE_GETTEXT)
219 if (locale == 0 || locale[0] == '\0' ||
220 (locale[0] == 'C' && locale[1] == '\0') || STREQ (locale, "POSIX"))
229 #if defined (HAVE_GETTEXT)
230 /* Now try to translate it. */
231 translated = gettext (string);
232 if (translated == string) /* gettext returns its argument if untranslatable */
241 tlen = strlen (translated);
242 t = xmalloc (tlen + 1);
243 strcpy (t, translated);
248 #endif /* HAVE_GETTEXT */