This bundle provides a symfony service to interact with Google Translate API. https://developers.google.com/translate/v2/getting_started
To be able to translate sentences, you have to enable billing on your Google Cloud Console https://developers.google.com/translate/v2/pricing
Installation is a quick 3 step process:
- Download PryonGoogleTranslatorBundle using composer
- Enable the Bundle
- Configure your application's config.yml
php composer.phar require zeliard91/google-translator-bundle
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Pryon\GoogleTranslatorBundle\PryonGoogleTranslatorBundle(),
);
}
# app/config/config.yml
pryon_google_translator:
google_api_key: MySecretKey
The bundle provides a service to your app in order to call the Google Translate API in REST.
<?php
$languages = $this->get('pryon.google.translator')->getSupportedLanguages();
//->['en','fr','de','it',...]
<?php
$translation = $this->get('pryon.google.translator')->translate('en','fr','I love Symfony');
//->"J'adore Symfony"
$translations = $this->get('pryon.google.translator')->translate('en','fr', array('I love Symfony', 'I like PHP'));
//->["J'adore Symfony", "J'aime PHP"]
Be aware that Google restricts the use of this service by limiting the size of the query at 5K caracteres with the POST method (and 2K in GET). The number of queries is also limited to 128 if you pass an array for the third argument of the translate method.
The translate method detects if these limits are reached and call the API as many times as necessary which may result a long processing.
A "translatorlanguage" Form Type is also present in this bundle. It is basically the same as the core "language" Form Type except from the choices list which is filled by the API.
<?php
use Pryon\GoogleTranslatorBundle\Form\Type\LanguageType;
use Symfony\Component\Form\FormBuilderInterface;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('source', LanguageType::class, array(
'required' => true,
'label' => 'Source language'
))
;
}
You can cache the responses of the API with one of the subclasses of Doctrine\Common\Cache\CacheProvider
This can be done by specifying what you want to cache and with what in the configuration file. This is the default configuration :
# app/config/config.yml
pryon_google_translator:
cache:
# Specify your doctrine cache service
service: pryon.google.translator.array_cache_provider
calls:
# get available languages method
languages: true
# translate method
translate: false
You can define default HTTP headesr for the REST client hitting the Google API :
# app/config/config.yml
pryon_google_translator:
# ...
client_options:
headers:
Referer: %router.request_context.scheme%://%router.request_context.host%%router.request_context.base_url%
User-Agent: Mr Robot
See Guzzle doc for more information.