• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

eko/GoogleTranslateBundle: A Symfony bundle to deals with Google Translate API

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

eko/GoogleTranslateBundle

开源软件地址(OpenSource Url):

https://github.com/eko/GoogleTranslateBundle

开源编程语言(OpenSource Language):

PHP 78.0%

开源软件介绍(OpenSource Introduction):

GoogleTranslateBundle

SensioLabsInsight

Build Status Latest Stable Version Total Downloads

Features

  • Detect language used for a string
  • Translate a string from a source language to a target one
  • Translate a string into a target language by using language auto-detection (consume 1 more API call)
  • Retrieve all languages available on API and obtain language names in a given language
  • Profile detector / translate / languages list API calls in the Symfony profiler!

Installation

Add the bundle to your composer.json file:

{
    "require" :  {
        "eko/googletranslatebundle": "dev-master"
    }
}

Add this to app/AppKernel.php

<?php
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Eko\GoogleTranslateBundle\EkoGoogleTranslateBundle(),
        );

        ...

        return $bundles;
    }

Configuration

Edit app/config.yml

The following configuration lines are required:

eko_google_translate:
    api_key: <your key api string>

Usages

Detect a string language

Retrieve the detector service and call the detect() method:

$detector = $this->get('eko.google_translate.detector');
$value = $detector->detect('Hi, this is my string to detect!');
// This will return 'en'

Translate a string

Retrieve the translator service and call the translate() method:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate('Hi, this is my text to detect!', 'fr', 'en');
// This will return 'Salut, ceci est mon texte à détecter!'

Translate a string from unknown language (use detector)

Retrieve the translator service and call the translate() method without the source (third) parameter:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate('Hi, this is my text to detect!', 'fr');
// This will return 'Salut, ceci est mon texte à détecter!'

Translate multiple strings

Retrieve the translator service and call the translate() method with an array of your strings:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate(array('Hi', 'This is my second text to detect!'), 'fr', 'en');
// This will return the following array:
// array(
//     0 => 'Salut',
//     1 => 'Ceci est mon second texte à détecter !',
// )

Note that you can also use an "economic mode" to translate multiple strings in a single request which is better for your application performances.

Your translations will be concatenated in one single Google request. To use it, simply add true to the last argument:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate(array('Hi', 'This is my second text to detect!'), 'fr', 'en', true);
// This will return the following array:
// array(
//     0 => 'Salut',
//     1 => 'Ceci est mon second texte à détecter !',
// )

Obtain all languages codes available

Retrieve the languages service and call the get() method without any argument:

$languages = $this->get('eko.google_translate.languages')->get();
// This will return:
// array(
//     array('language' => 'en'),
//     array('language' => 'fr'),
//     ...
// )

Obtain all languages codes available with their names translated

Retrieve the languages service and call the get() method with a target language argument:

$languages = $this->get('eko.google_translate.languages')->get('fr');
// This will return:
// array(
//     array('language' => 'en', 'name' => 'Anglais'),
//     array('language' => 'fr', 'name' => 'Français'),
//     ...
// )

Notice: this will consume a detector API call.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Sponsor @eko on GitHub Sponsors · GitHub发布时间:2022-06-11
下一篇:
googlemaps/js-samples: Samples for the Google Maps JavaScript v3 API发布时间:2022-06-11
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap