alice icon indicating copy to clipboard operation
alice copied to clipboard

Registering Faker provider for localized data

Open yann-eugone opened this issue 11 years ago • 7 comments

Hi, before I explain my problem I'd like to thank the contributors of this usefull repository.

I prefixed this issue with [question] because I will be very happy that you tell me that there is a way to handle my problem with something existing in this repository.

I'm facing a problem in the following context :

  • Working on a Symfony2 project
  • A project that rely on the CMF
  • A project with objects that can be translated

I need to load data fixtures, and I need some properties to get translated in several locales.

The problem is : how ?

I'm sad to say that I had to implement my own loader to handle objects properties translation with a pattern like this :

My\Bundle\Entity\Object:
    my_object:
        fields:
            aFieldNotTranslated: Hello
            anOtherFieldNotTranslated: World
        i18n:
            en:
                aFieldTranslated: Hello
            fr:
                aFieldTranslated: Bonjour

Under the hood, my loader do something like this :

public function createObject($object, $data)
{
    foreach ($data['fields'] as $field => $value) {
        $this->setValue($object, $field, $value);
        $this->manager->persist($object);
    }
    foreach ($data['i18n'] as $locale => $fields) {
        $object->setLocale($locale);
        foreach ($fields as $field => $value) {
            $this->setValue($object, $field, $value);
        }
        $this->manager->bindTranslation($object, $locale);
        $this->manager->persist($object);
    }
}

I said that I'm sad to do this because this loader is not as powerfull as the Alice's loader (no faker integration for example), and I rather prefer to use always the same loader against all my projects.

Please, if I missed something in the documentation, tell me, and I will test it.

If there is nothing that suit my need here, and if you are interested in, I can try to make a pull request with some features addition to do the job.

Best regards, Yann

yann-eugone avatar Nov 14 '14 11:11 yann-eugone

Closing as is a duplicate of #178.

theofidry avatar May 04 '16 22:05 theofidry

Does anyone have a working example of it? :thinking:

aistis- avatar Jul 20 '17 08:07 aistis-

You can now do calls (on 3.x): https://github.com/nelmio/alice/blob/master/doc/complete-reference.md#calling-methods

theofidry avatar Jul 20 '17 08:07 theofidry

Not sure to see how calls may help with this.

yann-eugone avatar Jul 26 '17 07:07 yann-eugone

I guess, you could use addTranslation(@translation_1) on your translatable model. But still - you have to build this @translation_1 object on separately as alice supports natively only flat descriptions.

aistis- avatar Jul 26 '17 07:07 aistis-

You are talking about "personal translation". With MyModelTranslation model, you can do what you say (you could do this at the moment where the issue where opened).

This is not a strategy I can choose.

yann-eugone avatar Jul 26 '17 07:07 yann-eugone

Sorry I may have misunderstood your case. I think you can add localised faker providers, cf. localized data generation.

That said I'm not entirely sure about how you register a faker provider only for a specific local.

theofidry avatar Jul 26 '17 08:07 theofidry