r333: In the path entry minibuffer, an exact match is favoured over any other
[rox-filer.git] / ROX-Filer / src / i18n.c
blobc17d605df453842df7dc2633efcb4c2d83c24eb5
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>
28 #ifdef HAVE_LOCALE_H
29 # include <locale.h>
30 #endif
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include <gtk/gtk.h>
36 #include "support.h"
37 #include "choices.h"
38 #include "options.h"
39 #include "i18n.h"
40 #include "gui_support.h"
41 #include "main.h"
44 /****************************************************************
45 * EXTERNAL INTERFACE *
46 ****************************************************************/
49 /* Set things up for internationalisation */
50 void i18n_init(void)
52 guchar *lang;
54 lang = getenv("LANG");
56 #ifdef HAVE_LOCALE_H
57 setlocale(LC_ALL, "");
58 #endif
60 if (lang)
62 struct stat info;
63 guchar *path;
65 path = g_strdup_printf("%s/Messages/%s.gmo", app_dir, lang);
66 if (stat(path, &info) == 0)
67 rox_add_translations(path);
68 g_free(path);
73 /****************************************************************
74 * INTERNAL FUNCTIONS *
75 ****************************************************************/
78 /* These two stolen from dia :-).
79 * Slight modification though: '/%s/' means 'same as above' so that
80 * if a translation is missing it doesn't muck up the whole menu structure!
82 GtkItemFactoryEntry *translate_entries(GtkItemFactoryEntry *entries, gint n)
84 guchar *first = NULL, *second = NULL; /* Previous menu, submenu */
85 gint i;
86 GtkItemFactoryEntry *ret;
88 ret = g_malloc(sizeof(GtkItemFactoryEntry) * n);
89 for (i = 0; i < n; i++)
91 guchar *from = entries[i].path;
92 guchar *trans, *slash;
93 int indent;
95 if (from[0] == '>')
97 if (from[1] == '>')
98 indent = 2;
99 else
100 indent = 1;
102 else
103 indent = 0;
105 if (from[indent])
106 from = _(from + indent);
107 else
108 from = "";
110 if (indent == 0)
111 trans = g_strdup_printf("/%s", from);
112 else if (indent == 1)
113 trans = g_strdup_printf("/%s/%s", first, from);
114 else
115 trans = g_strdup_printf("/%s/%s/%s",
116 first, second, from);
118 ret[i].path = trans;
120 g_free(first);
121 g_free(second);
122 second = NULL;
124 trans++;
125 slash = strchr(trans, '/');
126 if (slash)
128 first = g_strndup(trans, slash - trans);
129 trans = slash + 1;
131 slash = strchr(trans, '/');
132 if (slash)
133 second = g_strndup(trans, slash - trans);
134 else
135 second = g_strdup(trans);
137 else
138 first = g_strdup(trans);
140 /* accelerator and item_type are not duped, only referenced */
141 ret[i].accelerator = entries[i].accelerator;
142 ret[i].callback = entries[i].callback;
143 ret[i].callback_action = entries[i].callback_action;
144 ret[i].item_type = entries[i].item_type;
147 g_free(first);
148 g_free(second);
150 return ret;
153 void free_translated_entries(GtkItemFactoryEntry *entries, gint n)
155 gint i;
157 for (i=0; i<n; i++)
158 g_free(entries[i].path);
159 g_free(entries);