Forums / Developer / Create several language versions of an article

Create several language versions of an article

Author Message

Jerome Despatis

Tuesday 06 October 2009 3:14:12 am

Hi,

Thanks to eZPublish, my users can easily create articles in English (for example).

But I'd like that whenever an article is published, the content is automatically translated (and stored) in other languages (for example french, spanish etc...), thanks to Google API translator or any other webservice.

After digging, it seems that the best way to do so is to create a workflow, triggered on pre_publish or post_publish.

I've done so, but now I've great difficulties to create all the versions (french, spanish for example) of my article

Any idea is welcome, I love piece of code...
Or any code in eZ I could look at to get some inspiration, or some extension

I've seen code (powercontent, object creator) to create object, but i don't think it suits my need as I have already an object created. This is other versions of the object, one for each language, i need to create.

Cheers

Jerome Despatis

Tuesday 06 October 2009 3:38:28 am

Hum after reading my post, and reading the 'concepts and basics' of eZPublish, I've done a big mistake:

A version of an object already contains all the languages, so I "just" need to add in the published version all the languages (spanish and french in my example)

Any help is welcome

Jerome Despatis

Tuesday 06 October 2009 6:35:08 am

Hi again,

I've found the working following code.

Someone could confirm me this code is ok ? or maybe is there a better way to do so ?

This code translates an english article into russian for example

        $db = eZDB::instance();
        $db->begin();

        $newVersion = $object->createNewVersion( $object->CurrentVersion, true, 'rus-RU', 'eng-GB' );
        $contentObjectAttributes = $newVersion->contentObjectAttributes();

        foreach($contentObjectAttributes as $key => $objectAttribute) {
          if ($objectAttribute->contentClassAttributeCanTranslate()) {
            $from = $objectAttribute->attribute('data_text');

            // Translation process taking $from as an argument and returning $output,
            // which is the russian translation of $from for example

            $objectAttribute->setAttribute('data_text', $output);
            $objectAttribute->store();
          }
        }
        $result = eZOperationHandler::execute( 'content', 'publish',
                                               array( 'object_id' => $object->attribute( 'id' ),
                                                      'version' => $newVersion->attribute( 'version' ) ) );

        $db->commit();