Locale::Maketext::Extract::DBI - Extract translation keys from a database


Locale-Maketext-Extract-DBI documentation Contained in the Locale-Maketext-Extract-DBI distribution.

Index


Code Index:

NAME

Top

Locale::Maketext::Extract::DBI - Extract translation keys from a database

SYNOPSIS

Top

    my $extractor = Locale::Maketext::Extract::DBI->new;
    $extract->extract( %options );

DESCRIPTION

Top

This module extracts translation keys from a database table.

METHODS

Top

new( )

Creates a new Locale::Maketext::Extract::DBI instance.

extract( %options )

The main method for extraction. Take a list of options to pass to Locale::Maketext::Extract and extract_dbi.

extract_dbi( $extractor, %options )

Connects to the database, runs the query and stuffs the results in to the $extractor.

AUTHOR

Top

* Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

* Locale::Maketext::Extract

Locale-Maketext-Extract-DBI documentation Contained in the Locale-Maketext-Extract-DBI distribution.
package Locale::Maketext::Extract::DBI;

use strict;
use warnings;

use Locale::Maketext::Extract;
use DBI;
use Cwd;

our $VERSION = '0.01';

sub new {
    my $class = shift;
    return bless {}, $class;
}

sub extract {
    my $self    = shift;
    my %options = @_;

    my $extractor = Locale::Maketext::Extract->new;
    my $output    = $options{ o } || ( $options{ d } || 'messages' ) . '.po' ;
    my $cwd       = getcwd;

    $extractor->read_po( $output ) if -r $output and -s _;
    $self->extract_dbi( $extractor, %options );
    $extractor->compile;
 
    chdir( $options{ p } || '.' );
    $extractor->write_po( $output );   
    chdir $cwd;
}

sub extract_dbi {
    my( $self, $extractor, %options ) = @_;
    
    my $dbh   = DBI->connect( ( map{ $options{ $_ } } qw( dsn username password ) ), { RaiseError => 1 } );
    my $query = $options{ query };
    
    my $results = $dbh->selectall_arrayref( $query );
    for( 0..@$results - 1 ) {
        $extractor->add_entry( $results->[ $_ ]->[ 0 ] => [ "dbi:$query", $_ + 1] );
    }
}

1;