]> git.proxmox.com Git - proxmox-i18n.git/blame - po2js.pl
update traditional Chinese translations
[proxmox-i18n.git] / po2js.pl
CommitLineData
3f7b1143
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
9ce3f62e 5use Getopt::Std;
3f7b1143
DM
6use Locale::PO;
7use JSON;
8use Encode;
9
10# current limits:
11# - we do not support plural. forms
12# - no message content support
13
9ce3f62e
DM
14my $options = {};
15
c1ae7daa 16getopts('t:o:v:', $options) ||
9ce3f62e
DM
17 die "unable to parse options\n";
18
3f7b1143
DM
19die "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)
24sub 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 42my $catalog = {};
3f7b1143
DM
43
44foreach 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();
c1ae7daa
DC
62
63 # skip unused entries
64 next if !$ref;
65
66 # skip entries if t is defined (pve/pmg) and the string is
67 # not used there or in the widget toolkit
68 next if $options->{t} && $ref !~ m/($options->{t}|proxmox)\-/;
3f7b1143
DM
69
70 my $qmsgid = decode($charset, $po->msgid);
71 my $msgid = $po->dequote($qmsgid);
72
73 my $qmsgstr = decode($charset, $po->msgstr);
74 my $msgstr = $po->dequote($qmsgstr);
75
76 next if !length($msgid); # skip header
77
fe2d4dee 78 next if !length($msgstr); # skip untranslated entries
3f7b1143
DM
79
80 my $digest = fnv31a($msgid);
81
82 die "duplicate digest" if $catalog->{$digest};
83
84 $catalog->{$digest} = [ $msgstr ];
85 # later, we can add plural forms to the array
86 }
87}
88
89my $json = to_json($catalog, {canonical => 1, utf8 => 1});
90
c1ae7daa 91my $content = "// $options->{v}\n"; # write version to beginning
9ce3f62e
DM
92
93my $outfile = $options->{o};
94
95$content .= "// Proxmox Message Catalog: $outfile\n" if $outfile;
96
97$content .= <<__EOD;
4a189d3c 98__proxmox_i18n_msgcat__ = $json;
3f7b1143
DM
99
100function fnv31a(text) {
101 var len = text.length;
102 var hval = 0x811c9dc5;
103 for (var i = 0; i < len; i++) {
104 var c = text.charCodeAt(i);
105 hval ^= c;
106 hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
107 }
108 hval &= 0x7fffffff;
109 return hval;
110}
111
112function gettext(buf) {
113 var digest = fnv31a(buf);
4a189d3c 114 var data = __proxmox_i18n_msgcat__[digest];
3f7b1143
DM
115 if (!data) {
116 return buf;
117 }
118 return data[0] || buf;
119}
3f7b1143
DM
120__EOD
121
9ce3f62e
DM
122if ($outfile) {
123 open(my $fh, '>', $outfile) ||
124 die "unable to open '$outfile' - $!\n";
125 print $fh $content;
126} else {
127 print $content;
128}