NAME Locale::TextDomain::OO::Extract::Process - Prepare PO/MO files for extraction and store them back after extraction

$Id: Process.pm 719 2018-09-21 12:58:00Z steffenw $

$HeadURL: svn+ssh://steffenw@svn.code.sf.net/p/perl-gettext-oo/code/extract/trunk/lib/Locale/TextDomain/OO/Extract/Process.pm $

VERSION

2.015

DESCRIPTION

Prepare PO/MO files for extraction and store them back after extraction.

SYNOPSIS

build a process object

use Locale::TextDomain::OO::Extract::Perl; # subclass Perl
use Path::Tiny qw(path);

Set in constructor all static values. Set all other in loop.

my $process = Locale::TextDomain::OO::Extract::Process->new(
    # set all static things here, all dynamics in loops
    project     => 'my project',     # for special cases
                                     # set or default undef is used
    category    => 'LC_MESSAGES',    # for special cases
                                     # set or default q{} is used
    domain      => 'my domain',      # text domain
                                     # set or default q{} is used
    language    => 'en',             # mostly dynamic and not set here
                                     # set or the default 'i-default' is used
    lexicon_ref => $my_own_hash_ref, # mostly not set
    plugin_ref  => {                 # depends on read/write files
        po  => 'PO',                 # default
        mo  => 'MO',
        foo => '+My::FooPlugin',     # with full class name
    },
);

read all existing PO files

for ( @po_files ) {
    $process->project('my project');
    $process->category('LC_MESSAGES');
    $process->domain('my domain');
    $process->language('en');
    $process->slurp( po => $_ );
}

strip all references

This are the #: comments in PO files.

$process->remove_all_reference;

strip all automatic comments

This are the #. comments in PO files.

$process->remove_all_automatic;

extract e.g. all *.pl and *.pm files and so fill with new references

my $extract = Locale::TextDomain::OO::Extract::Perl->new;
for ( @perl_files ) {
    $extract->clear;
    $extract->project('my extract project');
    $exttact->category('my extract category');
    $extract->domain('my extract domain');
    # language is i-default
    $extract->filename($_);
    $extract->content_ref( \( path($_)->slurp_utf8 ) );
    $extract->extract;
}

merge extracted data

Merge of new or changed data from extract lexicon to process lexicon. Mostly the extract project/category/domain is the same like process project/category/domain. But in can be different.

$process->merge_extract({
    lexicon_ref       => $extract->lexicon_ref,
    project           => 'my extract project',
    category          => 'my extract category',
    domain            => 'my extract domain',
    # extract language is i-default
    # skip region if region file is only the difference, e.g. to language de
    skip_new_messages => $process->language eq 'de-at',
    # or extended
    skip_new_messages => {
        on           => $process->language eq 'de-at'
        no_skip_for  => # arrayref or scalar with string or regex
                        [ '.domain.de', '+49', qr{ ... }xmsi ) ],
        but_skip_for => # same like before but filter out false positive
                        'Fax: ',
    },
});

write back all PO files

for ( @po_files ) {
    $process->clear;
    $process->project('my project');
    $process->category('LC_MESSAGES');
    $process->domain('my domain');
    $process->language('en');
    $process->spew( po => $_ );
}

now translate all po files

Do it.

read agian all existing PO files

for ( @po_files ) {
    $process->project('my project');
    $process->category('LC_MESSAGES');
    $process->domain('my domain');
    $process->language('en');
    $process->slurp( po => $_ );
}

clean all entries with no reference

This are the #: comments in PO files.

$process->remove_all_non_referenced;

write back all PO files and also as MO file

for ( @po_files ) {
    $process->clear;
    $process->project('my project');
    $process->category('LC_MESSAGES');
    $process->domain('my domain');
    $process->language('en');
    $process->spew( po => $_ );
    ( my $mo_file = $_ ) =~ s{ [.] po \z }{.mo}xms;
    $process->spew( mo => $mo_file );
}

SUBROUTINES/METHODS

method new, category, domain, language, project, lexicon_ref, plugin_ref

see SYNOPSIS

method add_plugin

Needs a plugin name amd a package name. If no + is written the Package name is prefixed by "Locale::TextDomain::OO::Extract::Process::Plugin::".

$process->add_plugin( mo  => 'MO' );
$process->add_plugin( bar => '+My::BarPlugin' );

method slurp

Slurp a file and put the data into the lexicon_ref.

$process->slurp( po => 'filename.po' );

method spew

Spew a file with data of lexicon_ref.

$process->spew( mo => 'filename.mo');

method remove_all_reference

Strips all references. References are here gettext references, the filename and line of file the extractor has found.

$process->remove_all_reference;

method remove_all_automatic

Strips all automatic comments.

$process->remove_all_automatic;

method remove_all_non_referenced

All entries with no reference are no longer in source because the extractor has not found. So there is no need to translate this phrases.

$process->remove_all_non_referenced;

method merge_extract

The extractor extracts for language i-default. That is running one time for all files. But every language needs the new extraction data. So all new and changed data will be merged to any language. For sublanguages/regions it is possible to skip.

$process->merge_extract({
    lexicon_ref         => $extract->lexicon_ref,
    # all following optional
    category            => 'category during extraction',
    domain              => 'domain during extraction',
    project             => 'project during extraction',
    # simple
    skip_new_messages   => $boolean,
    # or extended
    skip_new_messages   => {
        on           => $boolean,
        no_skip_     => $arrayref_or_scalar_with_string_or_regex,
        but_skip_for => $arrayref_or_scalar_with_string_or_regex,
    },
});

EXAMPLE

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

DIAGNOSTICS

none

CONFIGURATION AND ENVIRONMENT

none

DEPENDENCIES

Carp

Clone

Class::Load

Locale::TextDomain::OO::Util::JoinSplitLexiconKeys

Moo

MooX::StrictConstructor

MooX::Types::MooseLike::Base

Set::Scalar

namespace::autoclean

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

none

SEE ALSO

Locale::TextDoamin::OO::Extract

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2014 - 2018, 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.