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