Jifty

 view release on metacpan or  search on metacpan

lib/Jifty/Script/Po.pm  view on Meta::CPAN

    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::encode_json( { map { my $text = ${"Jifty::I18N::".$lang."::Lexicon"}{$_};
                                              defined $text ? ( $_ => $text ) : () }
                                        keys %{$LMExtract->lexicon} } );
    }
}

=head2 _check_mime_type FILENAME

This routine returns a mimetype for the file C<FILENAME>.

=cut

sub _check_mime_type {
    my $self       = shift;
    my $local_path = shift;
    my $mimeobj = $MIME->mimeTypeOf($local_path);
    my $mime_type = ($mimeobj ? $mimeobj->type : "unknown");
    return if ( $mime_type =~ /^image/ );
    return 1;
}

=head2 update_catalogs

Extracts localizable messages from all files in your application, finds
all your message catalogs and updates them with new and changed messages.

=cut

sub update_catalogs {
    my $self = shift;
    my $podir = $self->{'podir'} || Jifty->config->framework('L10N')->{'PoDir'};

    $self->extract_messages;
    $self->update_catalog( File::Spec->catfile(
            $podir, $self->pot_name . ".pot"
        ) );

    if ($self->{'language'}) {
        $self->update_catalog( File::Spec->catfile(
            $podir, $self->{'language'} . ".po"
        ) );
        return;
    }

    my @catalogs = grep !m{(^|/)\.svn/}, File::Find::Rule->file->name('*.po')->in(
        $podir
    );

    unless ( @catalogs ) {
        $self->log->error("You have no existing message catalogs.");
        $self->log->error("Run `jifty po --language <lang>` to create a new one.");
        $self->log->error("Read `jifty po --help` to get more info.");
        return 
    }

    foreach my $catalog (@catalogs) {
        $self->update_catalog( $catalog );
    }
}

=head2 update_catalog FILENAME

Reads C<FILENAME>, a message catalog and integrates new or changed 
translations.

=cut

sub update_catalog {
    my $self       = shift;
    my $translation = shift;
    my $logger =Log::Log4perl->get_logger("main");
    $logger->info( "Updating message catalog '$translation'");

    $LMExtract->read_po($translation) if ( -f $translation && $translation !~ m/pot$/ );

    my $orig_lexicon;

    # Reset previously compiled entries before a new compilation
    $LMExtract->set_compiled_entries;
    $LMExtract->compile(USE_GETTEXT_STYLE);

    if ($self->_is_core && !$self->{'template_name'}) {
        $LMExtract->write_po($translation);
    }
    else {
        $orig_lexicon = $LMExtract->lexicon;
        my $lexicon = { %$orig_lexicon };

        # XXX: cache core_lm
        my $core_lm = Locale::Maketext::Extract->new();
        Locale::Maketext::Lexicon::set_option('allow_empty' => 1);
        $core_lm->read_po( File::Spec->catfile(
            Jifty->config->framework('L10N')->{'DefaultPoDir'}, 'jifty.pot'
        ));
        Locale::Maketext::Lexicon::set_option('allow_empty' => 0);
        for (keys %{ $core_lm->lexicon }) {
            next unless exists $lexicon->{$_};
            # keep the local entry overriding core if it exists
            delete $lexicon->{$_} unless length $lexicon->{$_};
        }
        $LMExtract->set_lexicon($lexicon);

        $LMExtract->write_po($translation);

        $LMExtract->set_lexicon($orig_lexicon);
    }
}


=head2 extract_messages

Find all translatable messages in your application, using 
L<Locale::Maketext::Extract>.

=cut

sub extract_messages {
    my $self = shift;
    # find all the .pm files in @INC
    my @files = File::Find::Rule->file->in( @{ $self->{directories} ||
                                                   [ Jifty->config->framework('Web')->{'TemplateRoot'},
                                                     'lib', 'bin'] } );

    my $logger =Log::Log4perl->get_logger("main");
    foreach my $file (@files) {
        next if $file =~ m{(^|/)[\._]svn/};
        next if $file =~ m{\~$};
        next if $file =~ m{\.pod$};



( run in 1.092 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )