l10n.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 =~ /.*(?<!\.min)\.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="8.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 @js_strings = ();
  124. my $plurals;
  125. TRANSLATIONS: foreach my $string ( @{$array} ){
  126. if( $string->msgid() eq '""' ){
  127. # Translator information
  128. $plurals = getPluralInfo( $string->msgstr());
  129. }
  130. elsif( defined( $string->msgstr_n() )){
  131. # plural translations
  132. my @variants = ();
  133. my $msgid = $string->msgid();
  134. $msgid =~ s/^"(.*)"$/$1/;
  135. my $msgid_plural = $string->msgid_plural();
  136. $msgid_plural =~ s/^"(.*)"$/$1/;
  137. my $identifier = "_" . $msgid."_::_".$msgid_plural . "_";
  138. foreach my $variant ( sort { $a <=> $b} keys( %{$string->msgstr_n()} )){
  139. next TRANSLATIONS if $string->msgstr_n()->{$variant} eq '""';
  140. push( @variants, $string->msgstr_n()->{$variant} );
  141. }
  142. push( @strings, "\"$identifier\" => array(".join(",", @variants).")");
  143. push( @js_strings, "\"$identifier\" : [".join(",", @variants)."]");
  144. }
  145. else{
  146. # singular translations
  147. next TRANSLATIONS if $string->msgstr() eq '""';
  148. push( @strings, $string->msgid()." => ".$string->msgstr());
  149. push( @js_strings, $string->msgid()." : ".$string->msgstr());
  150. }
  151. }
  152. next if $#strings == -1; # Skip empty files
  153. for (@strings) {
  154. s/\$/\\\$/g;
  155. }
  156. # delete old php file
  157. unlink "$language.php";
  158. # Write js file
  159. open( OUT, ">$language.js" );
  160. print OUT "OC.L10N.register(\n \"$app\",\n {\n ";
  161. print OUT join( ",\n ", @js_strings );
  162. print OUT "\n},\n\"$plurals\");\n";
  163. close( OUT );
  164. # Write json file
  165. open( OUT, ">$language.json" );
  166. print OUT "{ \"translations\": ";
  167. print OUT "{\n ";
  168. print OUT join( ",\n ", @js_strings );
  169. print OUT "\n},\"pluralForm\" :\"$plurals\"\n}";
  170. close( OUT );
  171. }
  172. chdir( $whereami );
  173. }
  174. }
  175. else{
  176. print "unknown task!\n";
  177. }