]>
Commit | Line | Data |
---|---|---|
3f7b1143 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use Time::Local; | |
5 | use PVE::Tools; | |
6 | use Data::Dumper; | |
7 | use Locale::PO; | |
8 | use Getopt::Std; | |
9 | use Encode; | |
10 | ||
11 | my $options = {}; | |
12 | ||
13 | getopts('o:b:', $options) || | |
14 | die "unable to parse options\n"; | |
15 | ||
16 | my $dirs = [@ARGV]; | |
17 | ||
18 | die "no directory specified\n" if !scalar(@$dirs); | |
19 | ||
20 | foreach my $dir (@$dirs) { | |
21 | die "no such directory '$dir'\n" if ! -d $dir; | |
22 | } | |
23 | ||
24 | my $basehref = {}; | |
25 | if (my $base = $options->{b}) { | |
26 | my $aref = Locale::PO->load_file_asarray($base) || | |
27 | die "unable to load '$base'\n"; | |
28 | ||
29 | my $charset; | |
30 | my $hpo = $aref->[0] || die "no header"; | |
31 | my $header = $hpo->dequote($hpo->msgstr); | |
32 | if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) { | |
33 | $charset = $1; | |
34 | } else { | |
35 | die "unable to get charset\n" if !$charset; | |
36 | } | |
37 | ||
38 | foreach my $po (@$aref) { | |
39 | my $qmsgid = decode($charset, $po->msgid); | |
40 | my $msgid = $po->dequote($qmsgid); | |
41 | $basehref->{$msgid} = $po; | |
42 | } | |
43 | } | |
44 | ||
45 | my $sources = []; | |
46 | ||
47 | my $findcmd = [['find', @$dirs, '-name', '*.js'],['sort']]; | |
48 | PVE::Tools::run_command($findcmd, outfunc => sub { | |
49 | my $line = shift; | |
50 | print "F: $line\n"; | |
51 | push @$sources, $line; | |
52 | }); | |
53 | ||
54 | my $header = <<__EOD; | |
55 | Proxmox message catalog. | |
56 | Copyright (C) 2011-2017 Proxmox Server Solutions GmbH | |
57 | This file is distributed under the same license as the proxmox-mailgateway | |
58 | package. | |
59 | Proxmox Support Team <support\@proxmox.com>, 2017. | |
60 | __EOD | |
61 | ||
62 | my $ctime = scalar localtime; | |
63 | ||
64 | my $href = {}; | |
65 | my $po = new Locale::PO(-msgid=> '', | |
66 | -comment=> $header, | |
67 | -fuzzy=> 1, | |
68 | -msgstr=> | |
69 | "Project-Id-Version: proxmox-mailgateway 5.0\n" . | |
70 | "Report-Msgid-Bugs-To: <support\@proxmox.com>\n" . | |
71 | "POT-Creation-Date: $ctime\n" . | |
72 | "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n" . | |
73 | "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n" . | |
74 | "Language-Team: LANGUAGE <support\@proxmox.com>\n" . | |
75 | "MIME-Version: 1.0\n" . | |
76 | "Content-Type: text/plain; charset=UTF-8\n" . | |
77 | "Content-Transfer-Encoding: 8bit\n"); | |
78 | ||
79 | $href->{''} = $po; | |
80 | ||
81 | sub extract_msg { | |
82 | my ($filename, $linenr, $line) = @_; | |
83 | ||
84 | my $count = 0; | |
85 | ||
86 | while(1) { | |
87 | my $text; | |
88 | if ($line =~ m/\Wgettext\s*\((("((?:[^"\\]++|\\.)*+)")|('((?:[^'\\]++|\\.)*+)'))\)/g) { | |
89 | $text = $3 || $5; | |
90 | } | |
91 | ||
92 | last if !$text; | |
93 | ||
94 | if ($basehref->{$text}) { | |
95 | return; | |
96 | } | |
97 | ||
98 | $count++; | |
99 | ||
100 | my $ref = "$filename:$linenr"; | |
101 | ||
102 | if (my $po = $href->{$text}) { | |
103 | $po->reference($po->reference() . " $ref"); | |
104 | } else { | |
105 | my $po = new Locale::PO(-msgid=> $text, -reference=> $ref, -msgstr=> ''); | |
106 | $href->{$text} = $po; | |
107 | } | |
108 | }; | |
109 | ||
110 | die "can't extract gettext message in '$filename' line $linenr\n" | |
111 | if !$count; | |
112 | } | |
113 | ||
114 | foreach my $s (@$sources) { | |
115 | open(SRC, $s) || die "unable to open file '$s' - $!\n"; | |
116 | while(defined(my $line = <SRC>)) { | |
117 | next if $line =~ m/^function gettext/; | |
118 | if ($line =~ m/gettext\s*\(/) { | |
119 | extract_msg($s, $., $line); | |
120 | } | |
121 | } | |
122 | close(SRC); | |
123 | } | |
124 | ||
125 | my $filename = $options->{o} // "messages.pot"; | |
126 | Locale::PO->save_file_fromhash($filename, $href); | |
127 |