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