{"id":49,"date":"2008-02-20T00:24:59","date_gmt":"2008-02-19T23:24:59","guid":{"rendered":"http:\/\/bemused.org\/blog\/?p=49"},"modified":"2012-04-28T18:27:01","modified_gmt":"2012-04-28T17:27:01","slug":"symfony-and-i18n","status":"publish","type":"post","link":"https:\/\/bemused.org\/blog\/2008\/02\/20\/symfony-and-i18n\/","title":{"rendered":"Symfony and i18n &#8211; site interface translation"},"content":{"rendered":"<p>UPDATE: I&#8217;ve packaged up the files into a plugin, which can be found <a href=\"https:\/\/bemused.org\/symfony\/sfI18nDbTranslationPlugin\/\">here<\/a>.<\/p>\n<p>I&#8217;ve been playing around with <a href=\"http:\/\/www.symfony-project.org\/\">Symfony<\/a> recently, with some <a href=\"http:\/\/en.restaurant-paris.org\/\">interesting results<\/a>.<br \/>\nOne frustrations for me was the lack of documentation explaining how to  store translations in a database.  While symfony provides a way to localise records in the DB, the default method of using XLIFF files for site translation seemed more complicated than necessary.<br \/>\nAs I couldnt find any information about implementing a admin interface to manage site translations, I&#8217;ve implemented this myself, what follows is a description of the steps to do this.  If there are better ways to do this, I&#8217;d love to know, I&#8217;m new to symfony, so still learning.<\/p>\n<p>Here are the steps to add a admin interface to manage site interface translations.<br \/>\n<!--more--><br \/>\n1. Add DB schema details in config\/translation_schema.yml:<\/p>\n<p><code><br \/>\npropel:<br \/>\n  catalogue:<br \/>\n    cat_id: { type: integer, size: 11,  required: true, autoincrement: true, primaryKey: true }<br \/>\n    name: { type: varchar, size: 100, required: true,  default: '' }<br \/>\n    source_lang: { type: varchar, size: 100, required: true,  default: '' }<br \/>\n    target_lang: { type: varchar, size: 100, required: true,  default: '' }<br \/>\n    date_created: { type: integer, size: 11, required: true,  default: 0 }<br \/>\n    date_modified: { type: integer, size: 11, required: true,  default: 0 }<br \/>\n    author: { type: varchar, size: 255, required: true,  default: '' }<br \/>\n  trans_unit:<br \/>\n    msg_id: { type: integer, size: 11, required: true, autoincrement: true, primaryKey: true }<br \/>\n    cat_id: { type: integer, size: 11, required: true, default: 1, foreignTable: catalogue, foreignRef<br \/>\nerence: cat_id }<br \/>\n    source: { type: longvarchar, required: true  }<br \/>\n    target: { type: longvarchar, required: true  }<br \/>\n    comments: { type: longvarchar }<br \/>\n    date_added: { type: integer, size: 11, required: true, default: now }<br \/>\n    date_modified: { type: integer, size: 11, required: true, default: now }<br \/>\n    author: { type: varchar, size: 255, required: true, default: ''}<br \/>\n    translated: { type: boolean, required: true, default: false }<br \/>\n<\/code><\/p>\n<p>2. rebuild model: <code>symfony propel-build-model<\/code> and add the tables to your database.<br \/>\n<code><br \/>\nCREATE TABLE `trans_unit` (<br \/>\n  `msg_id` int(11) NOT NULL auto_increment,<br \/>\n  `cat_id` int(11) NOT NULL default '1',<br \/>\n  `id` varchar(255) NOT NULL default '',<br \/>\n  `source` text NOT NULL,<br \/>\n  `target` text NOT NULL,<br \/>\n  `comments` text NOT NULL,<br \/>\n  `date_added` int(11) NOT NULL default '0',<br \/>\n  `date_modified` int(11) NOT NULL default '0',<br \/>\n  `author` varchar(255) NOT NULL default '',<br \/>\n  `translated` tinyint(1) NOT NULL default '0',<br \/>\n  PRIMARY KEY  (`msg_id`)<br \/>\n)<br \/>\nCREATE TABLE `catalogue` (<br \/>\n  `cat_id` int(11) NOT NULL auto_increment,<br \/>\n  `name` varchar(100) NOT NULL default '',<br \/>\n  `source_lang` varchar(100) NOT NULL default '',<br \/>\n  `target_lang` varchar(100) NOT NULL default '',<br \/>\n  `date_created` int(11) NOT NULL default '0',<br \/>\n  `date_modified` int(11) NOT NULL default '0',<br \/>\n  `author` varchar(255) NOT NULL default '',<br \/>\n  PRIMARY KEY  (`cat_id`)<br \/>\n)<br \/>\n<\/code><\/p>\n<p>3. enable dictionary translation of interface in apps\/frontend\/config\/i18n.yml:<br \/>\n<code><br \/>\nprod:<br \/>\n  default_culture:     en_GB<br \/>\n#  source:              XLIFF<br \/>\n  source:              MySQL<br \/>\n  database: mysql:\/\/user:password@host\/database<br \/>\n  debug:               off<br \/>\n  cache:               on<br \/>\n  untranslated_prefix: \"[T]\"<br \/>\n  untranslated_suffix: \"[\/T]\"<br \/>\ndev:<br \/>\n  default_culture:     en_GB<br \/>\n  source:              MySQL<br \/>\n  database: mysql:\/\/user:password@host\/database<br \/>\n  debug:               on<br \/>\n  cache:               off<br \/>\n  untranslated_prefix: \"[T]\"<br \/>\n  untranslated_suffix: \"[\/T]\"<br \/>\n<\/code><\/p>\n<p>At this stage, if you&#8217;ve been using __(&#8216;string to translate&#8217;) in your templates, you should be able to see [T]untranslated strings[\/T] tagged in your development environment.<\/p>\n<p>4. To check if everything is working correctly, you can manually add some data into the DB :<\/p>\n<p>to define the dictionaries, you need an record in the catalogue table for each culture:<br \/>\n<code><br \/>\nINSERT INTO `catalogue` VALUES (1,'messages.fr_FR','en_GB','fr_FR',0,1197401102,'username'),(2,'messages.de_DE','en_GB','de_DE',0,1197401129,'username'),(3,'messages.en_GB','en_GB','en_GB',0,1197397744,'username');<br \/>\n<\/code><\/p>\n<p>The translations are stores in the trans_unit table :<br \/>\n<code><br \/>\nINSERT INTO `trans_unit` VALUES (1,3,'1','Hello %1%','Hello %1%','',1197393042,0,'',1), (79,1,'2','Hello %1%','Bonjour %1%','',1197401102,0,'',1), (154,2,'2','Hello %1%','Hallo %1%','',1197401129,0,'',1);<br \/>\n<\/code><\/p>\n<p>Using something similar to <code>echo __('Hello %1%', array('%1%' => $username)<\/code> in your templates should give you the correct translations.<\/p>\n<p>Next I wanted to add an admin interface so an admin user can edit these translations without having to upload an XLIFF file.  Here&#8217;s the trans_unit generator.yml file I used:<br \/>\n<code><br \/>\ngenerator:<br \/>\n  class:              sfPropelAdminGenerator<br \/>\n  param:<br \/>\n    model_class:      TransUnit<br \/>\n    theme:            default<br \/>\n    list:<br \/>\n      title:        \"Translation list\"<br \/>\n      max_per_page: 50<br \/>\n      display:      [_lang, =source, target, comments, translated]<br \/>\n      filters: [source, _translated, _catfilter]<br \/>\n      object_actions:<br \/>\n        _edit: ~<br \/>\n        _delete: ~<br \/>\n      sort: source<br \/>\n      fields:<br \/>\n        source: { params: disabled=false }<br \/>\n        translated: { params: disabled=false type: boolean }<br \/>\n        _lang { params: lang }<br \/>\n        catfilter { name: language }<br \/>\n    create:<br \/>\n      title:            \"create Translation\"<br \/>\n      display: [source, comments]<br \/>\n      fields:<br \/>\n        source:      { params: disabled=false size=80, type: input_tag  }<br \/>\n        comments:      { params: disabled=false size=80x5, type: textarea_tag }<br \/>\n      actions:<br \/>\n        _list: ~<br \/>\n        _save: ~<br \/>\n        _delete: ~<br \/>\n    edit:<br \/>\n      title:            \"Edit Translation\"<br \/>\n      display: [_cat, source, target, comments]<br \/>\n      fields:<br \/>\n        source:      { params: disabled=false size=80, type: input_tag  }<br \/>\n        target:      { params: disabled=false size=80, type: input_tag }<br \/>\n        comments:      { params: disabled=false size=80x5, type: textarea_tag }<br \/>\n      actions:<br \/>\n        _list: ~<br \/>\n        _save: ~<br \/>\n        _delete: ~<br \/>\n<\/code><\/p>\n<p>the aim was to add the source string for all cultures simultaneously, but only mark the string as translated once the translation has been added.  This allows the admin to search for untranslated strings. This is what I added in trans_unit\/actions.class.php:<br \/>\n<code><br \/>\nclass trans_unitActions extends autotrans_unitActions<br \/>\n{<\/p>\n<p>  \/\/ need to add to all languages by default<br \/>\n  public function executeCreate()<br \/>\n  {<br \/>\n    $this->trans_unit = new TransUnit();<br \/>\n    if ($this->getRequest()->getMethod() == sfRequest::POST)<br \/>\n    {<br \/>\n      $trans_unit = $this->getRequestParameter('trans_unit');<br \/>\n      foreach (CataloguePeer::getCatalogues() as $catalogue) {<br \/>\n        $c = new Criteria();<br \/>\n        $c->add(TransUnitPeer::SOURCE, $trans_unit['source']);<br \/>\n        $c->add(TransUnitPeer::CAT_ID, $catalogue->getCatId());<br \/>\n        if (! TransUnitPeer::doSelectOne($c)) {<br \/>\n          $this->trans_unit = new TransUnit();<br \/>\n          $this->trans_unit->setSource($trans_unit['source']);<br \/>\n          $this->trans_unit->setTarget('');<br \/>\n          $this->trans_unit->setCatalogue($catalogue);<br \/>\n          $this->trans_unit->save();<br \/>\n        }<br \/>\n      }<br \/>\n      parent::executeList();<br \/>\n      $this->setTemplate('list');<br \/>\n    }<br \/>\n    $this->labels = $this->getLabels();<br \/>\n    \/\/$this->setFlash('notice', $string);<br \/>\n    \/\/$this->setTemplate('create');<br \/>\n  }<\/p>\n<p>  public function executeEdit()<br \/>\n  {<br \/>\n    $c = new Criteria();<br \/>\n    if ($this->getRequestParameter('cat_id')) {<br \/>\n      $c->add(TransUnitPeer::CAT_ID,$this->getRequestParameter('cat_id'));<br \/>\n    }<br \/>\n    $this->trans_unit = TransUnitPeer::doSelectOne($c);<br \/>\n    if ($this->trans_unit) {<br \/>\n      $this->lang = $this->trans_unit->getTargetLang();<br \/>\n    }<br \/>\n    $this->labels = $this->getLabels();<br \/>\n    parent::executeEdit();<br \/>\n  }<\/p>\n<p>  protected function updateTransUnitFromRequest()<br \/>\n  {<br \/>\n    $trans_unit = $this->getRequestParameter('trans_unit');<br \/>\n    $this->trans_unit->setTranslated(1);<br \/>\n    parent::updateTransUnitFromRequest();<br \/>\n  }<br \/>\n  protected function addFiltersCriteria($c)<br \/>\n  {<br \/>\n    if (isset($this->filters['source_is_empty']))<br \/>\n    {<br \/>\n      $criterion = $c->getNewCriterion(TransUnitPeer::SOURCE, '');<br \/>\n      $criterion->addOr($c->getNewCriterion(TransUnitPeer::SOURCE, null, Criteria::ISNULL));<br \/>\n      $c->add($criterion);<br \/>\n    }<br \/>\n    else if (isset($this->filters['source']) && $this->filters['source'] !== '')<br \/>\n    {<br \/>\n      $c->add(TransUnitPeer::SOURCE, '%' . $this->filters['source']. '%', Criteria::LIKE);<br \/>\n    }<br \/>\n    if (isset($this->filters['translated_is_empty']))<br \/>\n    {<br \/>\n      $criterion = $c->getNewCriterion(TransUnitPeer::TRANSLATED, '');<br \/>\n      $criterion->addOr($c->getNewCriterion(TransUnitPeer::TRANSLATED, null, Criteria::ISNULL));<br \/>\n      $c->add($criterion);<br \/>\n    }<br \/>\n    else if (isset($this->filters['translated']) && $this->filters['translated'] !== '')<br \/>\n    {<br \/>\n      $c->add(TransUnitPeer::TRANSLATED, $this->filters['translated']);<br \/>\n    }<br \/>\n    if (isset($this->filters['cat_is_empty']))<br \/>\n    {<br \/>\n      $criterion = $c->getNewCriterion(TransUnitPeer::CAT_ID, '');<br \/>\n      $criterion->addOr($c->getNewCriterion(TransUnitPeer::CAT_ID, null, Criteria::ISNULL));<br \/>\n      $c->add($criterion);<br \/>\n    }<br \/>\n    else if (isset($this->filters['cat_id']) && $this->filters['cat_id'] !== '')<br \/>\n    {<br \/>\n      $c->add(TransUnitPeer::CAT_ID, $this->filters['cat_id']);<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>This gives the interface shown in the following screenshot.<a href='https:\/\/bemused.org\/blog\/wp-content\/uploads\/2008\/02\/xlation.jpg' title='Screenshot of translation interface'><img src='https:\/\/bemused.org\/blog\/wp-content\/uploads\/2008\/02\/xlation-150x150.jpg' alt='Screenshot of translation interface' \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: I&#8217;ve packaged up the files into a plugin, which can be found here. I&#8217;ve been playing around with Symfony recently, with some interesting results. One frustrations for me was the lack of documentation explaining how to store translations in a database. While symfony provides a way to localise records in the DB, the default [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-geeky-stuff"],"_links":{"self":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":2,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bemused.org\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}