[jifty-devel] Re: [Jifty-commit] r4279 - jifty/trunk/lib/Jifty/Script

Jesse Vincent jesse at bestpractical.com
Sat Oct 20 06:27:05 EDT 2007


What's the use case for generating json files staticly at compile  
time  rather tahn at request time?
It seems likely to get us in a lot more trouble than doing this as  
custom web service.
-j


On Oct 20, 2007, at 10:15 AM, jifty-commit at lists.jifty.org wrote:

> Author: clkao
> Date: Sat Oct 20 05:15:33 2007
> New Revision: 4279
>
> Modified:
>    jifty/trunk/lib/Jifty/Script/Po.pm
>
> Log:
> bin/jifty po now takes two more options:
> * --dir for additional directories to look at so javascript
>   files can be scanned.
>
> * --js for generating json dictionaries for messages appeared
>   in javascript files declared with Jifty::Web.
>
>
> Modified: jifty/trunk/lib/Jifty/Script/Po.pm
> ====================================================================== 
> ========
> --- jifty/trunk/lib/Jifty/Script/Po.pm	(original)
> +++ jifty/trunk/lib/Jifty/Script/Po.pm	Sat Oct 20 05:15:33 2007
> @@ -5,6 +5,7 @@
>  use base qw(App::CLI::Command Class::Accessor::Fast);
>
>  use File::Copy ();
> +use File::Path 'mkpath';
>  use Jifty::Config ();
>  use Jifty::YAML ();
>  use Locale::Maketext::Extract ();
> @@ -28,14 +29,25 @@
>
>  =head2 options
>
> -This script only takes one option, C<--language>, which is  
> optional; it is
> -the name of a message catalog to create.
> +This script an option, C<--language>, which is optional; it is the
> +name of a message catalog to create.
> +
> +It also takes C<--dir> to specify additional directories to extract
> +from.
> +
> +If C<--js> is given, other options are ignored and the script will
> +generate json files for each language under
> +F<share/web/static/js/dict> from the current po files.  Before doing
> +so, you might want to run C<jifty po> with C<--dir share/web/ 
> static/js>
> +to include messages from javascript in your po files.
>
>  =cut
>
>  sub options {
>      (
>       'l|language=s' => 'language',
> +     'dir=s@'       => 'directories',
> +     'js'           => 'js',
>      )
>  }
>
> @@ -48,9 +60,46 @@
>
>
>  sub run {
> -        my $self = shift;
> -            Jifty->new(no_handle => 1);
> -        $self->update_catalogs;
> +    my $self = shift;
> +    Jifty->new(no_handle => 1);
> +
> +    return $self->_js_gen if $self->{js};
> +
> +    $self->update_catalogs;
> +}
> +
> +sub _js_gen {
> +    my $self = shift;
> +    my $static_handler = Jifty::View::Static::Handler->new;
> +    my $logger =Log::Log4perl->get_logger("main");
> +    for my $file ( @{ Jifty::Web->javascript_libs } ) {
> +        next if $file =~ m/^ext/;
> +        next if $file =~ m/^yui/;
> +        next if $file =~ m/^rico/;
> +        my $path = $static_handler->file_path( File::Spec->catdir 
> ( 'js', $file ) ) or next;
> +
> +        $logger->info("Extracting messages from '$path'");
> +
> +        $LMExtract->extract_file( $path );
> +    }
> +
> +    $LMExtract->set_compiled_entries;
> +    $LMExtract->compile(USE_GETTEXT_STYLE);
> +
> +    Jifty::I18N->new;
> +    mkpath ['share/web/static/js/dict'];
> +    for my $lang (Jifty::I18N->available_languages) {
> +        my $file = "share/web/static/js/dict/$lang.json";
> +        $logger->info("Generating $file");
> +        open my $fh, '>', $file or die "$file: $!";
> +
> +        no strict 'refs';
> +        print $fh
> +            Jifty::JSON::objToJson( { map { my $text = $ 
> {"Jifty::I18N::".$lang."::Lexicon"}{$_};
> +                                            defined $text ? ( $_  
> => $text ) : () }
> +                                      keys %{$LMExtract->lexicon} },
> +                                    { singlequote => 1 } );
> +    }
>  }
>
>  =head2 _check_mime_type FILENAME
> @@ -124,7 +173,7 @@
>  sub extract_messages {
>      my $self = shift;
>      # find all the .pm files in @INC
> -    my @files = File::Find::Rule->file->in( Jifty->config- 
> >framework('Web')->{'TemplateRoot'}, 'lib', 'bin' );
> +    my @files = File::Find::Rule->file->in( Jifty->config- 
> >framework('Web')->{'TemplateRoot'}, 'lib', 'bin', @{ $self-> 
> {directories} || [] } );
>
>      my $logger =Log::Log4perl->get_logger("main");
>      foreach my $file (@files) {
> _______________________________________________
> Jifty-commit mailing list
> Jifty-commit at lists.jifty.org
> http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-commit
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://lists.bestpractical.com/pipermail/jifty-devel/attachments/20071020/9303a0d4/PGP.pgp


More information about the jifty-devel mailing list