It has never been so easy to internationalize your Propel forms. In this post, you will learn how to leverage the new form framework bundled with symfony 1.1 to develop an interface to edit articles in several languages.

Let's take a very simple internationalized Propel schema:

propel:
  article:
    id:         ~
    author:     varchar(255)
    created_at: ~
  article_i18n:
    title:      { type: varchar, size: 255, required: true }
    content:    longvarchar

After your database has been configured, use the propel:build-all task to generate the Propel model and form classes:

$ php symfony propel:build-all

The following files has been created automatically under your project root directory:

lib/
  form/
    ArticleForm.class.php
    ArticleI18nForm.class.php
    BaseFormPropel.class.php
  model/
    Article.php
    ArticlePeer.php
    ArticleI18n.php
    ArticleI18nPeer.php

We want to be able to update both the English and French versions of an article in the same interface:

If you've already tried to code this kind of interface with symfony 1.0, you know that's a long and a somewhat annoying task.

Thanks to the symfony 1.1 enhancements, it will take us 2 minutes to make it work.

First, configure the languages you want to handle in the ArticleForm class:

class ArticleForm extends BaseArticleForm
{
  public function configure()
  {
    $this->embedI18n(array('en', 'fr'));
 
    $this->widgetSchema->setLabel('en', 'English');
    $this->widgetSchema->setLabel('fr', 'French');
  }
}
 

Secondly, generate a CRUD module to provide a web interface to be able to list, create, update, and delete articles:

$ php symfony generate:crud frontend article Article

Enjoy the fully working module by browsing to /frontend_dev.php/article. If you try to submit the edit form without any title, you will see that the database constraints defined in the schema are also automatically enforced.

Published in #Tutorials