Skip to content

Instantly share code, notes, and snippets.

@RoCry
Last active December 16, 2015 22:39
Show Gist options
  • Save RoCry/5508469 to your computer and use it in GitHub Desktop.
Save RoCry/5508469 to your computer and use it in GitHub Desktop.
Scripts for Cocoa/CocoaTouch
#!/usr/bin/perl -w
# Usage:
# find_nonlocalized [<directory> ...]
#
# Scans .m and .mm files for potentially nonlocalized
# strings that should be.
# Lines marked with DNL (Do Not Localize) are ignored.
# String constant assignments of this form are ignored if
# they have no spaces in the value:
# NSString * const <...> = @"...";
# Strings on the same line as NSLocalizedString are
# ignored.
# Certain common methods that take nonlocalized strings are
# ignored
# URLs are ignored
#
# Exits with 1 if there were strings found
use File::Basename;
use File::Find;
use strict;
# Include the basenames of any files to ignore
my @EXCLUDE_FILENAMES = qw();
# Regular expressions to ignore
my @EXCLUDE_REGEXES = (
qr/\bDNL\b/,
qr/NSLocalizedString/,
qr/NSString\s*\*\s*const\s[^@]*@"[^ ]*";/,
qr/NSLog\(/,
qr/@"http/, qr/@"mailto/, qr/@"ldap/,
qr/predicateWithFormat:@"/,
qr/Key(?:[pP]ath)?:@"/,
qr/setDateFormat:@"/,
qr/NSAssert/,
qr/imageNamed:@"/,
qr/NibNamed?:@"/,
qr/pathForResource:@"/,
qr/fileURLWithPath:@"/,
qr/fontWithName:@"/,
qr/stringByAppendingPathComponent:@"/,
);
my $FoundNonLocalized = 0;
sub find_nonlocalized {
return unless $File::Find::name =~ /\.mm?$/;
return if grep($_, @EXCLUDE_FILENAMES);
open(FILE, $_);
LINE:
while (<FILE>) {
if (/@"[^"]*[a-z]{3,}/) {
foreach my $regex (@EXCLUDE_REGEXES) {
next LINE if $_ =~ $regex;
}
print "$File::Find::name:$.:$_";
$FoundNonLocalized = 1;
}
}
close(FILE);
}
my @dirs = scalar @ARGV ? @ARGV : (".");
find(\&find_nonlocalized, @dirs);
exit $FoundNonLocalized ? 1 : 0;
@fly2never
Copy link

我在AppCode中用正则表达式筛选出所有中文硬编码字符串,然后一个一个改就行了.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment