NAME

Locale::TextDomain::OO::Extract - Extracts internationalization data

$Id: $

$HeadURL: $

VERSION

2.000

DESCRIPTION

This module extracts internationalization data.

The extractor runs the following steps:

* preprocess the file
* find all possible positions in content
* match exactly and create a stack
* map the stack to a lexicon entry

If you want to write the lexicon structure to pot files see folder example of this distribution how it works.

SYNOPSIS

There are different extract module for different files. All this extracted data are stored into one or selective lexicons. At the end of extraction this lexicons can be stored into pot files or anywhere else.

Use an existing extractor

use strict;
use warnings;
use Locale::TextDomain::OO::Extract::...;
use Path::Tiny qw(path);

my $extractor = Locale::TextDomain::OO::Extract::...->new(
    # all parameters are optional
    lexicon_ref => \my %lexicon,  # default is {}
    domain      => 'MyDomain',    # default is q{}
    category    => 'LC_MESSAGES', # default is q{}
    debug_code  => sub {          # default is undef
        my ($group, $message) = @_;
        print $group, ', ', $message, "\n";
        return;
    },
);

my @files = (
    'relative_dir/filename1.suffix',
    'relative_dir/filename2.suffix',
);
for ( @files ) {
    $extractor->clear;
    $extractor->filename($_);
    $extractor->content_ref( \( path($_)->slurp_utf8 ) );
    $extractor->extract;
}

# do something with that
# maybe write a pot file using Locale::PO
... = extractor->lexicon_ref;

Write your own extractor

package MyExtractor;

use Moo;

extends qw(
    Locale::TextDomain::OO::Extract::Base::RegexBasedExtractor
);
with qw(
    Locale::TextDomain::OO::Extract::Role::File
);

Optional method to uncomment or interpolate the file content or anything else.

sub preprocess {
    my $self = shift;

    my $content_ref = $self->content_ref;
    # modify anyhow
    ${$content_ref}=~ s{\\n}{\n}xmsg;

    return;
}

Map the reference, all the matches or defaults. See Perl extractor how it works. Maybe ignore some stack entries.

sub stack_item_mapping {
    my $self = shift;

    my $match = $_->{match};
    $self->add_message({
        reference    => ...,
        domain       => ...,
        category     => ...,
        msgctxt      => ...,
        msgid        => ...,
        msgid_plural => ...,
    });

    return;
}

Match all positions, the searched string is starting with. You can match to the end of the searched string but there is no need for.

my $start_rule = qr{ ... }xms;

Match exactly the different things. All the values from () are stored in stack. Prepare the stack in a way you are able to merge the data. Maybe an empty match helps to have all on the right position.

'or' means: if not then try the following. 'or' is only allowed between 2 array references.

my $rules = [
    [
        qr{ ... ( ... ) ...}xms, # match this
        qr{ ... ( ... ) ...}xms, # and then that
    ]
    'or',
    [
        [
            qr{ ... ( ... ) ... }xms,
        ],
        'or',
        [
            qr{ ... ( ... ) ... }xms,
        ],
    ],
];

Tell your extractor what steps he should run.

sub extract {
    my $self = shift;

    $self->start_rule($start_rule);
    $self->rules($rules);
    $self->preprocess;
    $self->SUPER::extract;
    for ( @{ $self->stack } ) {
        $self->stack_item_mapping;
    }

    return;
}

SUBROUTINES/METHODS

none

EXAMPLE

Inside of this distribution is a directory named example. Run this *.pl files.

DIAGNOSTICS

none

CONFIGURATION AND ENVIRONMENT

none

DEPENDENCIES

none

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

none

SEE ALSO

Locale::TextDoamin::OO

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2014, Steffen Winkler <steffenw at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.