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