check-po.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # A simple perl script that checks if non-fuzzy translations in the passed po file:
  5. # - have the same number of %s or %(something)s, and similar texts;
  6. # - have balanced quote chars around the above texts.
  7. # For simplicity the script uses Test::More module for comparisons.
  8. use Test::More;
  9. my $state=1; # 0 -start, 1 - skip, 2 - in msgid, 3 in msgstr
  10. my $msgid="";
  11. my $msgstr="";
  12. $#ARGV == 0 or die "Usage $0 file.po\n";
  13. my $file=$ARGV[0];
  14. open (F, '<', $file) or die "Cannot open $file for reading: $!\n";
  15. sub get_quotetype($$)
  16. {
  17. my $front = shift;
  18. my $s = shift;
  19. my $c = "";
  20. if ($front and $s =~ /^\\"/) { $c = 'quot'; }
  21. elsif ($front and $s =~ /^[\'\`]/) { $c = "apos"; }
  22. elsif ((not $front) and $s =~ /\\"$/) { $c = 'quot'; }
  23. elsif ((not $front) and $s =~ /[\'\`]$/) { $c = "apos"; }
  24. return $c;
  25. }
  26. sub get($)
  27. {
  28. $_ = shift;
  29. my $line = $_;
  30. my $items = { 'nl' => 0 + scalar(s/\\n/ /g) };
  31. while (/%(\([^\)]+\))?[sdf]/) {
  32. ++$items->{$&};
  33. $_ = $';
  34. is_deeply(get_quotetype(0,$`), get_quotetype(1, $_), "Testing quote type $file:$.")
  35. || diag explain "msg: $line\n\n";
  36. }
  37. return $items;
  38. }
  39. sub check($$)
  40. {
  41. my $msgid = shift;
  42. my $msgstr = shift;
  43. my $it1 = get($msgid);
  44. my $it2 = get($msgstr);
  45. is_deeply($it1, $it2, "Testing $file:$.") || diag explain "msgid: $msgid\nmsgstr: $msgstr\n\n";
  46. }
  47. while (<F>)
  48. {
  49. if (/^\s*$/)
  50. {
  51. $state = 0;
  52. check($msgid, $msgstr) if $msgstr;
  53. $msgid = $msgstr = ""
  54. }
  55. $state=1 if /^#.*fuzzy/;
  56. next if $state == 1;
  57. next if /^#/;
  58. $state=2 if s/^msgid //;
  59. $state=3 if s/^msgstr //;
  60. s/^\s*"//;
  61. s/"\s*$//;
  62. $msgid .= $_ if $state == 2;
  63. $msgstr .= $_ if $state == 3;
  64. }
  65. check($msgid, $msgstr) if $msgstr;
  66. done_testing();