r898: Applied Bernard Jungen's latest patch:
[rox-filer.git] / ROX-Filer / src / rox_gettext.c
blobea5f835156d94f2858d8b4120272920cd5d597d1
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* This is a reimplementation of the GNU gettext function, because not everyone
23 * has GNU gettext.
26 #include "config.h"
28 #include "global.h"
30 #include "gui_support.h"
32 static GHashTable *translate = NULL;
34 /* Returns the 32 bit number at 'addr'.
35 * Reverse the byte order of 'number' iff 'swap' is TRUE.
37 #define WORD(addr) word(addr, swap)
39 /* Static prototypes */
40 static guint32 word(char *addr, gboolean swap);
43 /****************************************************************
44 * EXTERNAL INTERFACE *
45 ****************************************************************/
47 /* Returns a pointer to a static, nul-terminated, string which
48 * is the translation of 'from' (or 'from' itself if there is
49 * no translation).
51 char *rox_gettext(char *from)
53 char *retval;
55 if (!translate)
56 return from;
58 retval = g_hash_table_lookup(translate, from);
60 return retval ? retval : from;
63 void rox_clear_translation(void)
65 if (translate)
67 g_hash_table_destroy(translate);
68 translate = NULL;
72 /* Read in this .gmo format file; all translations found override
73 * any existing translations for future calls to rox_gettext().
75 void rox_add_translations(char *path)
77 guint32 magic;
78 char *data, *from_base, *to_base;
79 long size;
80 gboolean swap; /* TRUE => reverse byte-order of ints */
81 int n, n_total;
83 if (load_file(path, &data, &size) == FALSE)
84 return;
86 if (size < 20)
88 delayed_rox_error(_("Invalid .gmo translation file "
89 "(too short): %s"), path);
90 return;
93 magic = *((guint *) data);
95 if (magic == 0x950412de)
96 swap = FALSE;
97 else if (magic == 0xde120495)
98 swap = TRUE;
99 else
101 delayed_rox_error(_("Invalid .gmo translation file "
102 "(GNU magic number not found): %s"),
103 path);
104 return;
107 if (WORD(data + 4) != 0)
108 g_warning("rox_add_translations: expected format revision 0");
110 if (!translate)
111 translate = g_hash_table_new(g_str_hash, g_str_equal);
113 n_total = WORD(data + 8);
114 from_base = data + WORD(data + 12);
115 to_base = data + WORD(data + 16);
117 for (n = 0; n < n_total; n++)
119 char *from = data + WORD(from_base + (n << 3) + 4);
120 char *to = data + WORD(to_base + (n << 3) + 4);
122 g_hash_table_insert(translate, from, to);
125 /* Note: Do not free the file data! */
128 /****************************************************************
129 * INTERNAL FUNCTIONS *
130 ****************************************************************/
132 static guint32 word(char *addr, gboolean swap)
134 guint32 val = *((guint32 *) addr);
136 if (swap)
137 return GUINT32_SWAP_LE_BE(val);
138 else
139 return val;