Switched completely to standard gettext.
[rox-filer.git] / ROX-Filer / src / i18n.c
blob1fe6f56045abf60047fc296f1d2d30d76b8beb8f
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 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #ifdef HAVE_LIBINTL_H
27 #include <libintl.h>
28 #endif
30 #include "global.h"
32 #include "support.h"
33 #include "choices.h"
34 #include "options.h"
35 #include "i18n.h"
36 #include "gui_support.h"
37 #include "main.h"
39 #define MESSAGE _("Note that you must save your choices " \
40 "and restart the filer for the new language " \
41 "setting to take full effect.")
43 /* Two/five-char country_territory code, or NULL */
44 char *current_lang = NULL;
46 /****************************************************************
47 * EXTERNAL INTERFACE *
48 ****************************************************************/
51 /* Set things up for internationalisation */
52 void i18n_init(void)
54 const char *lang;
56 gtk_set_locale();
58 #ifdef HAVE_LIBINTL_H
59 gchar *path = g_strdup_printf("%s/Messages", app_dir);
60 bindtextdomain("ROX-Filer", path);
61 g_free(path);
62 #endif
64 /* This is a hang-over from when we did translations ourselves.
65 * Maybe we can get this info from the gettext library?
67 lang = getenv("LANG");
68 if (lang)
70 const char *end;
72 /* Extract the language code from the locale name.
73 * language[_territory][.codeset][@modifier]
76 end = strchr(lang, '.');
77 if (!end)
78 end = strchr(lang, '@');
79 if (end)
80 current_lang = g_strndup(lang, end - lang);
81 else
82 current_lang = g_strdup(lang);
86 /* These two stolen from dia :-).
87 * Slight modification though: '>' means 'same as above' so that
88 * if a translation is missing it doesn't muck up the whole menu structure!
90 GtkItemFactoryEntry *translate_entries(GtkItemFactoryEntry *entries, gint n)
92 guchar *first = NULL, *second = NULL; /* Previous menu, submenu */
93 gint i;
94 GtkItemFactoryEntry *ret;
96 ret = g_malloc(sizeof(GtkItemFactoryEntry) * n);
97 for (i = 0; i < n; i++)
99 const gchar *from = entries[i].path;
100 gchar *trans, *slash;
101 int indent;
103 if (from[0] == '>')
105 if (from[1] == '>')
106 indent = 2;
107 else
108 indent = 1;
110 else
111 indent = 0;
113 if (from[indent])
114 from = _(from + indent);
115 else
116 from = "";
118 if (indent == 0)
119 trans = g_strdup_printf("/%s", from);
120 else if (indent == 1)
121 trans = g_strdup_printf("/%s/%s", first, from);
122 else
123 trans = g_strdup_printf("/%s/%s/%s",
124 first, second, from);
126 ret[i].path = trans;
128 g_free(first);
129 null_g_free(&second);
131 trans++;
132 slash = strchr(trans, '/');
133 if (slash)
135 first = g_strndup(trans, slash - trans);
136 trans = slash + 1;
138 slash = strchr(trans, '/');
139 if (slash)
140 second = g_strndup(trans, slash - trans);
141 else
142 second = g_strdup(trans);
144 else
145 first = g_strdup(trans);
147 /* accelerator and item_type are not duped, only referenced */
148 ret[i].accelerator = entries[i].accelerator;
149 ret[i].callback = entries[i].callback;
150 ret[i].callback_action = entries[i].callback_action;
151 ret[i].item_type = entries[i].item_type;
152 ret[i].extra_data = entries[i].extra_data;
155 g_free(first);
156 g_free(second);
158 return ret;
161 void free_translated_entries(GtkItemFactoryEntry *entries, gint n)
163 gint i;
165 for (i=0; i<n; i++)
166 g_free(entries[i].path);
167 g_free(entries);