Skip to content

Instantly share code, notes, and snippets.

@mamarx
Created July 3, 2012 08:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamarx/3038434 to your computer and use it in GitHub Desktop.
Save mamarx/3038434 to your computer and use it in GitHub Desktop.
Integrate phrase into Symfony2

How to integrate phrase into Symfony2

This tutorial shows how you can easily integrate phrase (https://phraseapp.com) into your Symfony2 app.

  1. Set up a new environment

A good workflow to use phrase in an existing app is to create a new environment in which the phrase app gets loaded. Let's call the new environment 'trans'.

Start by creating a new configuration file:

# app/config/config_trans.yml

imports:
    - { resource: config.yml }

parameters:
    translator.class: Acme\YourBundle\Translation\PhraseTranslator

Because you'll want this environment to be accessible via a browser, you should also create a front controller for it:

# web/app_trans.php

<?php

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('trans', false);
$kernel->handle(Request::createFromGlobals())->send();
  1. Override the trans() method of the standard translator

We have to override the standard translation method with our own to show the phrase translation keys instead of our translations on the page.

Create the new translator class:

# Acme/YourBundle/Translation/PhraseTranslator.php

<?php

namespace Acme\YourBundle\Translation;

use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;

class PhraseTranslator extends BaseTranslator
{
    public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
    {
        $prefix = "{{__phrase_";
        $suffix = "__}}";

        if (!isset($locale)) {
            $locale = $this->getLocale();
        }

        if (!isset($this->catalogues[$locale])) {
            $this->loadCatalogue($locale);
        }

        if ($domain == 'routes') {
            // Return translated values for 'routes' domain
            return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
        } else {
            // Return phrase translation keys for all other domains
            return $prefix.$id.$suffix;
        }
    }
}
  1. Add the phrase javascript to your view

You have to load the phrase app by integrating the given javascript to your view file:

# Acme/YourBundle/Resources/views/layout.html.twig

{% if app.environment == 'trans' %}
<script>
    var phrase_auth_token = 'YOUR_AUTH_TOKEN';
    (function() {
        var phraseapp = document.createElement('script'); phraseapp.type = 'text/javascript'; phraseapp.async = true;
        phraseapp.src = ['https://', 'phraseapp.com/assets/phrase/0.1/app.js?', new Date().getTime()].join('');
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(phraseapp, s);
    })();
</script>
{% endif %}
  1. Push and pull translation files

To push and pull the translation files you have to use ruby. Install the gem:

$ gem install phrase

Create a Gemfile:

# Gemfile
source 'http://rubygems.org'
gem 'phrase'

Now you can use all phrase commands. Take a look at the phrase documentation for further informations.

  1. Start translating your app

Start translating your app by accessing your front controller via browser. When you're done pull the translation file from phrase and add it to your app or integrate the pull command to your deployment process.

<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('trans', false);
$kernel->handle(Request::createFromGlobals())->send();
imports:
- { resource: config.yml }
parameters:
translator.class: Acme\YourBundle\Translation\PhraseTranslator
{% if app.environment == 'trans' %}
<script>
var phrase_auth_token = 'YOUR_AUTH_TOKEN';
(function() {
var phraseapp = document.createElement('script'); phraseapp.type = 'text/javascript'; phraseapp.async = true;
phraseapp.src = ['https://', 'phraseapp.com/assets/phrase/0.1/app.js?', new Date().getTime()].join('');
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(phraseapp, s);
})();
</script>
{% endif %}
<?php
namespace Acme\YourBundle\Translation;
use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;
class PhraseTranslator extends BaseTranslator
{
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
$prefix = "{{__phrase_";
$suffix = "__}}";
if (!isset($locale)) {
$locale = $this->getLocale();
}
if (!isset($this->catalogues[$locale])) {
$this->loadCatalogue($locale);
}
if ($domain == 'routes') {
// Return translated values for 'routes' domain
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
} else {
// Return phrase translation keys for all other domains
return $prefix.$id.$suffix;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment