Changed sregex().
[mp-5.x.git] / tools / po2mpsl.mpsl
blob6064e43b3578e3bad222b13ac2423dd15078558d
1 /*
3         Parses .po files and dumps MPSL code.
5         Angel Ortega <angel@triptico.com>
7 */
9 sub convert_po_line(line)
11         line = sregex(line, '/^\s*"/');
12         line = sregex(line, '/"$/');
14         line = split(line);
16         line = join(
17         map(
18             line,
19             sub (e) {
20                 (ord(e) > 127) &&
21                 sprintf("\\x{%04x}", ord(e)) ||
22                 e;
23             }
24         )
25         );
27         return line;
31 sub parse_po_file(pofile)
33         local f;
34         local r = [];
36         /* only UTF-8 po files are supported */
37         encoding('UTF-8');
39         if ((f = open(pofile, "r")) != NULL) {
41                 local l;
42                 local i = 0;
43                 local v = [];
45                 while ((l = read(f)) != NULL) {
47                         l = mp.chomp(l);
49                         local s = regex([ '/^(msgid|msgstr)*/', '/\s*\".*\"$/' ], l);
50                         s[1] = convert_po_line(s[1]);
52                         if (s[0] eq 'msgid') {
53                                 i = 0;
54                                 push(v, [[], []]);
55                         }
56                         else
57                         if (s[0] eq 'msgstr') {
58                                 i = 1;
59                         }
61                         if (s[1]) {
62                                 push(v[-1][i], s[1]);
63                         }
64                 }
66                 push(r, "/* Built by po2mpsl - Don't modify, change the .po file instead */");
67                 push(r, "__I18N__ = {");
69                 foreach (l, v) {
70                         if (size(l[1]))
71                                 push(r, "\t\"" ~ join(l[0]) ~ "\" => \"" ~ join(l[1]) ~ "\",");
72                 }
74                 push(r, "\t\"-\" => \"-\"\n};\n");
76                 close(f);
77         }
79         /* back to default encoding */
80         encoding();
82         return join(r, "\n");
85 /* main */
87 foreach (f, glob('po/*.po')) { /**/
88         local output = parse_po_file(f);
90         /* strip extension and path */
91         f = shift(split(f, '.po'));
92         f = pop(split(f, '/'));
94         local o = open('lang/' ~ f ~ '.mpsl', 'w');
95         write(o, output);
96         close(o);
99 mp.exit();