You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

l10n.pl 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/perl
  2. use strict;
  3. use Locale::PO;
  4. use Cwd;
  5. use Data::Dumper;
  6. use File::Path;
  7. sub crawlPrograms{
  8. my( $dir, $ignore ) = @_;
  9. my @found = ();
  10. opendir( DIR, $dir );
  11. my @files = readdir( DIR );
  12. closedir( DIR );
  13. @files = sort( @files );
  14. foreach my $i ( @files ){
  15. next if substr( $i, 0, 1 ) eq '.';
  16. if( $i eq 'l10n' && !$ignore ){
  17. push( @found, $dir );
  18. }
  19. elsif( -d $dir.'/'.$i ){
  20. push( @found, crawlPrograms( $dir.'/'.$i ));
  21. }
  22. }
  23. return @found;
  24. }
  25. sub crawlFiles{
  26. my( $dir ) = @_;
  27. my @found = ();
  28. opendir( DIR, $dir );
  29. my @files = readdir( DIR );
  30. closedir( DIR );
  31. @files = sort( @files );
  32. foreach my $i ( @files ){
  33. next if substr( $i, 0, 1 ) eq '.';
  34. next if $i eq 'l10n';
  35. if( -d $dir.'/'.$i ){
  36. push( @found, crawlFiles( $dir.'/'.$i ));
  37. }
  38. else{
  39. push(@found,$dir.'/'.$i) if $i =~ /\.js$/ || $i =~ /\.php$/;
  40. }
  41. }
  42. return @found;
  43. }
  44. sub readIgnorelist{
  45. return () unless -e 'l10n/ignorelist';
  46. my %ignore = ();
  47. open(IN,'l10n/ignorelist');
  48. while(<IN>){
  49. my $line = $_;
  50. chomp($line);
  51. $ignore{"./$line"}++;
  52. }
  53. close(IN);
  54. return %ignore;
  55. }
  56. my $task = shift( @ARGV );
  57. my $place = '..';
  58. die( "Usage: l10n.pl task\ntask: read, write\n" ) unless $task && $place;
  59. # Our current position
  60. my $whereami = cwd();
  61. die( "Program must be executed in a l10n-folder called 'l10n'" ) unless $whereami =~ m/\/l10n$/;
  62. # Where are i18n-files?
  63. my @dirs = crawlPrograms( $place, 1 );
  64. # Languages
  65. my @languages = ();
  66. opendir( DIR, '.' );
  67. my @files = readdir( DIR );
  68. closedir( DIR );
  69. foreach my $i ( @files ){
  70. push( @languages, $i ) if -d $i && substr( $i, 0, 1 ) ne '.';
  71. }
  72. if( $task eq 'read' ){
  73. rmtree( 'templates' );
  74. mkdir( 'templates' ) unless -d 'templates';
  75. print "Mode: reading\n";
  76. foreach my $dir ( @dirs ){
  77. my @temp = split( /\//, $dir );
  78. my $app = pop( @temp );
  79. chdir( $dir );
  80. my @totranslate = crawlFiles('.');
  81. my %ignore = readIgnorelist();
  82. my $output = "${whereami}/templates/$app.pot";
  83. print " Processing $app\n";
  84. foreach my $file ( @totranslate ){
  85. next if $ignore{$file};
  86. my $keyword = ( $file =~ /\.js$/ ? 't:2' : 't');
  87. my $language = ( $file =~ /\.js$/ ? 'Python' : 'PHP');
  88. my $joinexisting = ( -e $output ? '--join-existing' : '');
  89. print " Reading $file\n";
  90. `xgettext --output="$output" $joinexisting --keyword=$keyword --language=$language "$file"`;
  91. }
  92. chdir( $whereami );
  93. }
  94. }
  95. elsif( $task eq 'write' ){
  96. print "Mode: write\n";
  97. foreach my $dir ( @dirs ){
  98. my @temp = split( /\//, $dir );
  99. my $app = pop( @temp );
  100. chdir( $dir.'/l10n' );
  101. print " Processing $app\n";
  102. foreach my $language ( @languages ){
  103. next if $language eq 'templates';
  104. my $input = "${whereami}/$language/$app.po";
  105. next unless -e $input;
  106. print " Language $language\n";
  107. my $array = Locale::PO->load_file_asarray( $input );
  108. # Create array
  109. my @strings = ();
  110. foreach my $string ( @{$array} ){
  111. next if $string->msgid() eq '""';
  112. next if $string->msgstr() eq '""';
  113. push( @strings, $string->msgid()." => ".$string->msgstr());
  114. }
  115. next if $#strings == -1; # Skip empty files
  116. # Write PHP file
  117. open( OUT, ">$language.php" );
  118. print OUT "<?php \$TRANSLATIONS = array(\n";
  119. print OUT join( ",\n", @strings );
  120. print OUT "\n);\n";
  121. close( OUT );
  122. }
  123. chdir( $whereami );
  124. }
  125. }
  126. else{
  127. print "unknown task!\n";
  128. }