symfony i18n objects (Doctrine) get specific cultu

2019-06-08 16:15发布

问题:

I am having an issue where I cannot retrieve a specific translation from my i18n doctrine objects. If I call $object->getName(); I get the name in the current culture as expected. However, if I wish to retrieve a specific translation without switching the user culture... $object->getName('fr'); I still get the current culture instead of French in this example. This $object->getTranslation()->fr->name; does work though. What am I doing wrong? Isn't $object->getName($culture); the correct way to do this?

Here is the relevant part of my schema if that's helpful:

Object:
  actAs:
    Timestampable: ~
    I18n:
      fields:           [name, description]
  columns:
    name:               { type: string(255), notnull: true }
    description:        { type: string(1000) }
    user_id:            { type: integer }
  relations:
    User:               { class: sfGuardUser, local: user_id, foreign: id, type: one, foreignType: many, foreignAlias: Objects }

回答1:

$this->Translation['fr']->getName()

alternatively:

$translations = $this->getTranslations();
$translations['fr']->getName();


回答2:

This almost the same problem as on your other question, use:

$object->Translation['fr']->name;
$object->Translation['en']->name;
$object->Translation['it']->name;
// etc ...

I quote the comment I post on the other question:

->getName() works if you are on the object direclty, like $object->getName(). But if you hit the Translation relation, I never used a getter to retrieve value.

By the way, I didn't remember that we can use this $object->getName('fr'); with sf1.0.