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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. sub getPluralInfo {
  57. my( $info ) = @_;
  58. # get string
  59. $info =~ s/.*Plural-Forms: (.+)\\n.*/$1/;
  60. $info =~ s/^(.*)\\n.*/$1/g;
  61. return $info;
  62. }
  63. my $task = shift( @ARGV );
  64. my $place = '..';
  65. die( "Usage: l10n.pl task\ntask: read, write\n" ) unless $task && $place;
  66. # Our current position
  67. my $whereami = cwd();
  68. die( "Program must be executed in a l10n-folder called 'l10n'" ) unless $whereami =~ m/\/l10n$/;
  69. # Where are i18n-files?
  70. my @dirs = crawlPrograms( $place, 1 );
  71. # Languages
  72. my @languages = ();
  73. opendir( DIR, '.' );
  74. my @files = readdir( DIR );
  75. closedir( DIR );
  76. foreach my $i ( @files ){
  77. push( @languages, $i ) if -d $i && substr( $i, 0, 1 ) ne '.';
  78. }
  79. if( $task eq 'read' ){
  80. rmtree( 'templates' );
  81. mkdir( 'templates' ) unless -d 'templates';
  82. print "Mode: reading\n";
  83. foreach my $dir ( @dirs ){
  84. my @temp = split( /\//, $dir );
  85. my $app = pop( @temp );
  86. chdir( $dir );
  87. my @totranslate = crawlFiles('.');
  88. my %ignore = readIgnorelist();
  89. my $output = "${whereami}/templates/$app.pot";
  90. print " Processing $app\n";
  91. foreach my $file ( @totranslate ){
  92. next if $ignore{$file};
  93. my $keywords = '';
  94. if( $file =~ /\.js$/ ){
  95. $keywords = '--keyword=t:2 --keyword=n:2,3';
  96. }
  97. else{
  98. $keywords = '--keyword=t --keyword=n:1,2';
  99. }
  100. my $language = ( $file =~ /\.js$/ ? 'Python' : 'PHP');
  101. my $joinexisting = ( -e $output ? '--join-existing' : '');
  102. print " Reading $file\n";
  103. `xgettext --output="$output" $joinexisting $keywords --language=$language "$file" --add-comments=TRANSLATORS --from-code=UTF-8 --package-version="6.0.0" --package-name="ownCloud Core" --msgid-bugs-address="translations\@owncloud.org"`;
  104. }
  105. chdir( $whereami );
  106. }
  107. }
  108. elsif( $task eq 'write' ){
  109. print "Mode: write\n";
  110. foreach my $dir ( @dirs ){
  111. my @temp = split( /\//, $dir );
  112. my $app = pop( @temp );
  113. chdir( $dir.'/l10n' );
  114. print " Processing $app\n";
  115. foreach my $language ( @languages ){
  116. next if $language eq 'templates';
  117. my $input = "${whereami}/$language/$app.po";
  118. next unless -e $input;
  119. print " Language $language\n";
  120. my $array = Locale::PO->load_file_asarray( $input );
  121. # Create array
  122. my @strings = ();
  123. my $plurals;
  124. foreach my $string ( @{$array} ){
  125. if( $string->msgid() eq '""' ){
  126. # Translator information
  127. $plurals = getPluralInfo( $string->msgstr());
  128. }
  129. elsif( defined( $string->msgstr_n() )){
  130. # plural translations
  131. my @variants = ();
  132. my $identifier = $string->msgid()."::".$string->msgid_plural();
  133. $identifier =~ s/"/_/g;
  134. foreach my $variant ( sort { $a <=> $b} keys( %{$string->msgstr_n()} )){
  135. push( @variants, $string->msgstr_n()->{$variant} );
  136. }
  137. push( @strings, "\"$identifier\" => array(".join(",", @variants).")");
  138. }
  139. else{
  140. # singular translations
  141. next if $string->msgstr() eq '""';
  142. push( @strings, $string->msgid()." => ".$string->msgstr());
  143. }
  144. }
  145. next if $#strings == -1; # Skip empty files
  146. # Write PHP file
  147. open( OUT, ">$language.php" );
  148. print OUT "<?php\n\$TRANSLATIONS = array(\n";
  149. print OUT join( ",\n", @strings );
  150. print OUT "\n);\n\$PLURAL_FORMS = \"$plurals\";\n";
  151. close( OUT );
  152. }
  153. chdir( $whereami );
  154. }
  155. }
  156. else{
  157. print "unknown task!\n";
  158. }