]> git.proxmox.com Git - proxmox-i18n.git/blame - jsgettext.pl
bump version to 3.2.2
[proxmox-i18n.git] / jsgettext.pl
CommitLineData
3f7b1143
DM
1#!/usr/bin/perl
2
3use strict;
e6fbfe8b
TL
4use warnings;
5
6use Encode;
3bee6f0e 7use Getopt::Long;
e6fbfe8b 8use Locale::PO;
3f7b1143 9use Time::Local;
e6fbfe8b 10
3f7b1143 11my $options = {};
3bee6f0e 12GetOptions($options, 'o=s', 'b=s', 'p=s') or die "unable to parse options\n";
3f7b1143
DM
13
14my $dirs = [@ARGV];
15
16die "no directory specified\n" if !scalar(@$dirs);
17
18foreach my $dir (@$dirs) {
19 die "no such directory '$dir'\n" if ! -d $dir;
20}
21
1bddddef
DM
22my $projectId = $options->{p} || die "missing project ID\n";
23
3f7b1143
DM
24my $basehref = {};
25if (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
2bcbc733
TL
45sub 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);
16298bef 61
2bcbc733
TL
62 return $sources;
63}
3f7b1143 64
6ad9f0ce 65my $header = <<'__EOD';
3f7b1143 66Proxmox message catalog.
641582bd
TL
67
68Copyright (C) Proxmox Server Solutions GmbH
69
6ad9f0ce 70This file is free software: you can redistribute it and/or modify it under the terms of the GNU
641582bd
TL
71Affero General Public License as published by the Free Software Foundation, either version 3 of the
72License, or (at your option) any later version.
73-- Proxmox Support Team <support\@proxmox.com>
3f7b1143
DM
74__EOD
75
76my $ctime = scalar localtime;
77
6ad9f0ce
TL
78my $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};
3f7b1143
DM
94
95sub 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 }
3f7b1143 105 last if !$text;
6ad9f0ce 106 return if $basehref->{$text};
3f7b1143
DM
107 $count++;
108
109 my $ref = "$filename:$linenr";
110
111 if (my $po = $href->{$text}) {
112 $po->reference($po->reference() . " $ref");
d310270d 113 } else {
6ad9f0ce 114 $href->{$text} = Locale::PO->new(-msgid=> $text, -reference=> $ref, -msgstr=> '');
3f7b1143 115 }
6ad9f0ce
TL
116 }
117 die "can't extract gettext message in '$filename' line $linenr\n" if !$count;
118 return;
3f7b1143
DM
119}
120
2bcbc733
TL
121my $sources = find_js_sources($dirs);
122
3f7b1143 123foreach my $s (@$sources) {
d310270d
TL
124 open(my $SRC_FH, '<', $s) || die "unable to open file '$s' - $!\n";
125 while(defined(my $line = <$SRC_FH>)) {
052bd210 126 if ($line =~ m/gettext\s*\(/ && $line !~ m/^\s*function gettext/) {
3f7b1143
DM
127 extract_msg($s, $., $line);
128 }
129 }
d310270d 130 close($SRC_FH);
3f7b1143
DM
131}
132
133my $filename = $options->{o} // "messages.pot";
134Locale::PO->save_file_fromhash($filename, $href);
135