| 1 | #!/usr/local/bin/perl | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## PO Files Manipulation - ~/lib//mnt/src/perl/Text-PO/scripts/po.pl | ||
| 4 | ## Version v0.3.0 | ||
| 5 | ## Copyright(c) 2024 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2021/07/24 | ||
| 8 | ## Modified 2025/12/01 | ||
| 9 | ## All rights reserved | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use lib './lib'; # REMOVE ME | ||
| 19 | use DateTime; | ||
| 20 | use Getopt::Class; | ||
| 21 | use IO::File; | ||
| 22 | use Pod::Usage; | ||
| 23 | use Text::PO; | ||
| 24 | use Text::PO::MO; | ||
| 25 | use Text::Wrap (); | ||
| 26 | our $PLURALS = {}; | ||
| 27 | our $VERSION = 'v0.3.0'; | ||
| 28 | }; | ||
| 29 | |||
| 30 | { | ||
| 31 | our $DEBUG = 0; | ||
| 32 | our $VERBOSE = 0; | ||
| 33 | our $LOG_LEVEL = 0; | ||
| 34 | our $PROG_NAME = 'po'; | ||
| 35 | |||
| 36 | our $out = IO::File->new; | ||
| 37 | $out->fdopen( fileno( STDOUT ), 'w' ); | ||
| 38 | $out->binmode( ':utf8' ); | ||
| 39 | $out->autoflush(1); | ||
| 40 | |||
| 41 | our $err = IO::File->new; | ||
| 42 | $err->autoflush(1); | ||
| 43 | $err->fdopen( fileno( STDERR ), 'w' ); | ||
| 44 | $err->binmode( ":utf8" ); | ||
| 45 | |||
| 46 | &_load_plurals(); | ||
| 47 | |||
| 48 | my $dict = | ||
| 49 | { | ||
| 50 | # Actions | ||
| 51 | as_json => { type => 'boolean' }, | ||
| 52 | as_po => { type => 'boolean' }, | ||
| 53 | add => { type => 'boolean' }, | ||
| 54 | add_include => { type => 'boolean' }, | ||
| 55 | compile => { type => 'boolean' }, | ||
| 56 | dump => { type => 'boolean' }, | ||
| 57 | init => { type => 'boolean' }, | ||
| 58 | pp => { type => 'boolean', alias => [qw( pre-processing )] }, | ||
| 59 | sync => { type => 'boolean' }, | ||
| 60 | |||
| 61 | # Attributes | ||
| 62 | after => { type => 'string' }, | ||
| 63 | before => { type => 'string' }, | ||
| 64 | bugs_to => { type => 'string', class => [qw( init meta )] }, | ||
| 65 | charset => { type => 'string', class => [qw( init meta )], default => 'utf-8' }, | ||
| 66 | created_on => { type => 'datetime', class => [qw( init meta )] }, | ||
| 67 | domain => { type => 'string' }, | ||
| 68 | encoding => { type => 'string', class => [qw( init meta )], default => '8bit' }, | ||
| 69 | # Used for includes | ||
| 70 | file => { type => 'string' }, | ||
| 71 | header => { type => 'string' }, | ||
| 72 | include => { type => 'boolean' }, | ||
| 73 | lang => { type => 'string', alias => [qw( language )], class => [qw( init meta )], re => qr/^[a-z]{2}(?:_[A-Z]{2})?$/ }, | ||
| 74 | max_recurse => { type => 'integer', default => 0 }, | ||
| 75 | msgid => { type => 'string', class => [qw( edit )] }, | ||
| 76 | msgstr => { type => 'string', class => [qw( edit )] }, | ||
| 77 | output => { type => 'file' }, | ||
| 78 | output_dir => { type => 'file' }, | ||
| 79 | overwrite => { type => 'boolean', default => 0 }, | ||
| 80 | po_debug => { type => 'integer', default => 0 }, | ||
| 81 | # Used as a template to create the po file with --init | ||
| 82 | pot => { type => 'string', class => [qw( init )] }, | ||
| 83 | project => { type => 'string', class => [qw( init meta )] }, | ||
| 84 | revised_on => { type => 'datetime', class => [qw( init meta )] }, | ||
| 85 | settings => { type => 'string' }, | ||
| 86 | team => { type => 'string', class => [qw( init meta )], alias => [qw( language-team )] }, | ||
| 87 | translator => { type => 'string', class => [qw( init meta )] }, | ||
| 88 | tz => { type => 'string', alias => [qw( time_zone timezone )], class => [qw( init meta )] }, | ||
| 89 | version => { type => 'string', class => [qw( init meta )] }, | ||
| 90 | |||
| 91 | # Generic options | ||
| 92 | quiet => { type => 'boolean', default => 0 }, | ||
| 93 | debug => { type => 'integer', alias => [qw(d)], default => \$DEBUG }, | ||
| 94 | verbose => { type => 'integer', default => \$VERBOSE }, | ||
| 95 | v => { type => 'code', code => sub{ printf( STDOUT "2f\n", $VERSION ); } }, | ||
| 96 | help => { type => 'code', alias => [qw(?)], code => sub{ pod2usage( -exitstatus => 1, -verbose => 99, -sections => [qw( NAME SYNOPSIS DESCRIPTION MODES OPTIONS AUTHOR COPYRIGHT )] ); }, action => 1 }, | ||
| 97 | man => { type => 'code', code => sub{ pod2usage( -exitstatus => 0, -verbose => 2 ); } }, | ||
| 98 | }; | ||
| 99 | |||
| 100 | our $opt = Getopt::Class->new({ dictionary => $dict }) || die( "Error instantiating Getopt::Class object: ", Getopt::Class->error, "\n" ); | ||
| 101 | $opt->usage( sub{ pod2usage(2) } ); | ||
| 102 | our $opts = $opt->exec || die( "An error occurred executing Getopt::Class: ", $opt->error, "\n" ); | ||
| 103 | |||
| 104 | # Unless the log level has been set directly with a command line option | ||
| 105 | unless( $LOG_LEVEL ) | ||
| 106 | { | ||
| 107 | $LOG_LEVEL = 1 if( $VERBOSE ); | ||
| 108 | $LOG_LEVEL = ( 1 + $DEBUG ) if( $DEBUG ); | ||
| 109 | } | ||
| 110 | |||
| 111 | my @errors = (); | ||
| 112 | my $opt_errors = $opt->configure_errors; | ||
| 113 | push( @errors, @$opt_errors ) if( $opt_errors->length ); | ||
| 114 | if( $opts->{quiet} ) | ||
| 115 | { | ||
| 116 | $DEBUG = $VERBOSE = 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | $out->print( @errors ? " not ok\n" : " ok\n" ) if( $LOG_LEVEL ); | ||
| 120 | if( @errors ) | ||
| 121 | { | ||
| 122 | my $error = join( "\n", map{ "\t* $_" } @errors ); | ||
| 123 | substr( $error, 0, 0, "\n\tThe following arguments are mandatory and missing.\n" ); | ||
| 124 | if( !$opts->{quiet} ) | ||
| 125 | { | ||
| 126 | $out->print( <<EOT ); | ||
| 127 | $error | ||
| 128 | Please, use option '-h' or '--help' to find out and properly call | ||
| 129 | this program in interactive mode: | ||
| 130 | |||
| 131 | $PROG_NAME -h | ||
| 132 | EOT | ||
| 133 | } | ||
| 134 | exit(1); | ||
| 135 | } | ||
| 136 | |||
| 137 | if( $opts->{compile} && $opts->{output} ) | ||
| 138 | { | ||
| 139 | my $f = shift( @ARGV ) || bailout( "No po file to read was provided." ); | ||
| 140 | &compile( in => $f, out => $opts->{output} ); | ||
| 141 | } | ||
| 142 | elsif( $opts->{init} ) | ||
| 143 | { | ||
| 144 | my $out = $opts->{output} || shift( @ARGV ) || bailout( "No po file path was specified to initiate." ); | ||
| 145 | &init_po( $out ); | ||
| 146 | } | ||
| 147 | elsif( $opts->{as_json} && $opts->{output} ) | ||
| 148 | { | ||
| 149 | my $f = shift( @ARGV ) || bailout( "No po file to read was provided." ); | ||
| 150 | _message( 3, "Reading file \"$f\" and writing to \"$opts->{output}\"." ); | ||
| 151 | &to_json( in => $f, out => $opts->{output} ); | ||
| 152 | } | ||
| 153 | elsif( $opts->{as_po} && $opts->{output} ) | ||
| 154 | { | ||
| 155 | my $f = shift( @ARGV ) || bailout( "No (json) po file to read was provided." ); | ||
| 156 | _message( 3, "Reading file \"$f\" and writing to \"$opts->{output}\"." ); | ||
| 157 | &to_po( in => $f, out => $opts->{output} ); | ||
| 158 | } | ||
| 159 | elsif( $opts->{add} ) | ||
| 160 | { | ||
| 161 | my $f = shift( @ARGV ) || bailout( "No po file to read was provided." ); | ||
| 162 | &add( in => $f ); | ||
| 163 | } | ||
| 164 | elsif( $opts->{sync} && $opts->{output} ) | ||
| 165 | { | ||
| 166 | my $f = shift( @ARGV ) || bailout( "No (json) po file to read was provided." ); | ||
| 167 | _message( 3, "Reading file \"$f\" and writing to \"$opts->{output}\"." ); | ||
| 168 | &sync( in => $f, out => $opts->{output} ); | ||
| 169 | } | ||
| 170 | # Pre-processing for one file with an output file provided | ||
| 171 | elsif( $opts->{pp} && $opts->{output} ) | ||
| 172 | { | ||
| 173 | my $f = shift( @ARGV ) || bailout( "No po file to pre-process was provided." ); | ||
| 174 | _message( 3, "Pre-processing file \"$f\" and writing to \"$opts->{output}\"." ); | ||
| 175 | &pp( in => $f, out => $opts->{output} ); | ||
| 176 | } | ||
| 177 | elsif( $opts->{add_include} ) | ||
| 178 | { | ||
| 179 | my $f = shift( @ARGV ) || bailout( "No po file to add the include to was provided." ); | ||
| 180 | _message( 3, "Adding include to file \"$f\"." ); | ||
| 181 | &add_include( in => $f, file => $opts->{file} ); | ||
| 182 | } | ||
| 183 | else | ||
| 184 | { | ||
| 185 | my $is_multi = scalar( @ARGV ) > 1 ? 1 : 0; | ||
| 186 | if( $opts->{pp} && | ||
| 187 | $is_multi && | ||
| 188 | !$opts->{overwrite} && | ||
| 189 | !$opts->{output_dir} ) | ||
| 190 | { | ||
| 191 | bailout( "Error: Pre-processing request, but multiple input files provided, and neither --overwrite nor --output-dir was specified. Refusing to merge outputs into STDOUT." ); | ||
| 192 | } | ||
| 193 | my $output_dir; | ||
| 194 | $output_dir = $opt->new_file( $opts->{output_dir} ) if( $opts->{output_dir} ); | ||
| 195 | foreach my $f ( @ARGV ) | ||
| 196 | { | ||
| 197 | $out->print( "Processing file \"$f\"\n" ); | ||
| 198 | # $po->debug( 3 ); | ||
| 199 | if( $opts->{dump} ) | ||
| 200 | { | ||
| 201 | _messagec( 3, "Dumping file <green>$f</>" ); | ||
| 202 | my $file = $opt->new_file( $f ); | ||
| 203 | my $po; | ||
| 204 | if( $file->extension eq 'mo' ) | ||
| 205 | { | ||
| 206 | my $mo = Text::PO::MO->new( file => $file, debug => $opts->{debug} ); | ||
| 207 | $po = $mo->as_object || _messagec( 3, "<red>", $mo->error, "</>" ); | ||
| 208 | } | ||
| 209 | elsif( $file->extension eq 'json' ) | ||
| 210 | { | ||
| 211 | $po = Text::PO->new( | ||
| 212 | ( defined( $opts->{include} ) ? ( include => $opts->{include} ) : () ), | ||
| 213 | ( $opts->{max_recurse} ? ( max_recurse => $opts->{max_recurse} ) : () ), | ||
| 214 | debug => $opts->{debug} | ||
| 215 | ) || bailout( Text::PO->error ); | ||
| 216 | $po->parse2object( $file ) || bailout( $po->error ); | ||
| 217 | } | ||
| 218 | else | ||
| 219 | { | ||
| 220 | $po = Text::PO->new( | ||
| 221 | ( defined( $opts->{include} ) ? ( include => $opts->{include} ) : () ), | ||
| 222 | ( $opts->{max_recurse} ? ( max_recurse => $opts->{max_recurse} ) : () ), | ||
| 223 | debug => $opts->{debug} | ||
| 224 | ) || bailout( Text::PO->error ); | ||
| 225 | $po->parse( $file ) || bailout( $po->error ); | ||
| 226 | } | ||
| 227 | $po->dump( $out ); | ||
| 228 | next; | ||
| 229 | } | ||
| 230 | |||
| 231 | # Saving as JSON would imply processing all includes, but the user may very well use the option --noinclude, and then only the msgid within that specific file would be converted to JSON. | ||
| 232 | if( $opts->{as_json} ) | ||
| 233 | { | ||
| 234 | my $file = $opt->new_file( $f ); | ||
| 235 | my $base = $file->basename; | ||
| 236 | my $out; | ||
| 237 | if( $output_dir ) | ||
| 238 | { | ||
| 239 | $output_dir->mkpath if( !$output_dir->exists ); | ||
| 240 | # my $domain = $po->domain || bailout( "Unable to get the domain from the po file \"$f\"" ); | ||
| 241 | # $out = $output_dir->child( "${domain}.mo" ); | ||
| 242 | $out = $output_dir->child( $base )->extension( 'json' ); | ||
| 243 | } | ||
| 244 | # If multiple files, their respective output is saved to the equivalent file with extension 'json' | ||
| 245 | # If it is a lone file, its output will be printed to STDOUT | ||
| 246 | elsif( $is_multi ) | ||
| 247 | { | ||
| 248 | $out = $file->extension( 'json' ); | ||
| 249 | } | ||
| 250 | &to_json( in => $f, ( defined( $out ) ? ( out => $out ) : () ) ); | ||
| 251 | } | ||
| 252 | elsif( $opts->{as_po} ) | ||
| 253 | { | ||
| 254 | my $file = $opt->new_file( $f ); | ||
| 255 | my $base = $file->basename; | ||
| 256 | my $out; | ||
| 257 | if( $output_dir ) | ||
| 258 | { | ||
| 259 | $output_dir->mkpath if( !$output_dir->exists ); | ||
| 260 | # my $domain = $po->domain || bailout( "Unable to get the domain from the po file \"$f\"" ); | ||
| 261 | # $out = $output_dir->child( "${domain}.mo" ); | ||
| 262 | $out = $output_dir->child( $base )->extension( 'po' ); | ||
| 263 | } | ||
| 264 | # If multiple files, their respective output is saved to the equivalent file with extension 'json' | ||
| 265 | # If it is a lone file, its output will be printed to STDOUT | ||
| 266 | elsif( $is_multi ) | ||
| 267 | { | ||
| 268 | $out = $file->extension( 'po' ); | ||
| 269 | } | ||
| 270 | &to_po( in => $f, ( defined( $out ) ? ( out => $out ) : () ) ); | ||
| 271 | } | ||
| 272 | elsif( $opts->{compile} ) | ||
| 273 | { | ||
| 274 | my $file = $opt->new_file( $f ); | ||
| 275 | my $base = $file->basename; | ||
| 276 | my $out; | ||
| 277 | if( $output_dir ) | ||
| 278 | { | ||
| 279 | $output_dir->mkpath if( !$output_dir->exists ); | ||
| 280 | # my $domain = $po->domain || bailout( "Unable to get the domain from the po file \"$f\"" ); | ||
| 281 | # $out = $output_dir->child( "${domain}.mo" ); | ||
| 282 | $out = $output_dir->child( $base )->extension( 'mo' ); | ||
| 283 | } | ||
| 284 | else | ||
| 285 | { | ||
| 286 | $out = $file->extension( 'mo' ); | ||
| 287 | } | ||
| 288 | &compile( in => $f, out => $out ); | ||
| 289 | } | ||
| 290 | elsif( $opts->{pp} ) | ||
| 291 | { | ||
| 292 | my $file = $opt->new_file( $f ); | ||
| 293 | my $base = $file->basename; | ||
| 294 | my $out; | ||
| 295 | if( $output_dir ) | ||
| 296 | { | ||
| 297 | $output_dir->mkpath if( !$output_dir->exists ); | ||
| 298 | # my $domain = $po->domain || bailout( "Unable to get the domain from the po file \"$f\"" ); | ||
| 299 | # $out = $output_dir->child( "${domain}.mo" ); | ||
| 300 | # Make sure the extension is 'po' | ||
| 301 | $out = $output_dir->child( $base )->extension( 'po' ); | ||
| 302 | } | ||
| 303 | # If multiple files, their respective output is saved to the equivalent file with extension 'po', which may be the same as the original file, and if so pp() will bail out unless --overwrite is specified. | ||
| 304 | # If it is a lone file, its output will be printed to STDOUT | ||
| 305 | elsif( $is_multi ) | ||
| 306 | { | ||
| 307 | # We make sure the extension is 'po' | ||
| 308 | # pp() will bailout if this file already exists and --overwrite was not provided. | ||
| 309 | $out = $file->extension( 'po' ); | ||
| 310 | } | ||
| 311 | &pp( in => $f, ( defined( $out ) ? ( out => $out ) : () ) ); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | exit(0); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub add | ||
| 319 | { | ||
| 320 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 321 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 322 | $f = $opt->new_file( $f ); | ||
| 323 | # We just re-use this variable | ||
| 324 | $p = | ||
| 325 | { | ||
| 326 | debug => $opts->{debug}, | ||
| 327 | # We do not want to process include; we just want to manipulate the PO file. | ||
| 328 | include => 0, | ||
| 329 | }; | ||
| 330 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 331 | my $ext = $f->extension; | ||
| 332 | if( $ext eq 'po' ) | ||
| 333 | { | ||
| 334 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 335 | $po->parse( $f ) || bailout( $po->error ); | ||
| 336 | } | ||
| 337 | elsif( $ext eq 'json' ) | ||
| 338 | { | ||
| 339 | $p->{use_json} = 1; | ||
| 340 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 341 | $po->parse2object( $f ) || bailout( $po->error ); | ||
| 342 | } | ||
| 343 | elsif( $ext eq 'mo' ) | ||
| 344 | { | ||
| 345 | # The option 'include' is not supported by Text::PO::MO | ||
| 346 | delete( $p->{include} ); | ||
| 347 | $p->{file} = $f; | ||
| 348 | my $mo = Text::PO::MO->new( %$p ); | ||
| 349 | $po = $mo->as_object || bailout( $mo->error ); | ||
| 350 | } | ||
| 351 | else | ||
| 352 | { | ||
| 353 | bailout( "Unknown source file \"$f\"" ); | ||
| 354 | } | ||
| 355 | _messagec( 3, "Adding id \"<green>$opts->{msgid}</>\" -> \"<green>$opts->{msgstr}</>\"" ); | ||
| 356 | $po->add_element( | ||
| 357 | msgid => "$opts->{msgid}", | ||
| 358 | msgstr => "$opts->{msgstr}", | ||
| 359 | ( $opts->{after} ? ( after => $opts->{after} ) : () ), | ||
| 360 | ( $opts->{before} ? ( before => $opts->{before} ) : () ), | ||
| 361 | ) || bailout( $po->error ); | ||
| 362 | _messagec( 3, "Saving back to \"<green>$f</>\"" ); | ||
| 363 | |||
| 364 | if( $ext eq 'po' ) | ||
| 365 | { | ||
| 366 | my $binmode = ( $opts->{charset} || 'utf-8' ); | ||
| 367 | $binmode = 'utf8' if( lc( $binmode ) eq 'utf-8' ); | ||
| 368 | my $fh = $f->open( '>', { binmode => $binmode, autoflush => 1 } ) || bailout( "Unable to open the output file in write mode: ", $out->error ); | ||
| 369 | $po->dump( $fh ); | ||
| 370 | $fh->close; | ||
| 371 | } | ||
| 372 | elsif( $ext eq 'json' ) | ||
| 373 | { | ||
| 374 | my $json = $po->as_json({ pretty => 1, canonical => 1 }); | ||
| 375 | _messagec( 3, "<red>", $po->error, "</>" ) if( !$json ); | ||
| 376 | my $fh = $o->open( '>', { binmode => ':utf8', autoflush => 1 }) || bailout( "Unable to open output file \"$o\" in write mode: $!" ); | ||
| 377 | $fh->print( $json ); | ||
| 378 | $fh->close; | ||
| 379 | } | ||
| 380 | elsif( $ext eq 'mo' ) | ||
| 381 | { | ||
| 382 | my $mo = Text::PO::MO->new( | ||
| 383 | file => $f, | ||
| 384 | debug => $opts->{debug}, | ||
| 385 | ) || bailout( Text::PO::MO->error ); | ||
| 386 | $mo->write( $po ) || bailout( "Unable to write to \"$o\": ", $mo->error ); | ||
| 387 | } | ||
| 388 | _messagec( 3, "<green>Done.</>" ); | ||
| 389 | return(1); | ||
| 390 | } | ||
| 391 | |||
| 392 | sub add_include | ||
| 393 | { | ||
| 394 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 395 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 396 | my $inc = $p->{file} || bailout( "No include file to add was specificed." ); | ||
| 397 | $f = $opt->new_file( $f ); | ||
| 398 | # We just re-use this variable | ||
| 399 | $p = | ||
| 400 | { | ||
| 401 | debug => $opts->{debug}, | ||
| 402 | # We do not want to process include; we just want to manipulate the PO file. | ||
| 403 | include => 0, | ||
| 404 | }; | ||
| 405 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 406 | if( $f->extension eq 'po' ) | ||
| 407 | { | ||
| 408 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 409 | $po->parse( $f ) || bailout( $po->error ); | ||
| 410 | } | ||
| 411 | elsif( $f->extension eq 'json' ) | ||
| 412 | { | ||
| 413 | bailout( "Cannot add an include directive to a JSON file." ); | ||
| 414 | } | ||
| 415 | elsif( $f->extension eq 'mo' ) | ||
| 416 | { | ||
| 417 | bailout( "Cannot add an include directive to a .mo file." ); | ||
| 418 | } | ||
| 419 | else | ||
| 420 | { | ||
| 421 | bailout( "Unknown source file \"$f\"" ); | ||
| 422 | } | ||
| 423 | _messagec( 3, "Adding include directive for file '<green>$inc</>'" ); | ||
| 424 | $po->add_include( | ||
| 425 | file => $inc, | ||
| 426 | ( $opts->{after} ? ( after => $opts->{after} ) : () ), | ||
| 427 | ( $opts->{before} ? ( before => $opts->{before} ) : () ), | ||
| 428 | ) || bailout( $po->error ); | ||
| 429 | _messagec( 3, "Saving back to \"<green>$f</>\"" ); | ||
| 430 | my $binmode = ( $opts->{charset} || 'utf-8' ); | ||
| 431 | $binmode = 'utf8' if( lc( $binmode ) eq 'utf-8' ); | ||
| 432 | my $fh = $f->open( '>', { binmode => $binmode, autoflush => 1 } ) || bailout( "Unable to open the output file in write mode: ", $out->error ); | ||
| 433 | $po->dump( $fh ); | ||
| 434 | $fh->close; | ||
| 435 | _messagec( 3, "<green>Done.</>" ); | ||
| 436 | return(1); | ||
| 437 | } | ||
| 438 | |||
| 439 | sub bailout | ||
| 440 | { | ||
| 441 | $err->print( @_, "\n" ); | ||
| 442 | exit(1); | ||
| 443 | } | ||
| 444 | |||
| 445 | sub compile | ||
| 446 | { | ||
| 447 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 448 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 449 | # AN output file is required. We cannot just print to STDOUT | ||
| 450 | my $o = $p->{out} || bailout( "No mo file to write to was specified." ); | ||
| 451 | $f = $opt->new_file( $f ); | ||
| 452 | my $po; | ||
| 453 | if( $f->extension() eq 'mo' ) | ||
| 454 | { | ||
| 455 | &bailout( "The source file \"$f\" is already a mo file. You can simply copy it yourself." ); | ||
| 456 | } | ||
| 457 | |||
| 458 | $p = | ||
| 459 | { | ||
| 460 | debug => $opts->{debug}, | ||
| 461 | # Since we are saving as compiled data, we need to resolve all include directives | ||
| 462 | include => 1, | ||
| 463 | ( $opts->{max_recurse} ? ( max_recurse => $opts->{max_recurse} ) : () ), | ||
| 464 | }; | ||
| 465 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 466 | |||
| 467 | if( $f->extension eq 'po' ) | ||
| 468 | { | ||
| 469 | $po = Text::PO->new( $p ) || bailout( Text::PO->error ); | ||
| 470 | $po = $po->parse( $f ); | ||
| 471 | bailout( "This does not look like a po file" ) if( !$po->elements->length ); | ||
| 472 | } | ||
| 473 | elsif( $f->extension eq 'json' ) | ||
| 474 | { | ||
| 475 | $p->{use_json} = 1; | ||
| 476 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 477 | $po->parse2object( $f ) || bailout( $po->error ); | ||
| 478 | } | ||
| 479 | else | ||
| 480 | { | ||
| 481 | bailout( "Unknown source file \"$f\"" ); | ||
| 482 | } | ||
| 483 | # Exchange a string for a Module::Generic::File object | ||
| 484 | $o = $opt->new_file( $o ); | ||
| 485 | _message( 3, "Saving data to mo file \"$o\"." ); | ||
| 486 | my $mo = Text::PO::MO->new( file => $o, debug => $opts->{debug} ); | ||
| 487 | $o->parent->mkpath; | ||
| 488 | $mo->write( $po ) || bailout( "Unable to write to \"$o\": ", $mo->error ); | ||
| 489 | return(1); | ||
| 490 | } | ||
| 491 | |||
| 492 | sub init_po | ||
| 493 | { | ||
| 494 | my $out = shift( @_ ); | ||
| 495 | # If no output has been specified, we print to STDOUT | ||
| 496 | $out = $opt->new_file( $out ) if( defined( $out ) ); | ||
| 497 | if( defined( $out ) && | ||
| 498 | $out->exists && | ||
| 499 | !$opts->{overwrite} ) | ||
| 500 | { | ||
| 501 | bailout( "An output file with the same name \"$out\" already exists. If you want to overwrite it, please use the --overwrite option" ); | ||
| 502 | } | ||
| 503 | if( !$opts->{lang} ) | ||
| 504 | { | ||
| 505 | bailout( "No language code was specified." ); | ||
| 506 | } | ||
| 507 | elsif( !$opts->{domain} ) | ||
| 508 | { | ||
| 509 | bailout( "No domain for the po file was provided." ); | ||
| 510 | } | ||
| 511 | |||
| 512 | my $fields = [qw( bugs_to charset created_on encoding header lang project revised_on team translator tz version )]; | ||
| 513 | my $maps = | ||
| 514 | { | ||
| 515 | bugs_to => 'Report-Msgid-Bugs-To', | ||
| 516 | created_on => 'POT-Creation-Date', | ||
| 517 | revised_on => 'PO-Revision-Date', | ||
| 518 | translator => 'Last-Translator', | ||
| 519 | team => 'Language-Team', | ||
| 520 | lang => 'Language', | ||
| 521 | plural => 'Plural-Forms', | ||
| 522 | content_Type => 'Content-Type', | ||
| 523 | transfer_encoding => 'Content-Transfer-Encoding', | ||
| 524 | }; | ||
| 525 | |||
| 526 | if( $opts->{settings} ) | ||
| 527 | { | ||
| 528 | my $f = $opt->new_file( $opts->{settings} ); | ||
| 529 | bailout( "Settings json file specified \"$opts->{settings}\" does not exist." ) if( !$f->exists ); | ||
| 530 | local $@; | ||
| 531 | # try-catch | ||
| 532 | my $json = eval | ||
| 533 | { | ||
| 534 | my $data = $f->load; | ||
| 535 | my $j = JSON->new->utf8->relaxed; | ||
| 536 | $j->decode( $data ); | ||
| 537 | }; | ||
| 538 | if( $@ ) | ||
| 539 | { | ||
| 540 | warn( "An error occurred while trying to decode json data from file \"$opts->{settings}\": $@\n" ); | ||
| 541 | return; | ||
| 542 | } | ||
| 543 | # Make sure all fields are normalised | ||
| 544 | foreach my $k ( keys( %$json ) ) | ||
| 545 | { | ||
| 546 | ( my $k2 = $k ) =~ tr/-/_/; | ||
| 547 | $json->{ $k2 } = CORE::delete( $json->{ $k } ); | ||
| 548 | } | ||
| 549 | |||
| 550 | foreach my $k ( @$fields ) | ||
| 551 | { | ||
| 552 | # command line options take priority | ||
| 553 | next if( defined( $opts->{ $k } ) && length( $opts->{ $k } ) ); | ||
| 554 | $opts->{ $k } = $json->{ $k } if( exists( $json->{ $k } ) ); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | my $po = Text::PO->new( | ||
| 559 | debug => $opts->{debug}, | ||
| 560 | # We just want to instantiate a PO, so since we may read from a POT, we do not want to process include directives. | ||
| 561 | include => 0, | ||
| 562 | ); | ||
| 563 | if( $opts->{pot} ) | ||
| 564 | { | ||
| 565 | my $pot = $opt->new_file( $opts->{pot} ); | ||
| 566 | bailout( "The pot file specified \"$pot\" does not exist." ) if( !$pot->exists ); | ||
| 567 | $po->parse( $pot ) || | ||
| 568 | bailout( "Error while reading pot file \"$pot\": ", $po->error ); | ||
| 569 | |||
| 570 | } | ||
| 571 | if( $opts->{header} ) | ||
| 572 | { | ||
| 573 | local $Text::Wrap::columns = 80; | ||
| 574 | my $lines = [split( /\n/, $opts->{header} )]; | ||
| 575 | for( my $i = 0; $i < scalar( @$lines ); $i++ ) | ||
| 576 | { | ||
| 577 | substr( $lines->[$i], 0, 0, '# ' ) unless( substr( $lines->[$i], 0, 1 ) eq '#' ); | ||
| 578 | if( length( $lines->[$i] ) > 80 ) | ||
| 579 | { | ||
| 580 | my $new = Text::Wrap::wrap( '', '', $lines->[$i] ); | ||
| 581 | my $newLines = [split( /\n/, $new )]; | ||
| 582 | splice( @$lines, $i, 1, @$newLines ); | ||
| 583 | $i += scalar( @$newLines ) - 1; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | $po->header( $lines ); | ||
| 587 | } | ||
| 588 | |||
| 589 | my $vers = $opts->{version} ? $opts->{version} : '1.0'; | ||
| 590 | $po->meta( 'Project-Id-Version' => sprintf( '%s %.1f', ( $opts->{project} || 'PROJECT' ), $vers ) ); | ||
| 591 | if( $opts->{charset} ) | ||
| 592 | { | ||
| 593 | $po->meta( content_type => sprintf( 'text/plain; charset=%s', ( $opts->{charset} || 'utf-8' ) ) ); | ||
| 594 | } | ||
| 595 | my $plur = &_get_plural_rule( $opts->{lang} ); | ||
| 596 | if( defined( $plur ) && | ||
| 597 | ref( $plur ) eq 'ARRAY' && | ||
| 598 | scalar( @$plur ) ) | ||
| 599 | { | ||
| 600 | $po->meta( $maps->{plural} => sprintf( 'nplurals=%d; plural=%s;', @$plur ) ); | ||
| 601 | } | ||
| 602 | # Should we fallback, or simply not add any 'Plural-Forms' header at all ? | ||
| 603 | else | ||
| 604 | { | ||
| 605 | # Fallback to 'und' | ||
| 606 | $po->meta( $maps->{plural} => q{nplurals=1; plural=0;} ); | ||
| 607 | } | ||
| 608 | $po->domain( $opts->{domain} ) if( $opts->{domain} ); | ||
| 609 | foreach my $t ( qw( created_on revised_on ) ) | ||
| 610 | { | ||
| 611 | my $dt; | ||
| 612 | if( $opts->{ $t } ) | ||
| 613 | { | ||
| 614 | $dt = $opts->{ $t }; | ||
| 615 | } | ||
| 616 | else | ||
| 617 | { | ||
| 618 | $dt = DateTime->now( time_zone => ( $opts->{tz} || 'local' ) ); | ||
| 619 | } | ||
| 620 | $po->meta( $maps->{ $t } => $dt->strftime( '%F %T%z' ) ); | ||
| 621 | } | ||
| 622 | |||
| 623 | foreach my $k ( @$fields ) | ||
| 624 | { | ||
| 625 | next unless( length( $opts->{ $k } ) ); | ||
| 626 | if( !exists( $maps->{ $k } ) ) | ||
| 627 | { | ||
| 628 | # warn( "Field \"$k\" does not exist in our map table. This is a bug.\n" ); | ||
| 629 | next; | ||
| 630 | } | ||
| 631 | $po->meta( $maps->{ $k } => $opts->{ $k } ); | ||
| 632 | } | ||
| 633 | |||
| 634 | $po->dump if( $opts->{debug} ); | ||
| 635 | |||
| 636 | my $binmode = ( $opts->{charset} || 'utf-8' ); | ||
| 637 | $binmode = 'utf8' if( lc( $binmode ) eq 'utf-8' ); | ||
| 638 | my $fh; | ||
| 639 | if( defined( $out ) ) | ||
| 640 | { | ||
| 641 | $fh = $out->open( '>', { binmode => $binmode } ) || bailout( "Unable to open the output file in write mode: ", $out->error ); | ||
| 642 | } | ||
| 643 | else | ||
| 644 | { | ||
| 645 | $fh = IO::File->new; | ||
| 646 | $fh->fdopen( fileno( STDOUT ), 'w' ); | ||
| 647 | $fh->binmode( ":utf8" ); | ||
| 648 | } | ||
| 649 | $fh->autoflush(1); | ||
| 650 | $po->dump( $fh ); | ||
| 651 | $fh->close if( defined( $out ) ); | ||
| 652 | return(1); | ||
| 653 | } | ||
| 654 | |||
| 655 | # po.pl --pp input.po > normalised.po | ||
| 656 | # po.pl --pp --output normalised.po input.po | ||
| 657 | # po.pl --pp --overwrite input.po | ||
| 658 | # We take a file in and save it with all its include directives resolved | ||
| 659 | sub pp | ||
| 660 | { | ||
| 661 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 662 | my $f = $p->{in} || bailout( "No po file to pre-process was specified." ); | ||
| 663 | # If no output is specified, we print to STDOUT | ||
| 664 | my $o = $p->{out}; | ||
| 665 | $o = $opt->new_file( $o ) if( defined( $o ) ); | ||
| 666 | if( defined( $o ) && | ||
| 667 | $o->exists && | ||
| 668 | !$opts->{overwrite} ) | ||
| 669 | { | ||
| 670 | bailout( "An output file with the same name \"$o\" already exists. If you want to overwrite it, please use the --overwrite option" ); | ||
| 671 | } | ||
| 672 | $f = $opt->new_file( $f ); | ||
| 673 | $p = | ||
| 674 | { | ||
| 675 | debug => $opts->{debug}, | ||
| 676 | # Since we need to pre-process the include directives, we need to enable it. | ||
| 677 | include => 1, | ||
| 678 | ( $opts->{max_recurse} ? ( max_recurse => $opts->{max_recurse} ) : () ), | ||
| 679 | }; | ||
| 680 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 681 | if( $f->extension() ne 'po' ) | ||
| 682 | { | ||
| 683 | &bailout( "The source file \"$f\" is not a po file. Pre-processing only works on PO files." ); | ||
| 684 | } | ||
| 685 | my $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 686 | _messagec( 3, "Pre-processing po file <green>$f</>" ); | ||
| 687 | $po->parse( $f ) || bailout( $po->error ); | ||
| 688 | |||
| 689 | my $fh; | ||
| 690 | if( defined( $o ) ) | ||
| 691 | { | ||
| 692 | _messagec( 3, "Saving as PO file to <green>${o}</>" ); | ||
| 693 | $o->parent->mkpath if( !$o->parent->exists ); | ||
| 694 | $fh = $o->open( '>', { binmode => 'utf8' } ) || bailout( "Unable to open the output file in write mode: ", $o->error ); | ||
| 695 | $po->dump( $fh ); | ||
| 696 | $fh->close; | ||
| 697 | } | ||
| 698 | else | ||
| 699 | { | ||
| 700 | $fh = IO::File->new; | ||
| 701 | $fh->fdopen( fileno( STDOUT ), 'w' ); | ||
| 702 | $fh->binmode( ":utf8" ); | ||
| 703 | } | ||
| 704 | $fh->autoflush(1); | ||
| 705 | $po->dump( $fh ); | ||
| 706 | $fh->close if( defined( $o ) ); | ||
| 707 | return(1); | ||
| 708 | } | ||
| 709 | |||
| 710 | sub sync | ||
| 711 | { | ||
| 712 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 713 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 714 | my $o = $p->{out} || bailout( "No mo file to write to was specified." ); | ||
| 715 | $f = $opt->new_file( $f ); | ||
| 716 | my $po; | ||
| 717 | $p = | ||
| 718 | { | ||
| 719 | debug => $opts->{debug}, | ||
| 720 | # By synchronising PO files, we are in effect merely editing them, so we do not need to process includes. | ||
| 721 | include => 0, | ||
| 722 | }; | ||
| 723 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 724 | if( $f->extension eq 'po' ) | ||
| 725 | { | ||
| 726 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 727 | _messagec( 3, "Reading po file <green>$f</>" ); | ||
| 728 | $po->parse( $f ) || bailout( $po->error ); | ||
| 729 | } | ||
| 730 | elsif( $f->extension eq 'mo' ) | ||
| 731 | { | ||
| 732 | # The option 'include' is not supported by Text::PO::MO | ||
| 733 | delete( $p->{include} ); | ||
| 734 | $p->{file} = $f; | ||
| 735 | my $mo = Text::PO::MO->new( %$p ); | ||
| 736 | _messagec( 3, "Reading mo file <green>$f</>" ); | ||
| 737 | $po = $mo->as_object; | ||
| 738 | } | ||
| 739 | elsif( $f->extension eq 'json' ) | ||
| 740 | { | ||
| 741 | $p->{use_json} = 1; | ||
| 742 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 743 | _messagec( 3, "Reading json po file <green>$f</>" ); | ||
| 744 | $po->parse2object( $f ) || bailout( $po->error ); | ||
| 745 | } | ||
| 746 | _messagec( 3, "Synchronising against po file <green>$o</>" ); | ||
| 747 | $po->sync( $o ) || bailout( $po->error ); | ||
| 748 | if( $po->debug ) | ||
| 749 | { | ||
| 750 | my $added = $po->added; | ||
| 751 | my $removed = $po->removed; | ||
| 752 | if( scalar( @$added ) ) | ||
| 753 | { | ||
| 754 | _message( 3, "The following ", scalar( @$added ), " element(s) were added:\n", join( "\n\n", @$added ) ); | ||
| 755 | } | ||
| 756 | else | ||
| 757 | { | ||
| 758 | _message( 3, "No element were added." ); | ||
| 759 | } | ||
| 760 | |||
| 761 | if( scalar( @$removed ) ) | ||
| 762 | { | ||
| 763 | _message( 3, "The following ", scalar( @$removed ), " element(s) were removed:\n", join( "\n\n", @$removed ) ); | ||
| 764 | } | ||
| 765 | else | ||
| 766 | { | ||
| 767 | _message( 3, "No element were removed." ); | ||
| 768 | } | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | sub to_json | ||
| 773 | { | ||
| 774 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 775 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 776 | # If no output is specified, we print to STDOUT | ||
| 777 | my $o = $p->{out}; | ||
| 778 | $o = $opt->new_file( $o ) if( defined( $o ) ); | ||
| 779 | if( defined( $o ) && | ||
| 780 | $o->exists && | ||
| 781 | !$opts->{overwrite} ) | ||
| 782 | { | ||
| 783 | bailout( "An output file with the same name \"$o\" already exists. If you want to overwrite it, please use the --overwrite option" ); | ||
| 784 | } | ||
| 785 | $f = $opt->new_file( $f ); | ||
| 786 | my $po; | ||
| 787 | $p = | ||
| 788 | { | ||
| 789 | debug => $opts->{debug}, | ||
| 790 | # Because we are saving as JSON, and there are no special include directives in JSON, we must enable processing of include directives in PO file. | ||
| 791 | include => 1, | ||
| 792 | ( $opts->{max_recurse} ? ( max_recurse => $opts->{max_recurse} ) : () ), | ||
| 793 | }; | ||
| 794 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 795 | if( $f->extension() eq 'json' ) | ||
| 796 | { | ||
| 797 | &bailout( "The source file \"$f\" is already a json file. You can simply copy it yourself." ); | ||
| 798 | } | ||
| 799 | elsif( $f->extension eq 'mo' ) | ||
| 800 | { | ||
| 801 | # There is no 'include' option in Text::PO::MO | ||
| 802 | delete( $p->{include} ); | ||
| 803 | $p->{file} = $f; | ||
| 804 | my $mo = Text::PO::MO->new( %$p ); | ||
| 805 | $po = $mo->as_object; | ||
| 806 | } | ||
| 807 | elsif( $f->extension eq 'po' ) | ||
| 808 | { | ||
| 809 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 810 | $po->parse( $f ) || bailout( $po->error ); | ||
| 811 | } | ||
| 812 | else | ||
| 813 | { | ||
| 814 | bailout( "Unknown source file \"$f\"" ); | ||
| 815 | } | ||
| 816 | |||
| 817 | my $json = $po->as_json({ pretty => 1, canonical => 1 }); | ||
| 818 | _messagec( 3, "<red>", $po->error, "</>" ) if( !$json ); | ||
| 819 | my $fh; | ||
| 820 | if( defined( $o ) ) | ||
| 821 | { | ||
| 822 | _messagec( 3, "<green>", $po->elements->length, "</> elements found." ); | ||
| 823 | _messagec( 3, "Saving as json file to <green>${o}</>" ); | ||
| 824 | $o->parent->mkpath if( !$o->parent->exists ); | ||
| 825 | $fh = $o->open( '>', { binmode => ':utf8' }) || bailout( "Unable to open output file \"$o\" in write mode: $!" ); | ||
| 826 | } | ||
| 827 | else | ||
| 828 | { | ||
| 829 | $fh = IO::File->new; | ||
| 830 | $fh->fdopen( fileno( STDOUT ), 'w' ); | ||
| 831 | $fh->binmode( ":utf8" ); | ||
| 832 | } | ||
| 833 | $fh->autoflush(1); | ||
| 834 | # _message( 3, "Saving json '$json'" ); | ||
| 835 | $fh->print( $json ); | ||
| 836 | $fh->close if( defined( $o ) ); | ||
| 837 | return(1); | ||
| 838 | } | ||
| 839 | |||
| 840 | sub to_po | ||
| 841 | { | ||
| 842 | my $p = $opt->_get_args_as_hash( @_ ); | ||
| 843 | my $f = $p->{in} || bailout( "No po file to read was specified." ); | ||
| 844 | # If no output is specified, we print to STDOUT | ||
| 845 | my $o = $p->{out}; | ||
| 846 | $o = $opt->new_file( $o ) if( defined( $o ) ); | ||
| 847 | if( defined( $o ) && | ||
| 848 | $o->exists && | ||
| 849 | !$opts->{overwrite} ) | ||
| 850 | { | ||
| 851 | bailout( "An output file with the same name \"$o\" already exists. If you want to overwrite it, please use the --overwrite option" ); | ||
| 852 | } | ||
| 853 | $f = $opt->new_file( $f ); | ||
| 854 | my $po; | ||
| 855 | $p = | ||
| 856 | { | ||
| 857 | debug => $opts->{debug}, | ||
| 858 | include => 0, | ||
| 859 | }; | ||
| 860 | $p->{domain} = $opts->{domain} if( length( $opts->{domain} ) ); | ||
| 861 | if( $f->extension() eq 'po' ) | ||
| 862 | { | ||
| 863 | &bailout( "The source file \"$f\" is already a po file. You can simply copy it yourself." ); | ||
| 864 | } | ||
| 865 | elsif( $f->extension eq 'mo' ) | ||
| 866 | { | ||
| 867 | # The option 'include' is not supported by Text::PO::MO | ||
| 868 | delete( $p->{include} ); | ||
| 869 | $p->{file} = $f; | ||
| 870 | my $mo = Text::PO::MO->new( %$p ); | ||
| 871 | $po = $mo->as_object || _messagec( 3, "<red>", $mo->error, "</>" ); | ||
| 872 | } | ||
| 873 | elsif( $f->extension eq 'json' ) | ||
| 874 | { | ||
| 875 | $p->{use_json} = 1; | ||
| 876 | $po = Text::PO->new( %$p ) || bailout( Text::PO->error ); | ||
| 877 | $po->parse2object( $f ) || bailout( $po->error ); | ||
| 878 | } | ||
| 879 | else | ||
| 880 | { | ||
| 881 | bailout( "Unknown source file \"$f\"" ); | ||
| 882 | } | ||
| 883 | |||
| 884 | my $fh; | ||
| 885 | if( defined( $o ) ) | ||
| 886 | { | ||
| 887 | _messagec( 3, "<green>", $po->elements->length, "</> elements found." ); | ||
| 888 | _messagec( 3, "Saving as PO data file to <green>${o}</>" ); | ||
| 889 | $o->parent->mkpath if( !$o->parent->exists ); | ||
| 890 | $fh = $o->open( '>', { binmode => ':utf8' }) || bailout( "Unable to open output file \"$o\" in write mode: $!" ); | ||
| 891 | } | ||
| 892 | else | ||
| 893 | { | ||
| 894 | $fh = IO::File->new; | ||
| 895 | $fh->fdopen( fileno( STDOUT ), 'w' ); | ||
| 896 | $fh->binmode( ":utf8" ); | ||
| 897 | } | ||
| 898 | $fh->autoflush(1); | ||
| 899 | $po->dump( $fh ); | ||
| 900 | $fh->close if( defined( $o ) ); | ||
| 901 | return(1); | ||
| 902 | } | ||
| 903 | |||
| 904 | sub _get_plural_rule | ||
| 905 | { | ||
| 906 | my( $locale ) = @_; | ||
| 907 | # Try to use Locale::Unicode::Data if it is available | ||
| 908 | local $@; | ||
| 909 | # try-catch | ||
| 910 | eval | ||
| 911 | { | ||
| 912 | require Locale::Unicode::Data; | ||
| 913 | }; | ||
| 914 | if( $@ ) | ||
| 915 | { | ||
| 916 | my $ref; | ||
| 917 | if( exists( $PLURALS->{ $locale } ) ) | ||
| 918 | { | ||
| 919 | $ref = $PLURALS->{ $locale }; | ||
| 920 | } | ||
| 921 | elsif( exists( $PLURALS->{ substr( $locale, 0, 2 ) } ) ) | ||
| 922 | { | ||
| 923 | $ref = $PLURALS->{ substr( $locale, 0, 2 ) }; | ||
| 924 | } | ||
| 925 | else | ||
| 926 | { | ||
| 927 | warn( "Unknow locale \"$locale\" to find out about its plural form\n" ); | ||
| 928 | return; | ||
| 929 | } | ||
| 930 | return( $ref ); | ||
| 931 | } | ||
| 932 | else | ||
| 933 | { | ||
| 934 | my $cldr = Locale::Unicode::Data->new || bailout( Locale::Unicode::Data->error ); | ||
| 935 | my $rule = $cldr->plural_forms( $locale ) || bailout( $cldr->error ); | ||
| 936 | # e.g.: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5); | ||
| 937 | my( $n, $expr ) = split( /[[:blank:]]*\;[[:blank:]]*/, $rule, 2 ); | ||
| 938 | my $token; | ||
| 939 | ( $token, $n ) = split( /[[:blank:]]*=[[:blank:]]*/, $n, 2 ); | ||
| 940 | ( $token, $expr ) = split( /[[:blank:]]*=[[:blank:]]*/, $expr, 2 ); | ||
| 941 | return( [$n, $expr] ); | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | sub _load_plurals | ||
| 946 | { | ||
| 947 | # Ref: <http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html> | ||
| 948 | # <https://www.fincher.org/Utilities/CountryLanguageList.shtml> | ||
| 949 | # <http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html> | ||
| 950 | our $PLURALS = | ||
| 951 | { | ||
| 952 | # Afrikaans | ||
| 953 | af => [2, "(n != 1)"], | ||
| 954 | # Akan | ||
| 955 | ak => [2, "(n > 1)"], | ||
| 956 | # Aragonese | ||
| 957 | an => [2, "(n != 1)"], | ||
| 958 | # Arabic | ||
| 959 | ar => [6, "n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5"], | ||
| 960 | # Assamese | ||
| 961 | as => [2, "(n != 1)"], | ||
| 962 | # Aymará | ||
| 963 | ay => [2, 0], | ||
| 964 | # Azerbaijani | ||
| 965 | az => [2, "(n != 1)"], | ||
| 966 | # Belarusian | ||
| 967 | be => [2, "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"], | ||
| 968 | # Belarusian | ||
| 969 | be_BY => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 970 | # Bulgarian | ||
| 971 | bg => [2, "(n != 1)"], | ||
| 972 | # Bengali | ||
| 973 | bn => [2, ""], | ||
| 974 | # Tibetan | ||
| 975 | bo => [1,0], | ||
| 976 | # Breton | ||
| 977 | br => [2, "(n > 1)"], | ||
| 978 | # Bosnian | ||
| 979 | bs => [3, "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"], | ||
| 980 | # Catalan | ||
| 981 | ca => [2, "(n != 1)"], | ||
| 982 | # Czech | ||
| 983 | cs_CZ => [3, "plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2"], | ||
| 984 | # Slavic Bulgarian | ||
| 985 | cu_BG => [2, "n != 1"], | ||
| 986 | # Welsh | ||
| 987 | cy => [4, "(n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3"], | ||
| 988 | # Danish | ||
| 989 | da_DK => [2, "n != 1"], | ||
| 990 | de => [2, "n != 1"], | ||
| 991 | de_DE => [2, "n != 1"], | ||
| 992 | # Dzongkha | ||
| 993 | dz => [1,0], | ||
| 994 | # Greece | ||
| 995 | el_GR => [2, "n != 1"], | ||
| 996 | en => [2, "n != 1"], | ||
| 997 | en_GB => [2, "n != 1"], | ||
| 998 | en_US => [2, "n != 1"], | ||
| 999 | # Esperanto | ||
| 1000 | eo => [2, "n != 1"], | ||
| 1001 | es => [2, "n != 1"], | ||
| 1002 | es_ES => [2, "n != 1"], | ||
| 1003 | # Estonian | ||
| 1004 | et_EE => [2, "n != 1"], | ||
| 1005 | # Basque | ||
| 1006 | eu => [2, "(n != 1)"], | ||
| 1007 | # Persian | ||
| 1008 | fa => [2, "(n > 1)"], | ||
| 1009 | # Fulah | ||
| 1010 | ff => [2, "(n != 1)"], | ||
| 1011 | # Finland | ||
| 1012 | fi_FI => [2, "n != 1"], | ||
| 1013 | # Faroese | ||
| 1014 | fo_FO => [2, "n != 1"], | ||
| 1015 | fr => [2, "n>1"], | ||
| 1016 | fr_FR => [2, "n>1"], | ||
| 1017 | # Frisian | ||
| 1018 | fy => [2, "(n != 1)"], | ||
| 1019 | # Irish in UK | ||
| 1020 | ga_GB => [3, "n==1 ? 0 : n==2 ? 1 : 2"], | ||
| 1021 | # Irish in Ireland | ||
| 1022 | ga_IE => [3, "n==1 ? 0 : n==2 ? 1 : 2"], | ||
| 1023 | # Galician | ||
| 1024 | gl => [2, "(n != 1)"], | ||
| 1025 | # Gujarati | ||
| 1026 | gu => [2, "(n != 1)"], | ||
| 1027 | # Hausa | ||
| 1028 | ha => [2, "(n != 1)"], | ||
| 1029 | he_IL => [2, "n != 1"], | ||
| 1030 | # Hindi | ||
| 1031 | hi => [2, "(n != 1)"], | ||
| 1032 | # Croatian | ||
| 1033 | hr_HR => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1034 | # Hungarian (Finno-Ugric family) | ||
| 1035 | hu_HU => [2, "n != 1"], | ||
| 1036 | # Armenian | ||
| 1037 | hy => [2, "(n != 1)"], | ||
| 1038 | # Interlingua | ||
| 1039 | ia => [2, "(n != 1)"], | ||
| 1040 | # Bahasa Indonesian | ||
| 1041 | id_ID => [2, "n != 1"], | ||
| 1042 | # Icelandic | ||
| 1043 | is => [2, "(n%10!=1 || n%100==11)"], | ||
| 1044 | it => [2, "n != 1"], | ||
| 1045 | it_IT => [2, "n != 1"], | ||
| 1046 | ja_JP => [1, 0], | ||
| 1047 | # Javanese | ||
| 1048 | jv => [2, "(n != 0)"], | ||
| 1049 | # Kazakh | ||
| 1050 | kk => [2, "(n != 1)"], | ||
| 1051 | # Greenlandic | ||
| 1052 | kl => [2, "(n != 1)"], | ||
| 1053 | # Khmer | ||
| 1054 | km => [1, 0], | ||
| 1055 | # Kannada | ||
| 1056 | kn => [2, "(n != 1)"], | ||
| 1057 | ko_KR => [1, 0], | ||
| 1058 | # Kurdish | ||
| 1059 | ku => [2, "(n != 1)"], | ||
| 1060 | # Cornish | ||
| 1061 | kw => [4, "(n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3"], | ||
| 1062 | # Kyrgyz | ||
| 1063 | ky => [2, "(n != 1)"], | ||
| 1064 | # Letzeburgesch | ||
| 1065 | lb => [2, "(n != 1)"], | ||
| 1066 | # Lingala | ||
| 1067 | ln => [2, "(n > 1)"], | ||
| 1068 | # Lao | ||
| 1069 | lo => [1, 0], | ||
| 1070 | # Lithuanian (Baltic family) | ||
| 1071 | lt_LT => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1072 | # Latvia | ||
| 1073 | lv_LV => [3, "n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2"], | ||
| 1074 | # Montenegro | ||
| 1075 | me => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1076 | # Malagasy | ||
| 1077 | mg => [2, "(n > 1)"], | ||
| 1078 | # Maori | ||
| 1079 | mi => [2, "(n > 1)"], | ||
| 1080 | # Macedonian | ||
| 1081 | mk => [2, "n==1 || n%10==1 ? 0 : 1"], | ||
| 1082 | # Malayalam | ||
| 1083 | ml => [2, "(n != 1)"], | ||
| 1084 | # Mongolian | ||
| 1085 | mn => [2, "(n != 1)"], | ||
| 1086 | # Marathi | ||
| 1087 | mr => [2, "(n != 1)"], | ||
| 1088 | # Malay | ||
| 1089 | ms => [1, 0], | ||
| 1090 | # Maltese | ||
| 1091 | mt => [4, "(n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3)"], | ||
| 1092 | # Burmese | ||
| 1093 | my => [1, 0], | ||
| 1094 | # Norwegian Bokmal | ||
| 1095 | nb => [2, "(n != 1)"], | ||
| 1096 | # Nepali | ||
| 1097 | ne => [2, "(n != 1)"], | ||
| 1098 | nl => [2, "n != 1"], | ||
| 1099 | nl_NL => [2, "n != 1"], | ||
| 1100 | # Norwegian Nynorsk | ||
| 1101 | nn => [2, "(n != 1)"], | ||
| 1102 | # Norwegian | ||
| 1103 | no_NO => [2, "n != 1"], | ||
| 1104 | # Occitan | ||
| 1105 | oc => [2, "(n > 1)"], | ||
| 1106 | # Oriya | ||
| 1107 | or => [2, "(n != 1)"], | ||
| 1108 | # Punjabi | ||
| 1109 | pa => [2, "(n != 1)"], | ||
| 1110 | # Polish | ||
| 1111 | pl_PL => [3, "n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1112 | # Pashto | ||
| 1113 | ps => [2, "(n != 1)"], | ||
| 1114 | # Brazilian Portugese | ||
| 1115 | pt => [2, "n != 1"], | ||
| 1116 | pt_BR => [2, "n>1"], | ||
| 1117 | pt_PT => [2, "n != 1"], | ||
| 1118 | # Romansh | ||
| 1119 | rm => [2, "(n != 1)"], | ||
| 1120 | # Romanian | ||
| 1121 | ro_RO => [3, "n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2"], | ||
| 1122 | # Russian | ||
| 1123 | ru => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1124 | ru_RU => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1125 | # Kinyarwanda | ||
| 1126 | rw => [2, "(n != 1)"], | ||
| 1127 | # Sindhi | ||
| 1128 | sd => [2, "(n != 1)"], | ||
| 1129 | # Northern Sami | ||
| 1130 | se => [2, "(n != 1)"], | ||
| 1131 | # Sinhala | ||
| 1132 | si => [2, "(n != 1)"], | ||
| 1133 | # Slovak | ||
| 1134 | sk_SK => [3, "plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2"], | ||
| 1135 | # Slovenian | ||
| 1136 | sl_SI => [4, "n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3"], | ||
| 1137 | # Somali | ||
| 1138 | so => [2, "(n != 1)"], | ||
| 1139 | # Albanian | ||
| 1140 | sq => [2, "(n != 1)"], | ||
| 1141 | # Serbian | ||
| 1142 | sr_RS => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1143 | # Sundanese | ||
| 1144 | su => [1, 0], | ||
| 1145 | # Sweden | ||
| 1146 | sv => [2, "n != 1"], | ||
| 1147 | sv_SE => [2, "n != 1"], | ||
| 1148 | # Swedish | ||
| 1149 | sw => [2, "(n != 1)"], | ||
| 1150 | # Tamil | ||
| 1151 | ta => [2, "(n != 1)"], | ||
| 1152 | # Telugu | ||
| 1153 | te => [2, "(n != 1)"], | ||
| 1154 | # Tajik | ||
| 1155 | tg => [2, "(n > 1);"], | ||
| 1156 | th_TH => [1, 0], | ||
| 1157 | # Tigrinya | ||
| 1158 | ti => [2, "(n > 1)"], | ||
| 1159 | # Turkmen | ||
| 1160 | tk => [2, "(n != 1)"], | ||
| 1161 | # Turkey | ||
| 1162 | tr_TR => [2, "n != 1"], | ||
| 1163 | # Tatar | ||
| 1164 | tt => [1, 0], | ||
| 1165 | # Uyghur | ||
| 1166 | ug => [1, 0], | ||
| 1167 | # Ukrainian | ||
| 1168 | uk_UA => [3, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2"], | ||
| 1169 | # Urdu | ||
| 1170 | ur => [2, "(n != 1)"], | ||
| 1171 | # Uzbek | ||
| 1172 | uz => [2, "(n > 1)"], | ||
| 1173 | # Vietnamese | ||
| 1174 | vi_VN => [1, 0], | ||
| 1175 | # Walloon | ||
| 1176 | wa => [2, "(n > 1)"], | ||
| 1177 | # Wolof | ||
| 1178 | wo => [1, 0], | ||
| 1179 | # Yoruba | ||
| 1180 | yo => [2, "(n != 1)"], | ||
| 1181 | # Chinese | ||
| 1182 | zh => [1, 0], | ||
| 1183 | }; | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | sub _message | ||
| 1187 | { | ||
| 1188 | my $required_level; | ||
| 1189 | if( $_[0] =~ /^\d{1,2}$/ ) | ||
| 1190 | { | ||
| 1191 | $required_level = shift( @_ ); | ||
| 1192 | } | ||
| 1193 | else | ||
| 1194 | { | ||
| 1195 | $required_level = 0; | ||
| 1196 | } | ||
| 1197 | return if( !$LOG_LEVEL || $LOG_LEVEL < $required_level ); | ||
| 1198 | my $msg = join( '', map( ref( $_ ) eq 'CODE' ? $_->() : $_, @_ ) ); | ||
| 1199 | my $frame = 0; | ||
| 1200 | $frame++ if( (caller(1))[3] =~ /_messagec/ ); | ||
| 1201 | my( $pkg, $file, $line ) = caller( $frame ); | ||
| 1202 | my $sub = ( caller( $frame + 1 ) )[3]; | ||
| 1203 | my $sub2 = substr( $sub, rindex( $sub, '::' ) + 2 ); | ||
| 1204 | return( $err->print( "${pkg}::${sub2}() [$line]: $msg\n" ) ); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | sub _messagec | ||
| 1208 | { | ||
| 1209 | my $required_level; | ||
| 1210 | if( $_[0] =~ /^\d{1,2}$/ ) | ||
| 1211 | { | ||
| 1212 | $required_level = shift( @_ ); | ||
| 1213 | } | ||
| 1214 | else | ||
| 1215 | { | ||
| 1216 | $required_level = 0; | ||
| 1217 | } | ||
| 1218 | return( _message( $required_level, $opt->colour_parse( @_ ) ) ); | ||
| 1219 | } | ||
| 1220 | # NOTE: POD | ||
| 1221 | __END__ |