r249: Improved the menu code so that translations work better.
[rox-filer/dt.git] / ROX-Filer / src / i18n.c
blob5acf0589acaf6c5042755c0ac01572329e90b1a2
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
29 #include <gtk/gtk.h>
31 #include "support.h"
32 #include "choices.h"
33 #include "options.h"
34 #include "i18n.h"
35 #include "gui_support.h"
38 /****************************************************************
39 * EXTERNAL INTERFACE *
40 ****************************************************************/
43 /* Set things up for internationalisation */
44 void i18n_init(void)
46 #ifdef HAVE_GETTEXT
47 setlocale(LC_ALL, "");
48 bindtextdomain("ROX-Filer", make_path(getenv("APP_DIR"), "po")->str);
49 textdomain("ROX-Filer");
50 #endif
54 /****************************************************************
55 * INTERNAL FUNCTIONS *
56 ****************************************************************/
59 /* These two stolen from dia :-).
60 * Slight modification though: '/%s/' means 'same as above' so that
61 * if a translation is missing it doesn't muck up the whole menu structure!
63 GtkItemFactoryEntry *translate_entries(GtkItemFactoryEntry *entries, gint n)
65 guchar *first = NULL, *second = NULL; /* Previous menu, submenu */
66 gint i;
67 GtkItemFactoryEntry *ret;
69 ret = g_malloc(sizeof(GtkItemFactoryEntry) * n);
70 for (i = 0; i < n; i++)
72 guchar *from = entries[i].path;
73 guchar *trans, *slash;
74 int indent;
76 if (from[0] == '>')
77 if (from[1] == '>')
78 indent = 2;
79 else
80 indent = 1;
81 else
82 indent = 0;
84 if (from[indent])
85 from = _(from + indent);
86 else
87 from = "";
89 if (indent == 0)
90 trans = g_strdup_printf("/%s", from);
91 else if (indent == 1)
92 trans = g_strdup_printf("/%s/%s", first, from);
93 else
94 trans = g_strdup_printf("/%s/%s/%s",
95 first, second, from);
97 ret[i].path = trans;
99 g_free(first);
100 g_free(second);
101 second = NULL;
103 trans++;
104 slash = strchr(trans, '/');
105 if (slash)
107 first = g_strndup(trans, slash - trans);
108 trans = slash + 1;
110 slash = strchr(trans, '/');
111 if (slash)
112 second = g_strndup(trans, slash - trans);
113 else
114 second = g_strdup(trans);
116 else
117 first = g_strdup(trans);
119 /* accelerator and item_type are not duped, only referenced */
120 ret[i].accelerator = entries[i].accelerator;
121 ret[i].callback = entries[i].callback;
122 ret[i].callback_action = entries[i].callback_action;
123 ret[i].item_type = entries[i].item_type;
126 g_free(first);
127 g_free(second);
129 return ret;
132 void free_translated_entries(GtkItemFactoryEntry *entries, gint n)
134 gint i;
136 for (i=0; i<n; i++)
137 g_free(entries[i].path);
138 g_free(entries);