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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "intl/lang_tdb.h"
24 #include "lib/util/util_paths.h"
26 static TDB_CONTEXT *tdb;
28 /* the currently selected language */
29 static char *current_lang;
32 /* load a msg file into the tdb */
33 static bool load_msg(const char *msg_file)
40 lines = file_lines_load(msg_file, &num_lines, 0, NULL);
46 if (tdb_lockall(tdb) != 0) {
56 for (i=0;i<num_lines;i++) {
57 if (strncmp(lines[i], "msgid \"", 7) == 0) {
60 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
61 msgstr = lines[i] + 8;
62 trim_char(msgid, '\0', '\"');
63 trim_char(msgstr, '\0', '\"');
67 all_string_sub(msgid, "\\n", "\n", 0);
68 all_string_sub(msgstr, "\\n", "\n", 0);
69 data = string_term_tdb_data(msgstr);
70 tdb_store_bystring(tdb, msgid, data, 0);
82 /* work out what language to use from locale variables */
83 static const char *get_lang(void)
85 const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
89 for (i=0; vars[i]; i++) {
90 if ((p = getenv(vars[i]))) {
98 /* initialise the message translation subsystem. If the "lang" argument
99 is NULL then get the language from the normal environment variables */
100 bool lang_tdb_init(const char *lang)
103 char *msg_path = NULL;
105 static int initialised;
111 /* we only want to init once per process, unless given
113 if (initialised && !lang)
117 /* we are re-initialising, free up any old init */
122 SAFE_FREE(current_lang);
128 /* no lang given, use environment */
132 /* if no lang then we don't translate */
136 dpath = data_path(talloc_tos(), (const char *)lang);
141 if (asprintf(&msg_path, "%s.msg", dpath) == -1) {
142 DEBUG(0, ("asprintf failed\n"));
145 if (stat(msg_path, &st) != 0) {
146 /* the msg file isn't available */
147 DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path,
152 lpath = lock_path(talloc_tos(), "lang_");
157 if (asprintf(&path, "%s%s.tdb", lpath, lang) == -1) {
158 DEBUG(0, ("asprintf failed\n"));
162 DEBUG(10, ("lang_tdb_init: loading %s\n", path));
164 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
166 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
168 DEBUG(10, ("lang_tdb_init: %s: %s\n", path,
172 current_lang = SMB_STRDUP(lang);
177 loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
179 if (loadtime == -1 || loadtime < st.st_mtime) {
181 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
184 current_lang = SMB_STRDUP(lang);
196 /* translate a msgid to a message string in the current language
197 returns a string that must be freed by calling lang_msg_free()
199 const char *lang_msg(const char *msgid)
203 char *q, *msgid_quoted;
208 if (!tdb) return msgid;
210 /* Due to the way quotes in msgids are escaped in the msg file we
211 must replace " with \" before doing a lookup in the tdb. */
215 for(p = msgid; *p; p++) {
220 if (!(msgid_quoted = (char *)SMB_MALLOC(strlen(msgid) + count + 1)))
223 /* string_sub() is unsuitable here as it replaces some punctuation
224 chars with underscores. */
226 for(p = msgid, q = msgid_quoted; *p; p++) {
237 data = tdb_fetch_bystring(tdb, msgid_quoted);
241 /* if the message isn't found then we still need to return a pointer
242 that can be freed. Pity. */
244 return SMB_STRDUP(msgid);
246 return (const char *)data.dptr;
250 /* free up a string from lang_msg() */
251 void lang_msg_free(const char *msgstr)
254 free(discard_const_p(void, msgstr));
258 return the current language - needed for language file mappings
260 char *lang_tdb_current(void)