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,0);
43 if (tdb_lockall(tdb) != 0) {
44 file_lines_free(lines);
49 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
53 for (i=0;i<num_lines;i++) {
54 if (strncmp(lines[i], "msgid \"", 7) == 0) {
57 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
58 msgstr = lines[i] + 8;
59 trim_char(msgid, '\0', '\"');
60 trim_char(msgstr, '\0', '\"');
64 all_string_sub(msgid, "\\n", "\n", 0);
65 all_string_sub(msgstr, "\\n", "\n", 0);
66 data = string_term_tdb_data(msgstr);
67 tdb_store_bystring(tdb, msgid, data, 0);
72 file_lines_free(lines);
79 /* work out what language to use from locale variables */
80 static const char *get_lang(void)
82 const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
86 for (i=0; vars[i]; i++) {
87 if ((p = getenv(vars[i]))) {
95 /* initialise the message translation subsystem. If the "lang" argument
96 is NULL then get the language from the normal environment variables */
97 BOOL lang_tdb_init(const char *lang)
100 char *msg_path = NULL;
102 static int initialised;
106 /* we only want to init once per process, unless given
108 if (initialised && !lang)
112 /* we are re-initialising, free up any old init */
117 SAFE_FREE(current_lang);
123 /* no lang given, use environment */
127 /* if no lang then we don't translate */
131 asprintf(&msg_path, "%s.msg", lib_path((const char *)lang));
132 if (stat(msg_path, &st) != 0) {
133 /* the msg file isn't available */
134 DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path,
139 asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
141 DEBUG(10, ("lang_tdb_init: loading %s\n", path));
143 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
145 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
147 DEBUG(10, ("lang_tdb_init: %s: %s\n", path,
151 current_lang = SMB_STRDUP(lang);
156 loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
158 if (loadtime == -1 || loadtime < st.st_mtime) {
160 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
163 current_lang = SMB_STRDUP(lang);
173 /* translate a msgid to a message string in the current language
174 returns a string that must be freed by calling lang_msg_free()
176 const char *lang_msg(const char *msgid)
180 char *q, *msgid_quoted;
185 if (!tdb) return msgid;
187 /* Due to the way quotes in msgids are escaped in the msg file we
188 must replace " with \" before doing a lookup in the tdb. */
192 for(p = msgid; *p; p++) {
197 if (!(msgid_quoted = (char *)SMB_MALLOC(strlen(msgid) + count + 1)))
200 /* string_sub() is unsuitable here as it replaces some punctuation
201 chars with underscores. */
203 for(p = msgid, q = msgid_quoted; *p; p++) {
214 data = tdb_fetch_bystring(tdb, msgid_quoted);
218 /* if the message isn't found then we still need to return a pointer
219 that can be freed. Pity. */
221 return SMB_STRDUP(msgid);
223 return (const char *)data.dptr;
227 /* free up a string from lang_msg() */
228 void lang_msg_free(const char *msgstr)
231 free((void *)msgstr);
235 return the current language - needed for language file mappings
237 char *lang_tdb_current(void)