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