embedI18n('FR') only, not en and others...

Discussion relating to version 1.3.x and 1.4.x

embedI18n('FR') only, not en and others...

Postby martin.d » Thu Oct 07, 2010 5:16 pm

Hi,

My context ... If your working in english, everything is in english. UI and content. So I dont need to translate the content in 2 languages everytime i save some object. If the user switch to french, then he can translate the content to french.

With 1.4(doctrine and i18n) I used admin generator to build my backend and everyting is ok.

backend/config/settings.yml
Code: Select all
    i18n:  on
    default_culture:        fr

schema.yml ...
Code: Select all
Poll:
  actAs:
    Timestampable: ~
    I18n:
      fields: [ title, question ]
      actAs:
        Sluggable: { fields: [title], uniqueBy: [lang, title] }
  columns:
    title:         { type: string(255),  notnull: true }
    question:      { type: string(1000), notnull: true }
...

In my form class ...
Code: Select all
class BackendPollForm extends BasePollForm
{
  public function configure()
  {
        unset( $this['created_at'], $this['updated_at'] );
        // Hardcoded for testing.
        $this->embedI18n(array('fr'));
        $this->widgetSchema->setLabel('fr', 'Français');   
  }
}

Everything work fine until I save...
Integrity constraint violation: 1062 Duplicate entry 'en-' for key 'poll_translation_sluggable_idx'


I have embeded only FR and it's always using both FR and EN. EN is empty so it will always save as null and generate the constraint error.

How do I embed-it so it only use the form related to "current language" and not both ?

Mart
martin.d
Junior Member
 
Posts: 1
Joined: Thu Oct 07, 2010 4:49 pm

Re: embedI18n('FR') only, not en and others...

Postby lukas22 » Tue May 17, 2011 3:14 pm

you solve this??? I have the same issue..
User avatar
lukas22
Junior Member
 
Posts: 16
Joined: Sat Feb 21, 2009 2:20 am
Location: Paraná - Entre Ríos - Argentina

Re: embedI18n('FR') only, not en and others...

Postby etoileweb » Sat Dec 03, 2011 1:10 pm

Read http://www.symfony-project.org/forms/1_4/en/08-Internationalisation-and-Localisation, especially the "How to pass the User Culture to a Form?" section.
etoileweb
Member
 
Posts: 31
Joined: Thu Aug 26, 2010 7:53 am

Re: embedI18n('FR') only, not en and others...

Postby zaktrak » Wed Jan 23, 2013 11:23 pm

I know this is a very old thread, but I am currently stuck with the same issue, or similar and cannot figure out how to save my form without english.

I create the form in the action like this:
Code: Select all
$this->form = new MyForm(null, array('culture'=> $this->formCulture));


and here is my form class:
Code: Select all
 public function configure()
  {
     $currentLanguages = Doctrine_Core::getTable('Language')->getByCode($this->getCurrentCulture());
     
     //EMBED I18N FOR THE CURRENT OR MULTIPLE LANGUAGES
     $this->embedI18n($this->getCurrentCulture());
     //SET LANGUAGE LABELS
     foreach ($currentLanguages as $lang){
        if($this->widgetSchema->offsetExists($lang->getCode())){
           $this->widgetSchema->setLabel($lang->getCode(), $lang->getName());
        }
     }
  }
 
  public function getCurrentCulture()
  {
    return isset($this->options['culture']) ? $this->options['culture'] : array('en');
  }


So far so good. It works very well. I can pass an array of cultures to the form and it creates the form with the correct fields.
If I pass just 'en' then I get a form just for the english and it saves correctly.
If I pass just 'fr' then I get a form for just french, but it will save with an empty english record in the database as well as the french record.
If I pass 'fr', 'it', 'es' then I get a form with french, italian, and spanish form fields, but again it will save all those records in addition to a blank english record.

How do I prevent it from saving english records when there are no english fields in the form?
I have tried dumping the request parameter for the form before the request parameters are bound to the form and I can verify that there are no English fields (even blank ones). I guess it is getting added either during the bind or the save, but I don't know how to prevent this behavior.
Any suggestions would be greatly appreciated.

Thanks!
zaktrak
Member
 
Posts: 32
Joined: Thu Jun 26, 2008 12:21 am

Re: embedI18n('FR') only, not en and others...

Postby zaktrak » Mon Jan 28, 2013 9:18 pm

Some more information to maybe inspire an idea as to what could be going on here.

I previously said it always saves an English record. This is correct, but only when the user culture is English. The blank record is in whatever language the user's culture is set to. For instance, if my culture is fr, and I try to save an english record, I will get the english record as well as a blank fr record in the database. If my culture is fr and I try to save an Italian entry, I get a record for it and fr (the fr one is blank as always). I just wanted to clarify, that the blank record is not always English... it is always the same as the user's culture.

I have tried overriding the doSave() function and included a
Code: Select all
var_dump($this->getValues())
to see what was included, and there is only the data for the single language I entered. There is not the blank user culture info included in the array of data. I also tried overriding the saveEmbeddedForms() and save() methods by including the var_dump of the $this->getValues() just to be thorough, but the extra blank record was never there. I don't know where it is coming in at, and how to prevent it from being saved to the database.

Any ideas would be awesome.

Thanks,
Zach
zaktrak
Member
 
Posts: 32
Joined: Thu Jun 26, 2008 12:21 am

Re: embedI18n('FR') only, not en and others...

Postby zaktrak » Mon Feb 18, 2013 10:04 pm

I still have not found a solution for this, so in the meantime, I am just deleting the blank record after it is being created. Now I am wondering if there is something that just isn't set up correctly. When I echo a i18n related field it is displaying in the user's culture as expected, but if I want to get a specific translation through $object->getName('fr'); it does not work. Nothing is returned. However $object->getTranslation()->fr->name does work. I really have no idea why this is, and any help would be much appreciated.

Thanks!
zaktrak
Member
 
Posts: 32
Joined: Thu Jun 26, 2008 12:21 am

Re: embedI18n('FR') only, not en and others...

Postby zaktrak » Mon Feb 18, 2013 10:43 pm

Actually my previous test was on an object that only had a French translation...
$object->getName('fr'); did nothing
$object->getTranslation()->fr->name this works - returns french

if I make the same calls on an object with both English and French translation

$object->getName('fr'); returns the english translation
$object->getTranslation()->fr->name returns the french translation

User culture is 'en' on both tests.

Why is this??
zaktrak
Member
 
Posts: 32
Joined: Thu Jun 26, 2008 12:21 am


Return to symfony 1.3 and 1.4

Who is online

Users browsing this forum: No registered users and 6 guests