nelmio / alice

Expressive fixtures generator
MIT License
2.5k stars 329 forks source link

Registering Faker provider for localized data #160

Open yann-eugone opened 9 years ago

yann-eugone commented 9 years ago

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 :

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

theofidry commented 8 years ago

Closing as is a duplicate of #178.

aistis- commented 7 years ago

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

theofidry commented 7 years ago

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

yann-eugone commented 7 years ago

Not sure to see how calls may help with this.

aistis- commented 7 years ago

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.

yann-eugone commented 7 years ago

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.

theofidry commented 7 years ago

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.