r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / rox_gettext.c
blob0852d192e9e33145723885e152ac43e26f04f5e3
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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 <string.h>
30 #include "global.h"
32 #include "gui_support.h"
34 static GHashTable *translate = NULL;
36 /* Returns the 32 bit number at 'addr'.
37 * Reverse the byte order of 'number' iff 'swap' is TRUE.
39 #define WORD(addr) word(addr, swap)
41 /* Static prototypes */
42 static guint32 word(char *addr, gboolean swap);
45 /****************************************************************
46 * EXTERNAL INTERFACE *
47 ****************************************************************/
49 /* Returns a pointer to a static, nul-terminated, string which
50 * is the translation of 'from' (or 'from' itself if there is
51 * no translation).
53 const char *rox_gettext(const char *from)
55 char *retval;
57 if (!translate || !from || !*from)
58 return from;
60 retval = g_hash_table_lookup(translate, from);
62 return retval ? retval : from;
65 void rox_clear_translation(void)
67 if (translate)
69 g_hash_table_destroy(translate);
70 translate = NULL;
74 /* Read in this .gmo format file; all translations found override
75 * any existing translations for future calls to rox_gettext().
77 void rox_add_translations(const char *path)
79 guint32 magic;
80 char *data, *from_base, *to_base;
81 gsize size;
82 gboolean swap; /* TRUE => reverse byte-order of ints */
83 int n, n_total;
84 GError *error = NULL;
86 if (!g_file_get_contents(path, &data, &size, &error))
88 delayed_error("%s", error ? error->message : path);
89 g_error_free(error);
90 return;
93 if (size < 20)
95 delayed_error(_("Invalid .gmo translation file "
96 "(too short): %s"), path);
97 goto out;
100 magic = *((guint *) data);
102 if (magic == 0x950412de)
103 swap = FALSE;
104 else if (magic == 0xde120495)
105 swap = TRUE;
106 else
108 delayed_error(_("Invalid .gmo translation file "
109 "(GNU magic number not found): %s"),
110 path);
111 goto out;
114 if (WORD(data + 4) != 0)
115 g_warning("rox_add_translations: expected format revision 0");
117 if (!translate)
119 translate = g_hash_table_new_full(g_str_hash, g_str_equal,
120 g_free, g_free);
123 n_total = WORD(data + 8);
124 from_base = data + WORD(data + 12);
125 to_base = data + WORD(data + 16);
127 for (n = 0; n < n_total; n++)
129 char *from = data + WORD(from_base + (n << 3) + 4);
130 char *to = data + WORD(to_base + (n << 3) + 4);
132 g_hash_table_insert(translate, g_strdup(from), g_strdup(to));
135 out:
136 g_free(data);
139 /****************************************************************
140 * INTERNAL FUNCTIONS *
141 ****************************************************************/
143 static guint32 word(char *addr, gboolean swap)
145 guint32 val = *((guint32 *) addr);
147 if (swap)
148 return GUINT32_SWAP_LE_BE(val);
149 else
150 return val;