]>
Commit | Line | Data |
---|---|---|
3f7b1143 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
e6fbfe8b TL |
4 | use warnings; |
5 | ||
6 | use Encode; | |
7 | use Getopt::Std; | |
8 | use Locale::PO; | |
3f7b1143 | 9 | use Time::Local; |
e6fbfe8b | 10 | |
3f7b1143 DM |
11 | my $options = {}; |
12 | ||
1bddddef | 13 | getopts('o:b:p:', $options) || |
3f7b1143 DM |
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 | ||
1bddddef DM |
24 | my $projectId = $options->{p} || die "missing project ID\n"; |
25 | ||
3f7b1143 DM |
26 | my $basehref = {}; |
27 | if (my $base = $options->{b}) { | |
28 | my $aref = Locale::PO->load_file_asarray($base) || | |
29 | die "unable to load '$base'\n"; | |
30 | ||
31 | my $charset; | |
32 | my $hpo = $aref->[0] || die "no header"; | |
33 | my $header = $hpo->dequote($hpo->msgstr); | |
34 | if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) { | |
35 | $charset = $1; | |
36 | } else { | |
37 | die "unable to get charset\n" if !$charset; | |
38 | } | |
39 | ||
40 | foreach my $po (@$aref) { | |
41 | my $qmsgid = decode($charset, $po->msgid); | |
42 | my $msgid = $po->dequote($qmsgid); | |
43 | $basehref->{$msgid} = $po; | |
44 | } | |
45 | } | |
46 | ||
47 | my $sources = []; | |
48 | ||
16298bef TL |
49 | my $find_cmd = 'find '; |
50 | # shell quote heuristic, with the (here safe) assumption that the dirs don't contain single-quotes | |
51 | $find_cmd .= join(' ', map { "'$_'" } $dirs->@*); | |
52 | $find_cmd .= ' -name "*.js"'; | |
53 | open(my $find_cmd_output, '-|', "$find_cmd | sort") or die "Failed to execute command: $!"; | |
54 | ||
55 | # Iterate through the sorted output line by line | |
56 | while (my $line = <$find_cmd_output>) { | |
57 | chomp $line; | |
3f7b1143 DM |
58 | print "F: $line\n"; |
59 | push @$sources, $line; | |
16298bef TL |
60 | } |
61 | close($find_cmd_output); | |
62 | ||
3f7b1143 DM |
63 | |
64 | my $header = <<__EOD; | |
65 | Proxmox message catalog. | |
641582bd TL |
66 | |
67 | Copyright (C) Proxmox Server Solutions GmbH | |
68 | ||
69 | This file is free software: you can redistribute it and\/or modify it under the terms of the GNU | |
70 | Affero General Public License as published by the Free Software Foundation, either version 3 of the | |
71 | License, or (at your option) any later version. | |
72 | -- Proxmox Support Team <support\@proxmox.com> | |
3f7b1143 DM |
73 | __EOD |
74 | ||
75 | my $ctime = scalar localtime; | |
76 | ||
77 | my $href = {}; | |
d310270d TL |
78 | my $po = Locale::PO->new( |
79 | -msgid => '', | |
80 | -comment => $header, | |
81 | -fuzzy => 1, | |
82 | -msgstr => "Project-Id-Version: $projectId\n" | |
83 | ."Report-Msgid-Bugs-To: <support\@proxmox.com>\n" | |
84 | ."POT-Creation-Date: $ctime\n" | |
85 | ."PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n" | |
86 | ."Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n" | |
87 | ."Language-Team: LANGUAGE <support\@proxmox.com>\n" | |
88 | ."MIME-Version: 1.0\n" | |
89 | ."Content-Type: text/plain; charset=UTF-8\n" | |
90 | ."Content-Transfer-Encoding: 8bit\n", | |
91 | ); | |
3f7b1143 DM |
92 | |
93 | $href->{''} = $po; | |
94 | ||
95 | sub extract_msg { | |
96 | my ($filename, $linenr, $line) = @_; | |
97 | ||
98 | my $count = 0; | |
99 | ||
100 | while(1) { | |
101 | my $text; | |
78e560ba | 102 | if ($line =~ m/\bgettext\s*\((("((?:[^"\\]++|\\.)*+)")|('((?:[^'\\]++|\\.)*+)'))\)/g) { |
3f7b1143 DM |
103 | $text = $3 || $5; |
104 | } | |
105 | ||
106 | last if !$text; | |
107 | ||
108 | if ($basehref->{$text}) { | |
109 | return; | |
110 | } | |
111 | ||
112 | $count++; | |
113 | ||
114 | my $ref = "$filename:$linenr"; | |
115 | ||
116 | if (my $po = $href->{$text}) { | |
117 | $po->reference($po->reference() . " $ref"); | |
d310270d TL |
118 | } else { |
119 | my $po = Locale::PO->new(-msgid=> $text, -reference=> $ref, -msgstr=> ''); | |
3f7b1143 DM |
120 | $href->{$text} = $po; |
121 | } | |
122 | }; | |
123 | ||
124 | die "can't extract gettext message in '$filename' line $linenr\n" | |
125 | if !$count; | |
126 | } | |
127 | ||
128 | foreach my $s (@$sources) { | |
d310270d TL |
129 | open(my $SRC_FH, '<', $s) || die "unable to open file '$s' - $!\n"; |
130 | while(defined(my $line = <$SRC_FH>)) { | |
052bd210 | 131 | if ($line =~ m/gettext\s*\(/ && $line !~ m/^\s*function gettext/) { |
3f7b1143 DM |
132 | extract_msg($s, $., $line); |
133 | } | |
134 | } | |
d310270d | 135 | close($SRC_FH); |
3f7b1143 DM |
136 | } |
137 | ||
138 | my $filename = $options->{o} // "messages.pot"; | |
139 | Locale::PO->save_file_fromhash($filename, $href); | |
140 |