Blog

Play with the user language

Symfony Live 2010 Paris Conference

« Back to the Blog

Categories

Feeds

feed Posts feed

comments feed Comments feed

symfony training
Be trained by symfony experts
Feb 15: Paris (What's new in 1.3 / 1.4 - English)
Feb 15: Paris (and Zend Framework Together - English)
Feb 15: Paris (Hosting Practices with symfony - English)
Feb 24: Paris (1.4 + Doctrine - Français)
Mar 04: Online (What's new in 1.3/1.4 - Français)
and more...

Archives

Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.

I have already talked about the sfFormExtraPlugin in a previous post and people were quite amazed by some widgets provided by the plugin.

Today, I will talk about sfFormLanguage, a form also provided by the sfFormExtraPlugin plugin.

Let the user change his language

sfFormLanguage allows the user to change his current language:

sfFormExtraPlugin form

To create a sfFormLanguage form, you need to pass the symfony user object (sfUser) and an array of supported languages:

class mainActions extends sfActions
{
  public function executeChangeLanguage($request)
  {
    $this->form = new sfFormLanguage($this->getUser(), array('languages' => array('en', 'fr')));
  }
}
 

The related template is quite straightforward:

<form action="<?php echo url_for('@change_language') ?>" method="post">
  <label for="language">Choose a language:</label>
  <?php echo $form['language'] ?>
  <?php echo $form->renderHiddenFields() ?>
  <input type="submit" value="ok">
</form>
 

When the user submits the form (post), we need to process it:

class mainActions extends sfActions
{
  public function executeChangeLanguage($request)
  {
    $this->form = new sfFormLanguage($this->getUser(), array('languages' => array('en', 'fr')));
 
    if ($request->isMethod('post'))
    {
      $this->form->process($request);
 
      return $this->redirect('@homepage');
    }
  }
}
 

The sfFormLanguage::process() method takes the current request as a parameter, binds the form with the data provided in the request, validates the form, and if the form is valid, automatically changes the user culture.

The above code does not manage form errors because it can't really happen, except if the user fakes the select values. In such a case, we don't really care if an error is displayed or not. But if you want to, the process() method returns true if the culture has been changed and false otherwise (if there was an error during validation for example):

class mainActions extends sfActions
{
  public function executeChangeLanguage($request)
  {
    $this->form = new sfFormLanguage($this->getUser(), array('languages' => array('en', 'fr')));
    if ($this->form->process($request))
    {
      // culture has been changed
      return $this->redirect('@homepage');
    }
 
    // the form is not valid (can't happen... but you never know)
    return $this->redirect('@homepage');
  }
}
 

sfFormLanguage is simple to use and very useful for internationalized website.

How to guess the language of the user?

When a user comes to your website for the first time, it is sometimes desirable to display the homepage in her "preferred" language. The symfony request object provides a getPreferredCulture() method that does the work for you. So, it is just a matter of changing the current user culture by the preferred one:

$user->setCulture($request->getPreferredCulture(array('en', 'fr')));
 

The getPreferredCulture() method takes an array of ordered cultures you support on your website.

It guesses the best culture possible, based on the HTTP_ACCEPT_LANGUAGE HTTP header. It returns the fist match between the HTTP header and your array of languages. If there is no match, the method returns the first supported culture (en in this example).

Comments comments feed

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.