1 /* Some ELinks' auxiliary routines (ELinks<->gettext support) */
12 #include "intl/gettext/libintl.h"
13 #include "intl/gettext/gettextP.h"
14 #include "util/error.h"
15 #include "util/memory.h"
16 #include "util/string.h"
19 /* See libintl.h for comments. */
20 int current_charset
= -1;
22 /* This is a language lookup table. Indexed by code. */
23 /* Update this everytime you add a new translation. */
24 /* TODO: Try to autogenerate it somehow. Maybe just a complete table? Then we
25 * will anyway need a table of real translations. */
26 struct language languages
[] = {
27 {N_("System"), "system"},
28 {N_("English"), "en"},
30 {N_("Afrikaans"), "af"},
31 {N_("Belarusian"), "be"},
32 {N_("Brazilian Portuguese"), "pt-BR"},
33 {N_("Bulgarian"), "bg"},
34 {N_("Catalan"), "ca"},
35 {N_("Croatian"), "hr"},
39 {N_("Estonian"), "et"},
40 {N_("Finnish"), "fi"},
42 {N_("Galician"), "gl"},
45 {N_("Hungarian"), "hu"},
46 {N_("Icelandic"), "is"},
47 {N_("Indonesian"), "id"},
48 {N_("Italian"), "it"},
49 {N_("Lithuanian"), "lt"},
50 {N_("Norwegian"), "no"},
52 {N_("Portuguese"), "pt"},
53 {N_("Romanian"), "ro"},
54 {N_("Russian"), "ru"},
55 {N_("Serbian"), "sr"},
57 {N_("Spanish"), "es"},
58 {N_("Swedish"), "sv"},
59 {N_("Turkish"), "tr"},
60 {N_("Ukrainian"), "uk"},
65 /* XXX: In fact this is _NOT_ a real ISO639 code but RFC3066 code (as we're
66 * supposed to use that one when sending language tags through HTTP/1.1) and
67 * that one consists basically from ISO639[-ISO3166]. This is important for
69 /* TODO: We should reflect this in name of this function and of the tag. On the
70 * other side, it's ISO639 for gettext as well etc. So what? --pasky */
73 iso639_to_language(unsigned char *iso639
)
75 unsigned char *l
= stracpy(iso639
);
82 /* The environment variable transformation. */
96 for (i
= 0; languages
[i
].name
; i
++) {
97 if (strcmp(languages
[i
].iso639
, l
))
103 /* Base language match. */
107 for (i
= 0; languages
[i
].name
; i
++) {
108 if (strcmp(languages
[i
].iso639
, l
))
115 /* Any dialect match. */
118 for (i
= 0; languages
[i
].name
; i
++) {
119 int il
= strcspn(languages
[i
].iso639
, "-");
121 if (strncmp(languages
[i
].iso639
, l
, il
> ll
? ll
: il
))
127 /* Default to english. */
133 int system_language
= 0;
136 language_to_iso639(int language
)
138 /* Language is "system", we need to extract the index from
141 return system_language
?
142 languages
[system_language
].iso639
:
143 languages
[get_system_language_index()].iso639
;
146 return languages
[language
].iso639
;
150 name_to_language(const unsigned char *name
)
154 for (i
= 0; languages
[i
].name
; i
++) {
155 if (strcasecmp(languages
[i
].name
, name
))
163 language_to_name(int language
)
165 return languages
[language
].name
;
169 get_system_language_index(void)
173 /* At this point current_language must be "system" yet. */
174 l
= getenv("LANGUAGE");
176 l
= getenv("LC_ALL");
178 l
= getenv("LC_MESSAGES");
182 return (l
) ? iso639_to_language(l
) : 1;
185 int current_language
= 0;
188 set_language(int language
)
192 if (!system_language
)
193 system_language
= get_system_language_index();
195 if (language
== current_language
) {
200 current_language
= language
;
203 language
= system_language
;
206 /* We never free() this, purely intentionally. */
207 LANGUAGE
= malloc(256);
209 strcpy(LANGUAGE
, language_to_iso639(language
));
210 p
= strchr(LANGUAGE
, '-');
214 /* Propagate the change to gettext. From the info manual. */
216 extern int _nl_msg_cat_cntr
;