File: gettext.c

package info (click to toggle)
joe 3.7-2.4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,304 kB
  • ctags: 2,460
  • sloc: ansic: 34,071; sh: 3,426; makefile: 140
file content (141 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* JOE's gettext() library.  Why?  Once again we can not rely on the
 * system's or GNU's gettext being installed properly */

/* One modification from standard gettext: comments are allowed at
 * the start of strings: "|comment|comment|comment|foo".  The leading
 * '|' is required to indicate the comment.
 *
 * Comments can be used to make two otherwise identical strings distinct.
 */

#include "types.h"

HASH *gettext_ht;

unsigned char *ignore_prefix(unsigned char *set)
{
	unsigned char *s = zrchr(set, '|');
	if (s)
		++s;
	else
		s = set;
	return s;
}

unsigned char *my_gettext(unsigned char *s)
{
	if (gettext_ht) {
		unsigned char *r = htfind(gettext_ht, s);
		if (r)
			s = r;
	}
	if (s[0] == '|')
		return ignore_prefix(s);
	else
		return s;
}

/* Load a .po file, convert entries to local character set and add them to
 * hash table */

int load_po(FILE *f)
{
	unsigned char buf[1024];
	unsigned char msgid[1024];
	unsigned char msgstr[1024];
	unsigned char bf[8192];
	struct charmap *po_map = locale_map;
	int preload_flag = 0;
	msgid[0] = 0;
	msgstr[0] = 0;
	while (preload_flag || fgets((char *)buf,sizeof(buf)-1,f)) {
		unsigned char *p;
		preload_flag = 0;
		p = buf;
		parse_ws(&p, '#');
		if (!parse_field(&p, USTR "msgid")) {
			int ofst = 0;
			int len;
			msgid[0] = 0;
			parse_ws(&p, '#');
			while ((len = parse_string(&p, msgid + ofst, sizeof(msgid)-ofst)) >= 0) {
				preload_flag = 0;
				ofst += len;
				parse_ws(&p, '#');
				if (!*p) {
					if (fgets((char *)buf,sizeof(buf) - 1,f)) {
						p = buf;
						preload_flag = 1;
						parse_ws(&p, '#');
					} else {
						goto bye;
					}
				}
			}
		} else if (!parse_field(&p, USTR "msgstr")) {
			int ofst = 0;
			int len;
			msgstr[0] = 0;
			parse_ws(&p, '#');
			while ((len = parse_string(&p, msgstr + ofst, sizeof(msgstr)-ofst)) >= 0) {
				preload_flag = 0;
				ofst += len;
				parse_ws(&p, '#');
				if (!*p) {
					if (fgets((char *)buf,sizeof(buf) - 1,f)) {
						p = buf;
						preload_flag = 1;
						parse_ws(&p, '#');
					} else {
						break;
					}
				}
			}
			if (msgid[0] && msgstr[0]) {
				/* Convert to locale character map */
				my_iconv(bf,locale_map,msgstr,po_map);
				/* Add to hash table */
				htadd(gettext_ht, zdup(msgid), zdup(bf));
			} else if (!msgid[0] && msgstr[0]) {
				unsigned char *p = (unsigned char *)strstr((char *)msgstr, "charset=");
				if (p) {
					/* Copy character set name up to next delimiter */
					int x;
					p += sizeof("charset=") - 1;
					while (*p == ' ' || *p == '\t') ++p;
					for (x = 0; p[x] && p[x] !='\n' && p[x] != '\r' && p[x] != ' ' &&
					            p[x] != '\t' && p[x] != ';' && p[x] != ','; ++x)
					            msgid[x] = p[x];
					msgid[x] = 0;
					po_map = find_charmap(msgid);
					if (!po_map)
						po_map = locale_map;
				}
			}
		}
	}
	bye:
	fclose(f);
	return 0;
}

/* Initialize my_gettext().  Call after locale_map has been set. */

void init_gettext(unsigned char *s)
{
	FILE *f;
	unsigned char buf[1024];
	joe_snprintf_2(buf, sizeof(buf), "%slang/%s.po",JOEDATA,s);
	if ((f = fopen((char *)buf, "r"))) {
		/* Try specific language, like en_GB */
		gettext_ht = htmk(256);
		load_po(f);
	} else if (s[0] && s[1]) {
		/* Try generic language, like en */
		joe_snprintf_3(buf, sizeof(buf), "%slang/%c%c.po",JOEDATA,s[0],s[1]);
		if ((f = fopen((char *)buf, "r"))) {
			gettext_ht = htmk(256);
			load_po(f);
		}
	}
}