~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/intl/gettext/libintl.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Gervai
  • Date: 2004-01-21 22:13:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040121221345-ju33hai1yhhqt6kn
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Some ELinks' auxiliary routines (ELinks<->gettext support) */
 
2
/* $Id: libintl.c,v 1.14 2003/12/22 22:00:08 pasky Exp $ */
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
#include "config.h"
 
6
#endif
 
7
 
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
 
 
11
#include "elinks.h"
 
12
 
 
13
#include "intl/gettext/libintl.h"
 
14
#include "intl/gettext/gettextP.h"
 
15
#include "util/error.h"
 
16
#include "util/memory.h"
 
17
#include "util/string.h"
 
18
 
 
19
/* This is a language lookup table. Indexed by code. */
 
20
/* Update this everytime you add a new translation. */
 
21
/* TODO: Try to autogenerate it somehow. Maybe just a complete table? Then we
 
22
 * will anyway need a table of real translations. */
 
23
struct language languages[] = {
 
24
        {N_("System"), "system"},
 
25
        {N_("English"), "en"},
 
26
 
 
27
        {N_("Belarusian"), "be"},
 
28
        {N_("Brazilian Portuguese"), "pt-BR"},
 
29
        {N_("Bulgarian"), "bg"},
 
30
        {N_("Catalan"), "ca"},
 
31
        {N_("Croatian"), "hr"},
 
32
        {N_("Czech"), "cs"},
 
33
        {N_("Danish"), "da"},
 
34
        {N_("Dutch"), "nl"},
 
35
        {N_("Estonian"), "et"},
 
36
        {N_("Finnish"), "fi"},
 
37
        {N_("French"), "fr"},
 
38
        {N_("Galician"), "gl"},
 
39
        {N_("German"), "de"},
 
40
        {N_("Greek"), "el"},
 
41
        {N_("Hungarian"), "hu"},
 
42
        {N_("Icelandic"), "is"},
 
43
        {N_("Indonesian"), "id"},
 
44
        {N_("Italian"), "it"},
 
45
        {N_("Leet"), "leet"},
 
46
        {N_("Lithuanian"), "lt"},
 
47
        {N_("Norwegian"), "no"},
 
48
        {N_("Polish"), "pl"},
 
49
        {N_("Portuguese"), "pt"},
 
50
        {N_("Romanian"), "ro"},
 
51
        {N_("Russian"), "ru"},
 
52
        {N_("Slovak"), "sk"},
 
53
        {N_("Spanish"), "es"},
 
54
        {N_("Swedish"), "sv"},
 
55
        {N_("Turkish"), "tr"},
 
56
        {N_("Ukrainian"), "uk"},
 
57
 
 
58
        {NULL, NULL},
 
59
};
 
60
 
 
61
/* XXX: In fact this is _NOT_ a real ISO639 code but RFC3066 code (as we're
 
62
 * supposed to use that one when sending language tags through HTTP/1.1) and
 
63
 * that one consists basically from ISO639[-ISO3166].  This is important for
 
64
 * ie. pt vs pt-BR. */
 
65
/* TODO: We should reflect this in name of this function and of the tag. On the
 
66
 * other side, it's ISO639 for gettext as well etc. So what?  --pasky */
 
67
 
 
68
int
 
69
iso639_to_language(unsigned char *iso639)
 
70
{
 
71
        unsigned char *l = stracpy(iso639);
 
72
        unsigned char *p;
 
73
        int i, ll;
 
74
 
 
75
        if (!l)
 
76
                return 1;
 
77
 
 
78
        /* The environment variable transformation. */
 
79
 
 
80
        p = strchr(l, '.');
 
81
        if (p)
 
82
                *p = '\0';
 
83
 
 
84
        p = strchr(l, '_');
 
85
        if (p)
 
86
                *p = '-';
 
87
        else
 
88
                p = strchr(l, '-');
 
89
 
 
90
        /* Exact match. */
 
91
 
 
92
        for (i = 0; languages[i].name; i++) {
 
93
                if (strcmp(languages[i].iso639, l))
 
94
                        continue;
 
95
                mem_free(l);
 
96
                return i;
 
97
        }
 
98
 
 
99
        /* Base language match. */
 
100
 
 
101
        if (p) {
 
102
                *p = '\0';
 
103
                for (i = 0; languages[i].name; i++) {
 
104
                        if (strcmp(languages[i].iso639, l))
 
105
                                continue;
 
106
                        mem_free(l);
 
107
                        return i;
 
108
                }
 
109
        }
 
110
 
 
111
        /* Any dialect match. */
 
112
 
 
113
        ll = strlen(l);
 
114
        for (i = 0; languages[i].name; i++) {
 
115
                int il = strcspn(languages[i].iso639, "-");
 
116
 
 
117
                if (strncmp(languages[i].iso639, l, il > ll ? ll : il))
 
118
                        continue;
 
119
                mem_free(l);
 
120
                return i;
 
121
        }
 
122
 
 
123
        /* Default to english. */
 
124
 
 
125
        mem_free(l);
 
126
        return 1;
 
127
}
 
128
 
 
129
int system_language = 0;
 
130
 
 
131
unsigned char *
 
132
language_to_iso639(int language)
 
133
{
 
134
        /* Language is "system", we need to extract the index from
 
135
         * the environment */
 
136
        if (language == 0) {
 
137
                return system_language ?
 
138
                        languages[system_language].iso639 :
 
139
                        languages[get_system_language_index()].iso639;
 
140
        }
 
141
 
 
142
        return languages[language].iso639;
 
143
}
 
144
 
 
145
int
 
146
name_to_language(unsigned char *name)
 
147
{
 
148
        int i;
 
149
 
 
150
        for (i = 0; languages[i].name; i++) {
 
151
                if (strcasecmp(languages[i].name, name))
 
152
                        continue;
 
153
                return i;
 
154
        }
 
155
        return 1;
 
156
}
 
157
 
 
158
unsigned char *
 
159
language_to_name(int language)
 
160
{
 
161
        return languages[language].name;
 
162
}
 
163
 
 
164
int
 
165
get_system_language_index(void)
 
166
{
 
167
        unsigned char *l;
 
168
 
 
169
        /* At this point current_language must be "system" yet. */
 
170
        l = getenv("LANGUAGE");
 
171
        if (!l)
 
172
                l = getenv("LC_ALL");
 
173
        if (!l)
 
174
                l = getenv("LC_MESSAGES");
 
175
        if (!l)
 
176
                l = getenv("LANG");
 
177
 
 
178
        return (l) ? iso639_to_language(l) : 1;
 
179
}
 
180
 
 
181
int current_language = 0;
 
182
 
 
183
void
 
184
set_language(int language)
 
185
{
 
186
        unsigned char *p;
 
187
 
 
188
        if (!system_language)
 
189
                system_language = get_system_language_index();
 
190
 
 
191
        if (language == current_language) {
 
192
                /* Nothing to do. */
 
193
                return;
 
194
        }
 
195
 
 
196
        current_language = language;
 
197
 
 
198
        if (!language)
 
199
                language = system_language;
 
200
 
 
201
        if (!LANGUAGE) {
 
202
                /* We never free() this, purely intentionally. */
 
203
                LANGUAGE = malloc(256);
 
204
        }
 
205
        strcpy(LANGUAGE, language_to_iso639(language));
 
206
        p = strchr(LANGUAGE, '-');
 
207
        if (p)
 
208
                *p = '_';
 
209
 
 
210
        /* Propagate the change to gettext. From the info manual. */
 
211
        {
 
212
                extern int _nl_msg_cat_cntr;
 
213
 
 
214
                _nl_msg_cat_cntr++;
 
215
        }
 
216
}