2 Unix SMB/CIFS implementation.
3 tdb based replacement for gettext
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static TDB_CONTEXT *tdb;
25 /* the currently selected language */
26 static char *current_lang;
29 /* load a msg file into the tdb */
30 static BOOL load_msg(const char *msg_file)
37 lines = file_lines_load(msg_file, &num_lines);
43 if (tdb_lockall(tdb) != 0) return False;
46 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
50 for (i=0;i<num_lines;i++) {
51 if (strncmp(lines[i], "msgid \"", 7) == 0) {
54 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
55 msgstr = lines[i] + 8;
56 trim_string(msgid, NULL, "\"");
57 trim_string(msgstr, NULL, "\"");
62 key.dsize = strlen(msgid)+1;
64 data.dsize = strlen(msgstr)+1;
65 tdb_store(tdb, key, data, 0);
70 file_lines_free(lines);
77 /* work out what language to use from locale variables */
78 static char *get_lang(void)
80 char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
84 for (i=0; vars[i]; i++) {
85 if ((p = getenv(vars[i]))) {
93 /* initialise the message translation subsystem. If the "lang" argument
94 is NULL then get the language from the normal environment variables */
95 BOOL lang_tdb_init(const char *lang)
98 char *msg_path = NULL;
100 static int initialised;
103 /* we only want to init once per process, unless given
105 if (initialised && !lang) return True;
108 /* we are re-initialising, free up any old init */
113 SAFE_FREE(current_lang);
119 /* no lang given, use environment */
123 /* if no lang then we don't translate */
124 if (!lang) return True;
126 asprintf(&msg_path, "%s.msg", lib_path((char *)lang));
127 if (stat(msg_path, &st) != 0) {
128 /* the msg file isn't available */
134 asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
136 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
138 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
141 if (!tdb) return False;
142 current_lang = strdup(lang);
148 loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
150 if (loadtime == -1 || loadtime < st.st_mtime) {
152 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
156 current_lang = strdup(lang);
161 /* translate a msgid to a message string in the current language
162 returns a string that must be freed by calling lang_msg_free()
164 const char *lang_msg(const char *msgid)
170 if (!tdb) return msgid;
172 key.dptr = (char *)msgid;
173 key.dsize = strlen(msgid)+1;
175 data = tdb_fetch(tdb, key);
177 /* if the message isn't found then we still need to return a pointer
178 that can be freed. Pity. */
179 if (!data.dptr) return strdup(msgid);
181 return (const char *)data.dptr;
185 /* free up a string from lang_msg() */
186 void lang_msg_free(const char *msgstr)
189 free((void *)msgstr);
194 when the _() translation macro is used there is no obvious place to free
195 the resulting string and there is no easy way to give a static pointer.
196 All we can do is rotate between some static buffers and hope a single d_printf()
197 doesn't have more calls to _() than the number of buffers
199 const char *lang_msg_rotate(const char *msgid)
201 #define NUM_LANG_BUFS 4
203 static pstring bufs[NUM_LANG_BUFS];
206 msgstr = (char *)lang_msg(msgid);
207 if (!msgstr) return msgid;
209 pstrcpy(bufs[next], msgstr);
212 next = (next+1) % NUM_LANG_BUFS;
219 return the current language - needed for language file mappings
221 char *lang_tdb_current(void)