r248: Moved i18n support into it's own files. Now supports setting the
[rox-filer.git] / ROX-Filer / src / i18n.c
blob9ec0c8f8b830cdcf2e692618b539a7c58bcad61f
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>
28 #include <gtk/gtk.h>
30 #include "support.h"
31 #include "choices.h"
32 #include "options.h"
33 #include "i18n.h"
34 #include "gui_support.h"
36 /* Static prototypes */
37 static GtkWidget *create_options();
38 static void update_options();
39 static void set_options();
40 static void save_options();
41 static char *parse_lang(guchar *line);
43 static OptionsSection options =
45 N_("Locale options"),
46 create_options,
47 update_options,
48 set_options,
49 save_options
52 static guchar *o_default_lang = NULL;
55 /****************************************************************
56 * EXTERNAL INTERFACE *
57 ****************************************************************/
60 /* Set things up for internationalisation */
61 void i18n_init(void)
63 o_default_lang = g_strdup("");
65 #ifdef HAVE_GETTEXT
67 guchar *lang_path;
69 lang_path = choices_find_path_load("Locale", "ROX-Filer");
70 if (lang_path)
71 parse_file(lang_path, parse_lang);
73 if (*o_default_lang)
74 setenv("LANG", o_default_lang, 1);
75 setlocale(LC_ALL, "");
76 bindtextdomain("ROX-Filer",
77 make_path(getenv("APP_DIR"), "po")->str);
78 textdomain("ROX-Filer");
80 #endif
81 options_sections = g_slist_prepend(options_sections, &options);
85 /****************************************************************
86 * INTERNAL FUNCTIONS *
87 ****************************************************************/
90 /* OPTIONS */
92 #ifdef HAVE_GETTEXT
93 static GtkWidget *w_lang_entry = NULL;
94 #endif
96 /* Build up some option widgets to go in the options dialog, but don't
97 * fill them in yet.
99 static GtkWidget *create_options()
101 #ifdef HAVE_GETTEXT
102 GtkWidget *vbox, *hbox, *label;
104 vbox = gtk_vbox_new(FALSE, 0);
105 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
107 hbox = gtk_hbox_new(TRUE, 0);
108 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
110 label = gtk_label_new("Language to use");
111 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 0);
113 w_lang_entry = gtk_entry_new();
114 gtk_box_pack_start(GTK_BOX(hbox), w_lang_entry, TRUE, TRUE, 0);
116 return vbox;
117 #else
118 label = gtk_label_new("Not compiled with gettext support");
119 return label;
120 #endif
123 /* Reflect current state by changing the widgets in the options box */
124 static void update_options()
126 #ifdef HAVE_GETTEXT
127 gtk_entry_set_text(GTK_ENTRY(w_lang_entry), o_default_lang);
128 #endif
131 /* Set current values by reading the states of the widgets in the options box */
132 static void set_options()
134 #ifdef HAVE_GETTEXT
135 g_free(o_default_lang);
137 o_default_lang = gtk_editable_get_chars(GTK_EDITABLE(w_lang_entry),
138 0, -1);
139 #endif
142 static void save_options()
144 guchar *lang_path;
146 lang_path = choices_find_path_save("Locale", "ROX-Filer", TRUE);
148 if (lang_path)
150 FILE *file;
151 int count;
153 file = fopen(lang_path, "wb");
154 if (!file)
156 delayed_error(PROJECT, g_strerror(errno));
157 return;
160 count = strlen(o_default_lang);
161 if (fwrite(o_default_lang, 1, count, file) < count)
163 delayed_error(PROJECT, g_strerror(errno));
164 fclose(file);
166 else if (fclose(file))
167 delayed_error(PROJECT, g_strerror(errno));
171 static char *parse_lang(guchar *line)
173 o_default_lang = g_strdup(line);
175 return NULL;
178 /* These two stolen from dia :-) */
179 GtkItemFactoryEntry *translate_entries(GtkItemFactoryEntry *entries, gint n)
181 gint i;
182 GtkItemFactoryEntry *ret;
184 ret = g_malloc(sizeof(GtkItemFactoryEntry) * n);
185 for (i = 0; i < n; i++)
187 /* Translation. Note the explicit use of gettext(). */
188 ret[i].path = g_strdup(gettext(entries[i].path));
189 /* accelerator and item_type are not duped, only referenced */
190 ret[i].accelerator = entries[i].accelerator;
191 ret[i].callback = entries[i].callback;
192 ret[i].callback_action = entries[i].callback_action;
193 ret[i].item_type = entries[i].item_type;
195 return ret;
198 void free_translated_entries(GtkItemFactoryEntry *entries, gint n)
200 gint i;
202 for (i=0; i<n; i++)
203 g_free(entries[i].path);
204 g_free(entries);