Locale::Utils::Autotranslator - Base class to translate automaticly
1.011
package MyAutotranslator; use Moo; extends qw( Locale::Utils::Autotranslator ); sub translate_text { my ( $self, $text ) = @_; my $translation = MyTranslatorApi ->new( from => $self->developer_language, to => $self->language, ) ->translate($text); return $translation; }
How to use, see also Locale::Utils::Autotranslator::ApiMymemoryTranslatedNet.
For interactive translation at the console, see also Locale::Utils::Autotranslator::Interactive.
my $obj = MyAutotranslator->new( language => 'de', # all following parameters are optional developer_language => 'en', # en is the default before_translation_code => sub { my ( $self, $msgid ) = @_; ... return 1; # true: translate, false: skip translation }, after_translation_code => sub { my ( $self, $msgid, $msgstr ) = @_; ... return 1; # true: translate, false: skip translation }, bytes_max => 500, ); $identical_obj = $obj->translate( 'mydir/de.pot', 'mydir/de.po', ); # differs in case of bytes_max my $translation_count = $obj->translation_count; my $item_translation_count = $obj->item_translation_count;
E.g. you have a limit of 100 free translations or 10000 words for 1 day you can skip further translations by return any false.
Use that before_... and after_... callbacks for debugging output.
my $obj = MyAutotranslator->new( ... ); $msgstr = $obj->translate_any_msgid( 'short text, long text with paragraphs or multiline text', );
Base class to translate automaticly.
see SYNOPSIS
Get back the language of all msgid's. The default is 'en';
Get back the language you want to translate.
Get back the code references:
$code_ref = $obj->before_translation_code; $code_ref = $obj->after_translation_code;
Get back the bytes the translator API can handle:
$positive_int = $obj->bytes_max;
Translate po files.
$obj->translate('dir/de.pot', 'dir/de.po');
That means: Read the de.pot file (also possible *.po). Translate the missing stuff. Write back to de.po file.
Translate in one or more steps. Count of steps depend on bytes_max.
$msgstr = $obj->translate($msgid);
Called if bytes_max < length of whole text.
Split text into paragraphslines, remove empty lines around. Translate line by paragraph. Add the empty lines and join the translated lines.
$msgstr = $self->with_paragraths( $msgid, sub { return $self->translate_paragraph($_); }, );
Called if bytes_max < length of a paragraph.
Split paragraph into lines, remove the whitespace noise around. Translate line by line. Add the whitespace noise and join the translated lines.
$msgstr = $self->with_lines( $msgid, sub { return $self->translate_line($_); }, );
In base class there is only a dummy method that returns q{}.
q{}
The subclass has to implement that method. Check the code of Locale::Utils::Autotranslator::ApiMymemoryTranslatedNet to see how to implement.
Set a typical comment to mark the translation as translated by ... in API class. Get back that comment.
E.g.
$self->comment('translated by: api.mymemory.translated.net');
Get back the count of translations. This is not the count of translated messages. This is the count of successful translate_text calls if it would not be splitted into paragraphs and lines.
For plural forms we have to translate 1 to 6 times. That depends on language.
my $translation_count = $obj->translation_count;
Get back the count of splitted translations by paragraphs or lines. This is not the count of translated messages, this is the count of successful translate_text calls.
Get back the error message if method translate_text dies.
Inside of this distribution is a directory named example. Run the *.pl files.
none
Carp
Encode
Locale::PO
Locale::TextDomain::OO::Util::ExtractHeader
Moo
MooX::StrictConstructor
MooX::Types::MooseLike::Base
MooX::Types::MooseLike::Numeric
Try::Tiny
namespace::autoclean
not known
http://en.wikipedia.org/wiki/Gettext
Locale::TextDomain::OO
Steffen Winkler
Copyright (c) 2014 - 2021, Steffen Winkler <steffenw at cpan.org>. All rights reserved.
<steffenw at cpan.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Locale::Utils::Autotranslator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Locale::Utils::Autotranslator
CPAN shell
perl -MCPAN -e shell install Locale::Utils::Autotranslator
For more information on module installation, please visit the detailed CPAN module installation guide.