Development

HowToHandlei18nDbFieldsWithAdminGenerator

You must first sign up to be able to contribute.

How To Handle i18n Db Fields With the Admin Generator

You can manage i18n fields in the backend generated by the admin generator this way.

Imagine you got a schema like this for managing i18n categories :

  <table name="categories" isI18N="true" i18nTable="categories_i18n" baseClass="i18nBaseClass" phpName="Category">
    <column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true"/>
    <column name="name" type="VARCHAR" size="255" required="true"/>
    <column name="is_active" type="BOOLEAN"/>
  </table>
  <table name="categories_i18n" phpName="CategoryI18n">
    <column name="id" type="INTEGER" primaryKey="true" required="true"/>
    <column name="culture" isCulture="true" type="CHAR" size="3" primaryKey="true" required="true"/>
    <column name="title" type="VARCHAR" size="255" required="true"/>
    <column name="excerpt" type="LONGVARCHAR"/>
    <column name="description" type="LONGVARCHAR"/>
    <foreign-key foreignTable="categories">
      <reference local="id" foreign="id"/>
    </foreign-key>
  </table>

Did you notice our Category model extends the i18nBaseClass? All this class has is a constructor which adds the culture currently selected. This way, we're getting localized content in the edit and list actions without the need to override a bunch of methods:

<?php
class i18nBaseClass extends BaseObject
{
  public function __construct()
  {
    $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
  }
}
?>

Then we must enable the external i18n fiels, by enabling them in the generator.yml for the categories module:

generator:
  class:                sfPropelAdminGenerator
  param:
    model_class:        Category
    theme:              default

    list:
      display:          [=name, title, excerpt, description]

    edit:   
      display:          [name, title, excerpt, description]
      fields:         
        title:          { params: disabled=false }
        excerpt:        { params: disabled=false, params: size=100x2, type: textarea_tag }
        description:    { params: disabled=false, params: size=100x4, type: textarea_tag }

Specify a default culture in the app/config/i18n.yml file:

all:
  default_culture:   en

Then, you'll probably want to add a lang chooser in your app/templates/layout.php, like this :

<div class="lang-area">
  <?php echo form_tag('utils/language') ?>
    <p>
      <?php echo label_for('culture', __('Langue courante d\'édition des contenus').' :') ?><br/>
      <?php echo select_language_tag('culture', $sf_user->getCulture(), array('id' => 'culture')) ?>
      <?php echo submit_tag('Go') ?>
    </p>
  </form>
</div>

And - for our example - in the utils module, for the executeLanguage action:

<?php

  ...  

  public function executeLanguage() {
    $this->getUser()->setCulture($this->getRequestParameter('culture'));
    $this->setFlash('notice', 'New current session locale : '.$this->getUser()->getCulture());
    $url = $this->getRequest()->getReferer() != '' ? $this->getRequest()->getReferer() : '@homepage';
    $this->redirect($url);
  }