l10n.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. sub init() {
  64. # let's get the version from stdout of xgettext
  65. my $out = `xgettext --version`;
  66. # we assume the first line looks like this 'xgettext (GNU gettext-tools) 0.19.3'
  67. $out = substr $out, 29, index($out, "\n")-29;
  68. $out =~ s/^\s+|\s+$//g;
  69. $out = "v" . $out;
  70. my $actual = version->parse($out);
  71. # 0.18.3 introduced JavaScript as a language option
  72. my $expected = version->parse('v0.18.3');
  73. if ($actual < $expected) {
  74. die( "Minimum expected version of xgettext is " . $expected . ". Detected: " . $actual );
  75. }
  76. }
  77. init();
  78. my $task = shift( @ARGV );
  79. my $place = '..';
  80. die( "Usage: l10n.pl task\ntask: read, write\n" ) unless $task && $place;
  81. # Our current position
  82. my $whereami = cwd();
  83. die( "Program must be executed in a l10n-folder called 'l10n'" ) unless $whereami =~ m/\/l10n$/;
  84. # Where are i18n-files?
  85. my @dirs = crawlPrograms( $place, 1 );
  86. # Languages
  87. my @languages = ();
  88. opendir( DIR, '.' );
  89. my @files = readdir( DIR );
  90. closedir( DIR );
  91. foreach my $i ( @files ){
  92. push( @languages, $i ) if -d $i && substr( $i, 0, 1 ) ne '.';
  93. }
  94. if( $task eq 'read' ){
  95. rmtree( 'templates' );
  96. mkdir( 'templates' ) unless -d 'templates';
  97. print "Mode: reading\n";
  98. foreach my $dir ( @dirs ){
  99. my @temp = split( /\//, $dir );
  100. my $app = pop( @temp );
  101. chdir( $dir );
  102. my @totranslate = crawlFiles('.');
  103. my %ignore = readIgnorelist();
  104. my $output = "${whereami}/templates/$app.pot";
  105. print " Processing $app\n";
  106. foreach my $file ( @totranslate ){
  107. next if $ignore{$file};
  108. my $keywords = '';
  109. if( $file =~ /\.js$/ ){
  110. $keywords = '--keyword=t:2 --keyword=n:2,3';
  111. }
  112. else{
  113. $keywords = '--keyword=t --keyword=n:1,2';
  114. }
  115. my $language = ( $file =~ /\.js$/ ? 'Javascript' : 'PHP');
  116. my $joinexisting = ( -e $output ? '--join-existing' : '');
  117. print " Reading $file\n";
  118. `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"`;
  119. }
  120. chdir( $whereami );
  121. }
  122. }
  123. elsif( $task eq 'write' ){
  124. print "Mode: write\n";
  125. foreach my $dir ( @dirs ){
  126. my @temp = split( /\//, $dir );
  127. my $app = pop( @temp );
  128. chdir( $dir.'/l10n' );
  129. print " Processing $app\n";
  130. foreach my $language ( @languages ){
  131. next if $language eq 'templates';
  132. my $input = "${whereami}/$language/$app.po";
  133. next unless -e $input;
  134. print " Language $language\n";
  135. my $array = Locale::PO->load_file_asarray( $input );
  136. # Create array
  137. my @strings = ();
  138. my @js_strings = ();
  139. my $plurals;
  140. TRANSLATIONS: foreach my $string ( @{$array} ){
  141. if( $string->msgid() eq '""' ){
  142. # Translator information
  143. $plurals = getPluralInfo( $string->msgstr());
  144. }
  145. elsif( defined( $string->msgstr_n() )){
  146. # plural translations
  147. my @variants = ();
  148. my $msgid = $string->msgid();
  149. $msgid =~ s/^"(.*)"$/$1/;
  150. my $msgid_plural = $string->msgid_plural();
  151. $msgid_plural =~ s/^"(.*)"$/$1/;
  152. my $identifier = "_" . $msgid."_::_".$msgid_plural . "_";
  153. foreach my $variant ( sort { $a <=> $b} keys( %{$string->msgstr_n()} )){
  154. next TRANSLATIONS if $string->msgstr_n()->{$variant} eq '""';
  155. push( @variants, $string->msgstr_n()->{$variant} );
  156. }
  157. push( @strings, "\"$identifier\" => array(".join(",", @variants).")");
  158. push( @js_strings, "\"$identifier\" : [".join(",", @variants)."]");
  159. }
  160. else{
  161. # singular translations
  162. next TRANSLATIONS if $string->msgstr() eq '""';
  163. push( @strings, $string->msgid()." => ".$string->msgstr());
  164. push( @js_strings, $string->msgid()." : ".$string->msgstr());
  165. }
  166. }
  167. next if $#strings == -1; # Skip empty files
  168. for (@strings) {
  169. s/\$/\\\$/g;
  170. }
  171. # delete old php file
  172. unlink "$language.php";
  173. # Write js file
  174. open( OUT, ">$language.js" );
  175. print OUT "OC.L10N.register(\n \"$app\",\n {\n ";
  176. print OUT join( ",\n ", @js_strings );
  177. print OUT "\n},\n\"$plurals\");\n";
  178. close( OUT );
  179. # Write json file
  180. open( OUT, ">$language.json" );
  181. print OUT "{ \"translations\": ";
  182. print OUT "{\n ";
  183. print OUT join( ",\n ", @js_strings );
  184. print OUT "\n},\"pluralForm\" :\"$plurals\"\n}";
  185. close( OUT );
  186. }
  187. chdir( $whereami );
  188. }
  189. }
  190. else{
  191. print "unknown task!\n";
  192. }