NAME
Locale::Utils::Autotranslator - Base class to translate automaticly
VERSION
1.016
SYNOPSIS
Create your own translator
package
MyAutotranslator;
use
Moo;
extends
qw(
)
;
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.
Translate po files
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.
Translate text directly
my
$obj
= MyAutotranslator->new(
...
);
$msgstr
=
$obj
->translate_any_msgid(
'short text, long text with paragraphs or multiline text'
,
);
DESCRIPTION
Base class to translate automaticly.
SUBROUTINES/METHODS
method new
see SYNOPSIS
method developer_language
Get back the language of all msgid's. The default is 'en';
method language
Get back the language you want to translate.
before_translation_code, after_translation_code, bytes_max
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;
method translate
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.
method translate_any_msgid
Translate in one or more steps. Count of steps depend on bytes_max.
$msgstr
=
$obj
->translate(
$msgid
);
method with_paragraphs (normally not used directly)
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(
$_
);
},
);
method with_lines (normally not used directly)
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(
$_
);
},
);
method translate_text
In base class there is only a dummy method that returns q{}
.
The subclass has to implement that method. Check the code of Locale::Utils::Autotranslator::ApiMymemoryTranslatedNet to see how to implement.
method comment
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'
);
method translation_count
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;
method item_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.
my $translation_count = $obj->translation_count;
method error
Get back the error message if method translate_text dies.
EXAMPLE
Inside of this distribution is a directory named example. Run the *.pl files.
DIAGNOSTICS
none
CONFIGURATION AND ENVIRONMENT
none
DEPENDENCIES
Locale::TextDomain::OO::Util::ExtractHeader
MooX::Types::MooseLike::Numeric
INCOMPATIBILITIES
not known
BUGS AND LIMITATIONS
not known
SEE ALSO
http://en.wikipedia.org/wiki/Gettext
AUTHOR
Steffen Winkler
LICENSE AND COPYRIGHT
Copyright (c) 2014 - 2021, 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.