00001 #!/usr/bin/perl -w 00002 #Last Updated: 2004.11.29 (xris) 00003 # 00004 # build_translation.pl 00005 # 00006 # scans mythweb files for translation strings, and builds 00007 # 00008 00009 # Load some required libraries 00010 use File::Find; 00011 use English; 00012 use Cwd 'abs_path'; 00013 use File::Basename; 00014 00015 # Slurp mode! 00016 $/ = undef; 00017 00018 # Initialize the strings hash 00019 my %strings; 00020 00021 # Find the mythweb dir 00022 my $languages_path = dirname(abs_path($PROGRAM_NAME)); 00023 my $mythweb_path = dirname(dirname(dirname($languages_path))); 00024 00025 # Make at least a reasonable attempt to make sure the script is where it's supposed to be 00026 die "Please make sure that this script is in modules/_shared/lang/\n" 00027 unless ($languages_path =~ /\/lang$/ && -e "$languages_path/English.lang"); 00028 00029 # Scan the files 00030 print "Scanning php files in $mythweb_path\n"; 00031 finddepth({wanted => \&process}, $mythweb_path); 00032 00033 # Scan the language files and update them for any missing strings 00034 foreach my $trans_file (<$languages_path/*lang>) { 00035 # Slurp in the file 00036 my $data = ''; 00037 open (DATA, $trans_file) or die "Can't read $trans_file: $!\n"; 00038 $data .= $_ while (<DATA>); 00039 close DATA; 00040 # Extract any existing strings 00041 my %translations = (); 00042 foreach my $group (split /\n(?=\S)/, $data) { 00043 my ($key, $indent, $trans) = $group =~ /^([^\n]+?)\s*(?:$|\s*\n(\s+)(.+)$)/s; 00044 # Cleanup 00045 $key =~ s/^\s+//; 00046 $key =~ s/\s+$//; 00047 if ($key =~ /^["\']/) { 00048 $key =~ s/^(["\'])(.+)\1$/$2/sg; 00049 } 00050 if ($trans) { 00051 $trans =~ s/\n$indent/\n/sg; 00052 $trans =~ s/^\s+//; 00053 $trans =~ s/\s+$//; 00054 } 00055 else { 00056 next; 00057 } 00058 # Store 00059 $translations{$key} = $trans; 00060 } 00061 # Open a new output file 00062 open(DATA, ">$languages_path/tmp.$$.lang") or die "Couldn't create tempfile $languages_path/tmp.$$.lang: $!\n"; 00063 # Build the new file 00064 foreach my $str (sort { lc($a) cmp lc($b) } keys %strings) { 00065 print DATA "\"$str\"\n"; 00066 if ($translations{$str}) { 00067 my $trans = $translations{$str}; 00068 $trans =~ s/\n/\n /g; 00069 print DATA " $trans\n"; 00070 } 00071 } 00072 # Close and rename the file into place 00073 close DATA; 00074 rename("$languages_path/tmp.$$.lang", $trans_file) or die "Couldn't rename $languages_path/tmp.$$.lang to $trans_file: $!\n"; 00075 # Notify the user 00076 print "Updated: $trans_file\n"; 00077 } 00078 00079 # This subroutine does all of the work 00080 sub process { 00081 my $this_file = $_; 00082 # Not a php file? 00083 return unless ($this_file =~ /\.php$/i); 00084 # Skip language files 00085 return if ($File::Find::dir eq "$mythweb_path/languages"); 00086 # For some reason, the file is gone? 00087 return unless (-e $this_file); 00088 # Load the file contents 00089 my $data = ''; 00090 open(TEXT, $this_file) or die "Can't open $File::Find::dir/$this_file: $!\n\n"; 00091 $data .= $_ while (<TEXT>); 00092 close TEXT; 00093 # Scan for translation strings 00094 while ($data =~ /\bt\(('.+?(?<!\\)'|".+?(?<!\\)")\s*[,\)]/sg) { 00095 $strings{clean_string($1)}++; 00096 } 00097 while ($data =~ /\btn\(((?:(?:'.+?(?<!\\)'|".+?(?<!\\)")(?:\s*,\s*))+)/sg) { 00098 foreach my $s (split(/\s*,\s*/, $1)) { 00099 next unless ($s =~ /\w/); 00100 $strings{clean_string($s)}++; 00101 } 00102 } 00103 } 00104 00105 sub clean_string { 00106 my $str = shift; 00107 if (eval("\$str = $str;")) { 00108 return $str; 00109 } 00110 return ''; 00111 } 00112
1.5.5