NAME

Lingua::Translator::Microsoft - A client library for the Microsoft Translator API

SYNOPSIS

my $api_key = read_file('/home/myapp/priv/translator.priv');
my $translator = Lingua::Translator::Microsoft->new(
    api_key  => $api_key,
    app_id   => $app_id,
);

say $translator->translate('nl', 'en', 'voorbeeld'); # outputs 'example'

my $mp3 = $translator->speak('de', 'Worüber man nicht sprechen kann, darüber muss man schweigen');
open(my $fh, ">", "tractatus.mp3", {format => "mp3"});
print $fh $mp3;
system("mplayer tractatus.mp3");

say $translator->detect("Ci vuole un fiore."); # outputs 'it'

DESCRIPTION

This is a client library for Microsoft's translate service. Currently you can use the following calls from the API:

Translate
GetTranslations
Detect
Speak

All API-calling methods croak() unless they get a successful reply from the service.

FUNCTIONS

Lingua::Translator::Microsoft->new(api_key => $api_key, app_id => $app_id);

Instantiate a new Lingua::Translator::Microsoft object.

Arguments:

  • api_key [required]

    The API key (client secret).

  • app_id [required]

    Your application ID (client id). You need to register your application to be able to use the service.

  • auth_url [optional]

    The URL to get the OAuth token from. Defaults to https://datamarket.accesscontrol.windows.net/v2/OAuth2-13. You probably don't need to change this.

  • api_url [optional]

    The URL for the Microsoft Translator API (v2). Defaults to http://api.microsofttranslator.com/v2/Http.svc. You probably don't need to change this.

Returns:

  • A new Lingua::Translator::Microsoft instance.

$translator->translate($source_language_code, $target_language_code, $text)

Translate some text

Arguments:

  • source_language_code [required] (String)

  • target_language_code [required] (String)

  • text [required] (String)

    The text to translate.

Returns:

  • The translated text as a string.

$translator->get_translations($source_language_code, $target_language_code, $text, { max_translations => 3})

Translate some text (with multiple results).

This function is sensitive to context. It returns an arrayref of translation in scalar context but a list of translations in list context.

Arguments:

  • source_language_code [required] (String)

  • target_language_code [required] (String)

  • text [required] (String)

    The text to translate.

  • options [optional] (Hashref)

    A struct containing options to the call. For now the only option that you can put here is max_translations which limits the number of results to a given number. max_translations defaults to 5.

Returns:

  • In list context the results as a list of strings (translations).

  • In scalar context an arrayref of strings (translations).

$translator->speak($language_code, $text)

Pronounce some text

Arguments:

  • language_code [required] (String)

  • text [required] (String)

    The text to synthetize.

Returns:

  • A wav stream containing the text spoken in the chosen language.

$translator->detect($text)

Detect the language of a text.

Arguments:

  • text [required] (String)

    The text to do language detection on.

Returns:

  • The code of the detected language.

AUTHOR

This module is written by Larion Garaczi <larion@cpan.org> (2016)

SOURCE CODE

The source code for this module is hosted on GitHub https://github.com/larion/lingua-translator-microsoft.

Feel free to contribute :)

LICENSE AND COPYRIGHT

This module is free software and is published under the same terms as Perl itself.