extract-localizable-strings.pl [plain text]
use strict;
use File::Compare;
use File::Copy;
use FindBin;
use Getopt::Long;
use lib $FindBin::Bin;
use LocalizableStrings;
no warnings 'deprecated';
my %isDebugMacro = ( ASSERT_WITH_MESSAGE => 1, LOG_ERROR => 1, ERROR => 1, NSURL_ERROR => 1, FATAL => 1, LOG => 1, LOG_WARNING => 1, UI_STRING_LOCALIZE_LATER => 1, UI_STRING_LOCALIZE_LATER_KEY => 1, LPCTSTR_UI_STRING_LOCALIZE_LATER => 1, UNLOCALIZED_STRING => 1, UNLOCALIZED_LPCTSTR => 1, dprintf => 1, NSException => 1, NSLog => 1, printf => 1 );
my $verify;
my $exceptionsFile;
my @directoriesToSkip = ();
my $treatWarningsAsErrors;
my %options = (
'verify' => \$verify,
'exceptions=s' => \$exceptionsFile,
'skip=s' => \@directoriesToSkip,
'treat-warnings-as-errors' => \$treatWarningsAsErrors,
);
GetOptions(%options);
setTreatWarningsAsErrors($treatWarningsAsErrors);
@ARGV >= 2 or die "Usage: extract-localizable-strings [--verify] [--treat-warnings-as-errors] [--exceptions <exceptions file>] <file to update> [--skip directory | directory]...\nDid you mean to run update-webkit-localizable-strings instead?\n";
-f $exceptionsFile or die "Couldn't find exceptions file $exceptionsFile\n" unless !defined $exceptionsFile;
my $fileToUpdate = shift @ARGV;
-f $fileToUpdate or die "Couldn't find file to update $fileToUpdate\n";
my $warnAboutUnlocalizedStrings = defined $exceptionsFile;
my @directories = ();
if (@ARGV < 1) {
push(@directories, ".");
} else {
for my $dir (@ARGV) {
push @directories, $dir;
}
}
my $notLocalizedCount = 0;
my $NSLocalizeCount = 0;
my %exception;
my %usedException;
if (defined $exceptionsFile && open EXCEPTIONS, $exceptionsFile) {
while (<EXCEPTIONS>) {
chomp;
if (/^"([^\\"]|\\.)*"$/ or /^[-_\/\w\s.]+.(h|m|mm|c|cpp)$/ or /^[-_\/\w\s.]+.(h|m|mm|c|cpp):"([^\\"]|\\.)*"$/) {
if ($exception{$_}) {
emitWarning($exceptionsFile, $., "exception for $_ appears twice");
emitWarning($exceptionsFile, $exception{$_}, "first appearance");
} else {
$exception{$_} = $.;
}
} else {
emitWarning($exceptionsFile, $., "syntax error");
}
}
close EXCEPTIONS;
}
my $quotedDirectoriesString = '"' . join('" "', @directories) . '"';
for my $dir (@directoriesToSkip) {
$quotedDirectoriesString .= ' -path "' . $dir . '" -prune -o';
}
my @files = ( split "\n", `find $quotedDirectoriesString \\( -name "*.h" -o -name "*.m" -o -name "*.mm" -o -name "*.c" -o -name "*.cpp" \\)` );
for my $file (sort @files) {
next if $file =~ /\/\w+LocalizableStrings\w*\.h$/ || $file =~ /\/LocalizedStrings\.h$/;
$file =~ s-^./--;
open SOURCE, $file or die "can't open $file\n";
my $inComment = 0;
my $expected = "";
my $macroLine;
my $macro;
my $UIString;
my $key;
my $comment;
my $mnemonic;
my $string;
my $stringLine;
my $nestingLevel;
my $previousToken = "";
while (<SOURCE>) {
chomp;
# Handle continued multi-line comment.
if ($inComment) {
next unless s-.*\*/--;
$inComment = 0;
}
next unless defined $nestingLevel or /(\"|\/\*)/;
# Handle all the tokens in the line.
while (s-^\s*([#\w]+|/\*|//|[^#\w/'"()\[\],]+|.)--) {
my $token = $1;
if ($token eq "@" and $expected and $expected eq "a quoted string") {
next;
}
if ($token eq "\"") {
if ($expected and $expected ne "a quoted string") {
emitError($file, $., "found a quoted string but expected $expected");
$expected = "";
}
if (s-^(([^\\$token]|\\.)*?)$token--) {
if (!defined $string) {
$stringLine = $.;
$string = $1;
} else {
$string .= $1;
}
} else {
emitError($file, $., "mismatched quotes");
$_ = "";
}
next;
}
if (defined $string) {
handleString:
if ($expected) {
if (!defined $UIString) {
# FIXME: Validate UTF-8 here?
$UIString = $string;
$expected = ",";
} elsif (($macro =~ /(WEB_)?UI_STRING_KEY(_INTERNAL)?$/) and !defined $key) {
# FIXME: Validate UTF-8 here?
$key = $string;
$expected = ",";
} elsif (($macro =~ /WEB_UI_STRING_WITH_MNEMONIC$/) and !defined $mnemonic) {
$mnemonic = $string;
$expected = ",";
} elsif (!defined $comment) {
# FIXME: Validate UTF-8 here?
$comment = $string;
$expected = ")";
}
} else {
if (defined $nestingLevel) {
# In a debug macro, no need to localize.
} elsif ($previousToken eq " } elsif ($previousToken eq "extern" and $string eq "C") {
} elsif ($string eq "") {
} elsif ($exception{$file}) {
$usedException{$file} = 1;
} elsif ($exception{"\"$string\""}) {
$usedException{"\"$string\""} = 1;
} elsif ($exception{"$file:\"$string\""}) {
$usedException{"$file:\"$string\""} = 1;
} else {
emitWarning($file, $stringLine, "\"$string\" is not marked for localization") if $warnAboutUnlocalizedStrings;
$notLocalizedCount++;
}
}
$string = undef;
last if !defined $token;
}
$previousToken = $token;
if ($token =~ /^NSLocalized/ && $token !~ /NSLocalizedDescriptionKey/ && $token !~ /NSLocalizedStringFromTableInBundle/ && $token !~ /NSLocalizedFileSizeDescription/ && $token !~ /NSLocalizedDescriptionKey/ && $token !~ /NSLocalizedRecoverySuggestionErrorKey/) {
emitError($file, $., "found a use of an NSLocalized macro ($token); not supported");
$nestingLevel = 0 if !defined $nestingLevel;
$NSLocalizeCount++;
} elsif ($token eq "/*") {
if (!s-^.*?\*/--) {
$_ = ""; $inComment = 1;
}
} elsif ($token eq "//") {
$_ = ""; } elsif ($token eq "'") {
if (!s-([^\\]|\\.)'--) { #' <-- that single quote makes the Project Builder editor less confused
emitError($file, $., "mismatched single quote");
$_ = "";
}
} else {
if ($expected and $expected ne $token) {
emitError($file, $., "found $token but expected $expected");
$expected = "";
}
if (($token =~ /(WEB_)?UI_STRING(_KEY)?(_INTERNAL)?$/) || ($token =~ /WEB_UI_NSSTRING$/) || ($token =~ /WEB_UI_STRING_WITH_MNEMONIC$/) || ($token =~ /WEB_UI_CFSTRING$/)) {
$expected = "(";
$macro = $token;
$UIString = undef;
$key = undef;
$comment = undef;
$mnemonic = undef;
$macroLine = $.;
} elsif ($token eq "(" or $token eq "[") {
++$nestingLevel if defined $nestingLevel;
$expected = "a quoted string" if $expected;
} elsif ($token eq ",") {
$expected = "a quoted string" if $expected;
} elsif ($token eq ")" or $token eq "]") {
$nestingLevel = undef if defined $nestingLevel && !--$nestingLevel;
if ($expected) {
$key = $UIString if !defined $key;
HandleUIString($UIString, $key, $comment, $file, $macroLine);
$macro = "";
$expected = "";
}
} elsif ($isDebugMacro{$token}) {
$nestingLevel = 0 if !defined $nestingLevel;
}
}
}
}
goto handleString if defined $string;
if ($expected) {
emitError($file, 0, "reached end of file but expected $expected");
}
close SOURCE;
}
print "\n" if sawError() || $notLocalizedCount || $NSLocalizeCount;
my @unusedExceptions = sort grep { !$usedException{$_} } keys %exception;
if (@unusedExceptions) {
for my $unused (@unusedExceptions) {
emitWarning($exceptionsFile, $exception{$unused}, "exception $unused not used");
}
print "\n";
}
print localizedCount() . " localizable strings\n" if localizedCount();
print keyCollisionCount() . " key collisions\n" if keyCollisionCount();
print "$notLocalizedCount strings not marked for localization\n" if $notLocalizedCount;
print "$NSLocalizeCount uses of NSLocalize\n" if $NSLocalizeCount;
print scalar(@unusedExceptions), " unused exceptions\n" if @unusedExceptions;
if (sawError()) {
print "\nErrors encountered. Exiting without writing to $fileToUpdate.\n";
exit 1;
}
if (-e "$fileToUpdate") {
if (!$verify) {
my $temporaryFile = "$fileToUpdate.updated";
writeStringsFile($temporaryFile);
if (compare($temporaryFile, $fileToUpdate)) {
move($temporaryFile, $fileToUpdate);
} else {
unlink $temporaryFile;
}
} else {
verifyStringsFile($fileToUpdate);
}
} else {
print "error: $fileToUpdate does not exist\n";
exit 1;
}