2 Unix SMB/Netbios implementation.
4 tdb based replacement for gettext
5 Copyright (C) Andrew Tridgell 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static TDB_CONTEXT *tdb;
26 /* load a po file into the tdb */
27 static BOOL load_po(const char *po_file)
34 lines = file_lines_load(po_file, &num_lines);
40 if (tdb_lockall(tdb) != 0) return False;
43 tdb_traverse(tdb, (tdb_traverse_func) tdb_delete, NULL);
45 for (i=0;i<num_lines;i++) {
46 if (strncmp(lines[i], "msgid \"", 7) == 0) {
49 if (strncmp(lines[i], "msgstr \"", 8) == 0) {
50 msgstr = lines[i] + 8;
51 trim_string(msgid, NULL, "\"");
52 trim_string(msgstr, NULL, "\"");
57 key.dsize = strlen(msgid)+1;
59 data.dsize = strlen(msgstr)+1;
60 tdb_store(tdb, key, data, 0);
64 file_lines_free(lines);
71 /* work out what language to use from locale variables */
72 static char *get_lang(void)
74 char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
78 for (i=0; vars[i]; i++) {
79 if ((p = getenv(vars[i]))) {
87 /* initialise the message translation subsystem */
88 void lang_tdb_init(void)
93 static int initialised;
96 /* we only want to init once per process */
97 if (initialised) return;
102 /* if no lang then we don't translate */
105 asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
107 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
109 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
116 asprintf(&path, "%s.po", lock_path(lang));
118 loadtime = tdb_fetch_int(tdb, "/LOADTIME/");
120 if (stat(path, &st) == 0 && (loadtime == -1 || loadtime < st.st_mtime)) {
122 tdb_store_int(tdb, "/LOADTIME/", (int)time(NULL));
127 /* translate a msgid to a message string in the current language
128 returns a string that must be freed by calling lang_msg_free()
130 const char *lang_msg(const char *msgid)
136 if (!tdb) return msgid;
138 key.dptr = (char *)msgid;
139 key.dsize = strlen(msgid)+1;
141 data = tdb_fetch(tdb, key);
143 /* if the message isn't found then we still need to return a pointer
144 that can be freed. Pity. */
145 if (!data.dptr) return strdup(msgid);
147 return (const char *)data.dptr;
151 /* free up a string from lang_msg() */
152 void lang_msg_free(const char *msgstr)
155 free((void *)msgstr);
160 when the _() translation macro is used there is no obvious place to free
161 the resulting string and there is no easy way to give a static pointer.
162 All we can do is rotate between some static buffers and hope a single d_printf()
163 doesn't have more calls to _() than the number of buffers
165 const char *lang_msg_rotate(const char *msgid)
167 #define NUM_LANG_BUFS 4
169 static pstring bufs[NUM_LANG_BUFS];
172 msgstr = lang_msg(msgid);
173 if (!msgstr) return msgid;
175 pstrcpy(bufs[next], msgstr);
178 next = (next+1) % NUM_LANG_BUFS;