1 | # Copyright (C) 2006, 2007, 2009, 2010, 2013, 2015 Apple Inc. All rights reserved.
|
---|
2 | #
|
---|
3 | # Redistribution and use in source and binary forms, with or without
|
---|
4 | # modification, are permitted provided that the following conditions
|
---|
5 | # are met:
|
---|
6 | # 1. Redistributions of source code must retain the above copyright
|
---|
7 | # notice, this list of conditions and the following disclaimer.
|
---|
8 | # 2. Redistributions in binary form must reproduce the above copyright
|
---|
9 | # notice, this list of conditions and the following disclaimer in the
|
---|
10 | # documentation and/or other materials provided with the distribution.
|
---|
11 | #
|
---|
12 | # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
13 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
14 | # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
15 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
16 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
17 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
18 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
19 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
20 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
21 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
22 | # THE POSSIBILITY OF SUCH DAMAGE.
|
---|
23 |
|
---|
24 | use strict;
|
---|
25 |
|
---|
26 | my $treatWarningsAsErrors = 0;
|
---|
27 |
|
---|
28 | sub setTreatWarningsAsErrors($)
|
---|
29 | {
|
---|
30 | ($treatWarningsAsErrors) = @_;
|
---|
31 | }
|
---|
32 |
|
---|
33 | my $sawError = 0;
|
---|
34 |
|
---|
35 | sub sawError()
|
---|
36 | {
|
---|
37 | return $sawError;
|
---|
38 | }
|
---|
39 |
|
---|
40 | sub emitError($$$)
|
---|
41 | {
|
---|
42 | my ($file, $line, $message) = @_;
|
---|
43 | print "$file:$line: $message\n";
|
---|
44 | $sawError = 1;
|
---|
45 | }
|
---|
46 |
|
---|
47 | sub emitWarning($$$)
|
---|
48 | {
|
---|
49 | my ($file, $line, $message) = @_;
|
---|
50 | my $prefix = $treatWarningsAsErrors ? "" : "warning: ";
|
---|
51 | print "$file:$line: $prefix$message\n";
|
---|
52 | $sawError = 1 if $treatWarningsAsErrors;
|
---|
53 | }
|
---|
54 |
|
---|
55 | # Unescapes C language hexadecimal escape sequences.
|
---|
56 | sub unescapeHexSequence($)
|
---|
57 | {
|
---|
58 | my ($originalStr) = @_;
|
---|
59 |
|
---|
60 | my $escapedStr = $originalStr;
|
---|
61 | my $unescapedStr = "";
|
---|
62 |
|
---|
63 | for (;;) {
|
---|
64 | if ($escapedStr =~ s-^\\x([[:xdigit:]]+)--) {
|
---|
65 | if (256 <= hex($1)) {
|
---|
66 | print "Hexadecimal escape sequence out of range: \\x$1\n";
|
---|
67 | return undef;
|
---|
68 | }
|
---|
69 | $unescapedStr .= pack("H*", $1);
|
---|
70 | } elsif ($escapedStr =~ s-^(.)--) {
|
---|
71 | $unescapedStr .= $1;
|
---|
72 | } else {
|
---|
73 | return $unescapedStr;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | my $keyCollisionCount = 0;
|
---|
79 |
|
---|
80 | sub keyCollisionCount()
|
---|
81 | {
|
---|
82 | return $keyCollisionCount;
|
---|
83 | }
|
---|
84 |
|
---|
85 | my $localizedCount = 0;
|
---|
86 |
|
---|
87 | sub localizedCount()
|
---|
88 | {
|
---|
89 | return $localizedCount;
|
---|
90 | }
|
---|
91 |
|
---|
92 | my %stringByKey;
|
---|
93 | my %commentByKey;
|
---|
94 | my %fileByKey;
|
---|
95 | my %lineByKey;
|
---|
96 |
|
---|
97 | sub HandleUIString
|
---|
98 | {
|
---|
99 | my ($string, $key, $comment, $file, $line) = @_;
|
---|
100 |
|
---|
101 | $localizedCount++;
|
---|
102 |
|
---|
103 | my $bad = 0;
|
---|
104 | $string = unescapeHexSequence($string);
|
---|
105 | if (!defined($string)) {
|
---|
106 | print "$file:$line: string has an illegal hexadecimal escape sequence\n";
|
---|
107 | $bad = 1;
|
---|
108 | }
|
---|
109 | $key = unescapeHexSequence($key);
|
---|
110 | if (!defined($key)) {
|
---|
111 | print "$file:$line: key has an illegal hexadecimal escape sequence\n";
|
---|
112 | $bad = 1;
|
---|
113 | }
|
---|
114 | $comment = unescapeHexSequence($comment);
|
---|
115 | if (!defined($comment)) {
|
---|
116 | print "$file:$line: comment has an illegal hexadecimal escape sequence\n";
|
---|
117 | $bad = 1;
|
---|
118 | }
|
---|
119 | if (grep { $_ == 0xFFFD } unpack "U*", $string) {
|
---|
120 | print "$file:$line: string for translation has illegal UTF-8 -- most likely a problem with the Text Encoding of the source file\n";
|
---|
121 | $bad = 1;
|
---|
122 | }
|
---|
123 | if ($string ne $key && grep { $_ == 0xFFFD } unpack "U*", $key) {
|
---|
124 | print "$file:$line: key has illegal UTF-8 -- most likely a problem with the Text Encoding of the source file\n";
|
---|
125 | $bad = 1;
|
---|
126 | }
|
---|
127 | if (grep { $_ == 0xFFFD } unpack "U*", $comment) {
|
---|
128 | print "$file:$line: comment for translation has illegal UTF-8 -- most likely a problem with the Text Encoding of the source file\n";
|
---|
129 | $bad = 1;
|
---|
130 | }
|
---|
131 | if ($bad) {
|
---|
132 | $sawError = 1;
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if ($stringByKey{$key} && $stringByKey{$key} ne $string) {
|
---|
137 | emitWarning($file, $line, "encountered the same key, \"$key\", twice, with different strings");
|
---|
138 | emitWarning($fileByKey{$key}, $lineByKey{$key}, "previous occurrence");
|
---|
139 | $keyCollisionCount++;
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | if ($commentByKey{$key} && $commentByKey{$key} ne $comment) {
|
---|
143 | emitWarning($file, $line, "encountered the same key, \"$key\", twice, with different comments");
|
---|
144 | emitWarning($fileByKey{$key}, $lineByKey{$key}, "previous occurrence");
|
---|
145 | $keyCollisionCount++;
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | $fileByKey{$key} = $file;
|
---|
150 | $lineByKey{$key} = $line;
|
---|
151 | $stringByKey{$key} = $string;
|
---|
152 | $commentByKey{$key} = $comment;
|
---|
153 | }
|
---|
154 |
|
---|
155 | sub writeStringsFile($)
|
---|
156 | {
|
---|
157 | my ($file) = @_;
|
---|
158 |
|
---|
159 | my $localizedStrings = "";
|
---|
160 |
|
---|
161 | for my $key (sort keys %commentByKey) {
|
---|
162 | $localizedStrings .= "/* $commentByKey{$key} */\n\"$key\" = \"$stringByKey{$key}\";\n\n";
|
---|
163 | }
|
---|
164 |
|
---|
165 | # Write out the strings file as UTF-8
|
---|
166 | open STRINGS, ">", $file or die;
|
---|
167 | print STRINGS $localizedStrings;
|
---|
168 | close STRINGS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | sub verifyStringsFile($)
|
---|
172 | {
|
---|
173 | my ($file) = @_;
|
---|
174 |
|
---|
175 | open STRINGS, $file or die;
|
---|
176 |
|
---|
177 | my $lastComment;
|
---|
178 | my $line;
|
---|
179 | my $sawError;
|
---|
180 |
|
---|
181 | while (<STRINGS>) {
|
---|
182 | chomp;
|
---|
183 |
|
---|
184 | next if (/^\s*$/);
|
---|
185 |
|
---|
186 | if (/^\/\* (.*) \*\/$/) {
|
---|
187 | $lastComment = $1;
|
---|
188 | } elsif (/^"((?:[^\\]|\\[^"])*)"\s*=\s*"((?:[^\\]|\\[^"])*)";$/) #
|
---|
189 | {
|
---|
190 | my $string = delete $stringByKey{$1};
|
---|
191 | if (!defined $string) {
|
---|
192 | print "$file:$.: unused key \"$1\"\n";
|
---|
193 | $sawError = 1;
|
---|
194 | } else {
|
---|
195 | if (!($string eq $2)) {
|
---|
196 | print "$file:$.: unexpected value \"$2\" for key \"$1\"\n";
|
---|
197 | print "$fileByKey{$1}:$lineByKey{$1}: expected value \"$string\" defined here\n";
|
---|
198 | $sawError = 1;
|
---|
199 | }
|
---|
200 | if (!($lastComment eq $commentByKey{$1})) {
|
---|
201 | print "$file:$.: unexpected comment /* $lastComment */ for key \"$1\"\n";
|
---|
202 | print "$fileByKey{$1}:$lineByKey{$1}: expected comment /* $commentByKey{$1} */ defined here\n";
|
---|
203 | $sawError = 1;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | } else {
|
---|
207 | print "$file:$.: line with unexpected format: $_\n";
|
---|
208 | $sawError = 1;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | for my $missing (keys %stringByKey) {
|
---|
213 | print "$fileByKey{$missing}:$lineByKey{$missing}: missing key \"$missing\"\n";
|
---|
214 | $sawError = 1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | if ($sawError) {
|
---|
218 | print "\n$file:0: file is not up to date.\n";
|
---|
219 | exit 1;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | 1;
|
---|