The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Locale::Utils::Autotranslator - Base class to translate automaticly

$Id: Autotranslator.pm 614 2015-08-17 10:28:48Z steffenw $

$HeadURL: $

VERSION

1.001

SYNOPSIS

    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 Locale::Utils::Autotranslator::ApiMymemoryTranslatedNet.

    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
        },
    );
    $identical_obj = $obj->translate(
        'mydir/de.pot',
        'mydir/de.po',
    );
    my $translation_count = $obj->translation_count;

Return code of 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 methods for debugging output.

DESCRIPTION

Base class to translate automaticly.

SUBROUTINES/METHODS

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

Get back the code references:

$code_ref = $obj->before_translation_code; $code_ref = $obj->after_translation_code;

method translate

    $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_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.

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

Carp

Encode

Locale::PO

Locale::TextDomain::OO::Util::ExtractHeader

Moo

MooX::StrictConstructor

MooX::Types::MooseLike::Base

MooX::Types::MooseLike::Numeric

Try::Tiny

namespace::autoclean

INCOMPATIBILITIES

not known

BUGS AND LIMITATIONS

not known

SEE ALSO

http://en.wikipedia.org/wiki/Gettext

Locale::TextDomain::OO

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

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