i18n-update.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/perl
  2. @ARGV <= 2 || die "Usage: $0 [<po directory>] [<file pattern>]\n";
  3. my $source = shift @ARGV;
  4. my $pattern = shift @ARGV || '*.po';
  5. sub read_header
  6. {
  7. my $file = shift || return;
  8. local $/;
  9. open P, "< $file" || die "open(): $!";
  10. my $data = readline P;
  11. close P;
  12. $data =~ /
  13. ^ (
  14. msgid \s "" \n
  15. msgstr \s "" \n
  16. (?: " [^\n]+ " \n )+
  17. \n )
  18. /mx;
  19. return $1;
  20. }
  21. sub write_header
  22. {
  23. my $file = shift || return;
  24. my $head = shift || return;
  25. local $/;
  26. open P, "< $file" || die "open(): $!";
  27. my $data = readline P;
  28. close P;
  29. $data =~ s/
  30. ^ (
  31. msgid \s "" \n
  32. msgstr \s "" \n
  33. (?: " [^\n]+ " \n )+
  34. \n )
  35. /$head/mx;
  36. open P, "> $file" || die "open(): $!";
  37. print P $data;
  38. close P;
  39. }
  40. my @dirs;
  41. if( ! $source )
  42. {
  43. @dirs = glob("./*/*/po");
  44. }
  45. else
  46. {
  47. @dirs = ( $source );
  48. }
  49. foreach my $dir (@dirs)
  50. {
  51. if( open F, "find $dir -type f -name '$pattern' |" )
  52. {
  53. while( chomp( my $file = readline F ) )
  54. {
  55. my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
  56. if( -f "$dir/templates/$basename.pot" )
  57. {
  58. my $head = read_header($file);
  59. printf "Updating %-40s", $file;
  60. system("msgmerge", "-U", "-N", $file, "$dir/templates/$basename.pot");
  61. write_header($file, $head);
  62. }
  63. }
  64. close F;
  65. }
  66. }