r4895: New MIME type for Jar archives.
[rox-filer.git] / ROX-Filer / src / rox_gettext.c
bloba68fcd5b27f5b794b3e15d21379afc3c0c4a886a
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* This is a reimplementation of the GNU gettext function, because not everyone
21 * has GNU gettext.
24 #include "config.h"
26 #include <string.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 const char *rox_gettext(const char *from)
53 char *retval;
55 if (!translate || !from || !*from)
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(const char *path)
77 guint32 magic;
78 char *data, *from_base, *to_base;
79 gsize size;
80 gboolean swap; /* TRUE => reverse byte-order of ints */
81 int n, n_total;
82 GError *error = NULL;
84 if (!g_file_get_contents(path, &data, &size, &error))
86 delayed_error("%s", error ? error->message : path);
87 g_error_free(error);
88 return;
91 if (size < 20)
93 delayed_error(_("Invalid .gmo translation file "
94 "(too short): %s"), path);
95 goto out;
98 magic = *((guint *) data);
100 if (magic == 0x950412de)
101 swap = FALSE;
102 else if (magic == 0xde120495)
103 swap = TRUE;
104 else
106 delayed_error(_("Invalid .gmo translation file "
107 "(GNU magic number not found): %s"),
108 path);
109 goto out;
112 if (WORD(data + 4) != 0)
113 g_warning("rox_add_translations: expected format revision 0");
115 if (!translate)
117 translate = g_hash_table_new_full(g_str_hash, g_str_equal,
118 g_free, g_free);
121 n_total = WORD(data + 8);
122 from_base = data + WORD(data + 12);
123 to_base = data + WORD(data + 16);
125 for (n = 0; n < n_total; n++)
127 char *from = data + WORD(from_base + (n << 3) + 4);
128 char *to = data + WORD(to_base + (n << 3) + 4);
130 g_hash_table_insert(translate, g_strdup(from), g_strdup(to));
133 out:
134 g_free(data);
137 /****************************************************************
138 * INTERNAL FUNCTIONS *
139 ****************************************************************/
141 static guint32 word(char *addr, gboolean swap)
143 guint32 val = *((guint32 *) addr);
145 if (swap)
146 return GUINT32_SWAP_LE_BE(val);
147 else
148 return val;