]>
Commit | Line | Data |
---|---|---|
3f7b1143 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
9ce3f62e | 5 | use Getopt::Std; |
3f7b1143 DM |
6 | use Locale::PO; |
7 | use JSON; | |
8 | use Encode; | |
9 | ||
10 | # current limits: | |
11 | # - we do not support plural. forms | |
12 | # - no message content support | |
13 | ||
9ce3f62e DM |
14 | my $options = {}; |
15 | ||
16 | getopts('o:', $options) || | |
17 | die "unable to parse options\n"; | |
18 | ||
3f7b1143 DM |
19 | die "no files specified\n" if !scalar(@ARGV); |
20 | ||
21 | #my $filename = shift || die "no po file specified\n"; | |
22 | ||
23 | # like FNV32a, but we only return 31 bits (positive numbers) | |
24 | sub fnv31a { | |
25 | my ($string) = @_; | |
26 | ||
27 | my $hval = 0x811c9dc5; | |
28 | ||
29 | foreach my $c (unpack('C*', $string)) { | |
30 | $hval ^= $c; | |
31 | $hval += ( | |
32 | (($hval << 1) ) + | |
33 | (($hval << 4) ) + | |
34 | (($hval << 7) ) + | |
35 | (($hval << 8) ) + | |
36 | (($hval << 24) ) ); | |
37 | $hval = $hval & 0xffffffff; | |
38 | } | |
39 | return $hval & 0x7fffffff; | |
40 | } | |
41 | ||
fe2d4dee | 42 | my $catalog = {}; |
3f7b1143 DM |
43 | |
44 | foreach my $filename (@ARGV) { | |
45 | my $href = Locale::PO->load_file_ashash($filename) || | |
46 | die "unable to load '$filename'\n"; | |
47 | ||
48 | my $charset; | |
49 | my $hpo = $href->{'""'} || die "no header"; | |
50 | my $header = $hpo->dequote($hpo->msgstr); | |
51 | if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) { | |
52 | $charset = $1; | |
53 | } else { | |
54 | die "unable to get charset\n" if !$charset; | |
55 | } | |
56 | ||
57 | ||
58 | foreach my $k (keys %$href) { | |
59 | my $po = $href->{$k}; | |
60 | next if $po->fuzzy(); # skip fuzzy entries | |
61 | my $ref = $po->reference(); | |
62 | ||
63 | my $qmsgid = decode($charset, $po->msgid); | |
64 | my $msgid = $po->dequote($qmsgid); | |
65 | ||
66 | my $qmsgstr = decode($charset, $po->msgstr); | |
67 | my $msgstr = $po->dequote($qmsgstr); | |
68 | ||
69 | next if !length($msgid); # skip header | |
70 | ||
fe2d4dee | 71 | next if !length($msgstr); # skip untranslated entries |
3f7b1143 DM |
72 | |
73 | my $digest = fnv31a($msgid); | |
74 | ||
75 | die "duplicate digest" if $catalog->{$digest}; | |
76 | ||
77 | $catalog->{$digest} = [ $msgstr ]; | |
78 | # later, we can add plural forms to the array | |
79 | } | |
80 | } | |
81 | ||
82 | my $json = to_json($catalog, {canonical => 1, utf8 => 1}); | |
83 | ||
9ce3f62e DM |
84 | my $content = ''; |
85 | ||
86 | my $outfile = $options->{o}; | |
87 | ||
88 | $content .= "// Proxmox Message Catalog: $outfile\n" if $outfile; | |
89 | ||
90 | $content .= <<__EOD; | |
91 | Proxmox = { i18n_msgcat: $json } | |
3f7b1143 DM |
92 | |
93 | function fnv31a(text) { | |
94 | var len = text.length; | |
95 | var hval = 0x811c9dc5; | |
96 | for (var i = 0; i < len; i++) { | |
97 | var c = text.charCodeAt(i); | |
98 | hval ^= c; | |
99 | hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); | |
100 | } | |
101 | hval &= 0x7fffffff; | |
102 | return hval; | |
103 | } | |
104 | ||
105 | function gettext(buf) { | |
106 | var digest = fnv31a(buf); | |
9ce3f62e | 107 | var data = Proxmox.i18n_msgcat[digest]; |
3f7b1143 DM |
108 | if (!data) { |
109 | return buf; | |
110 | } | |
111 | return data[0] || buf; | |
112 | } | |
3f7b1143 DM |
113 | __EOD |
114 | ||
9ce3f62e DM |
115 | if ($outfile) { |
116 | open(my $fh, '>', $outfile) || | |
117 | die "unable to open '$outfile' - $!\n"; | |
118 | print $fh $content; | |
119 | } else { | |
120 | print $content; | |
121 | } |