]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/mk-error-details-po.pl
Source Format Enforcement (#963)
[thirdparty/squid.git] / scripts / mk-error-details-po.pl
CommitLineData
02259ff8 1#!/usr/bin/perl -w
a151895d 2#
bf95c10a 3## Copyright (C) 1996-2022 The Squid Software Foundation and contributors
a151895d
AJ
4##
5## Squid software is distributed under GPLv2+ license and includes
6## contributions from numerous individuals and organizations.
7## Please see the COPYING and CONTRIBUTORS files for details.
8##
9
02259ff8
CT
10#
11# Author: Tsantilas Christos
12# (C) 2011 The Measurement Factory
47f28373
FC
13#
14# Usage:
02259ff8
CT
15# mk-error-details-po.pl error-details.txt
16#
17# This script read the error-details.txt error details template, and prints to the
8d78487d 18# std output the contents of a .PO file template for translation.
02259ff8
CT
19# The error-details.txt file consist of records like the following:
20#
21# name: X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
2f8abb64 22# detail: "SSL Certificate error: certificate issuer (CA) not known: %ssl_ca_name"
02259ff8
CT
23# descr: "Unable to get issuer certificate"
24#
25# The records separated with an empty line.
26# Comments starting with '#' supported.
27#
28
29use warnings;
30use strict;
31
32my $File;
33my $mode;
34
47f28373 35$File = shift @ARGV or
02259ff8
CT
36 die "Usage: \n ".$0." error-detail-file\n\n";
37
38open(IN, "<$File") or
39 die "Can not open file '$File': $!";
40
41my @PO_RECORDS = ();
42my $lineNumber=0;
43while(my $line = <IN>) {
44 $lineNumber++;
45
46 if ($line =~ /^\#.*/ ) {
47 next;
48 }
49 elsif ($line =~ /^\s*$/ ) {
50 next;
51 }
52 my($rec) = "";
53 my($lineOffset) = 0;
54 do {
55 $rec = $rec.$line;
56 $line = <IN>;
57 $lineOffset++;
47f28373 58 } while($line && $line !~ /^\s*$/);
02259ff8
CT
59
60 processRecord(\@PO_RECORDS, $rec, $lineNumber);
61 $lineNumber= $lineNumber + $lineOffset;
62}
63
64foreach my $poRec (@PO_RECORDS) {
65 print $poRec->{"comment"};
66 print "msgid ".$poRec->{"msgid"}."\n";
8d78487d
AJ
67 # Being a template msgstr is always empty awaiting translated texts.
68 print "msgstr \"\"\n\n";
02259ff8
CT
69}
70
71exit(0);
72
73
74sub processRecord
75{
76 my($RECS, $rec, $lnumber) = @_;
77 my(@lines) = split /\n/, $rec;
78 my($currentField) = "";
79 my(%currentRec);
80 my $offset = 0;
81 foreach my $l (@lines) {
82 if ($l =~ /^name:(.*)/) {
83 $currentRec{"name"} = trim($1);
84 $currentField = "name";
85 }
86 elsif ( $l =~ /^detail:(.*)/ ) {
87 $currentRec{"detail"} = toCstr(trim_quoted($1));
88 $currentField = "detail";
89 }
90 elsif ($l =~ /^descr:(.*)/) {
91 $currentRec{"descr"} = toCstr(trim_quoted($1));
92 $currentField = "descr";
93 }
94 elsif($l = ~ /^(\s+.*)/ && defined($currentRec{$currentField})) {
95 my($fmtl) = toCstr($1);
96 $currentRec{$currentField}= $currentRec{$currentField}."\\n".$fmtl;
97 }
98 }
99
100 my (%poRecDetail, %poRecDescr);
47f28373 101
02259ff8
CT
102 $poRecDetail{"comment"} = "#: $File+".$currentRec{"name"}.".detail:$lnumber\n";
103 $poRecDetail{"msgid"} = $currentRec{"detail"};
02259ff8
CT
104 merge(\@$RECS, \%poRecDetail);
105
106 $poRecDescr{"comment"} = "#: $File+".$currentRec{"name"}.".descr:$lnumber\n";
107 $poRecDescr{"msgid"} = $currentRec{"descr"};
02259ff8
CT
108 merge(\@$RECS, \%poRecDescr);
109}
110
111sub merge {
112 my ($RECS, $poRec) = @_;
113 foreach my $item (@$RECS) {
114 if ($poRec->{"msgid"} eq $item->{"msgid"}) {
115 $item->{"comment"} = $item->{"comment"}.$poRec->{"comment"};
116 return;
117 }
118 }
119 push @$RECS, $poRec;
120 return;
121}
122
123sub trim
124{
125 my $string = shift;
126 $string =~ s/^\s+//;
127 $string =~ s/\s+$//;
128 return $string;
129}
130
131sub trim_quoted
132{
133 my $string = shift;
134 $string =~ s/^\s+"/"/;
135 $string =~ s/"\s+$/"/;
136 return $string;
137}
138
139sub toCstr
140{
141 my $string = shift;
142 $string =~ s/\t/\\t/g;
143 return $string;
144}