eZ Community » Forums » Developer » What is the correct way to translate...
expandshrink

What is the correct way to translate an object via php?

What is the correct way to translate an object via php?

Friday 20 April 2012 9:51:11 am - 3 replies

I have read many posts, but no one gave me the solution.
I must create an object via php (that I know do) and I must create it in all languages​​.
What is the best method?
The approach is to translate the full version or translate attribute by attribute?
You can post the code that does this?
There are weeks that I'm stuck with this thing.
Please!

Modified on Friday 20 April 2012 9:58:31 am by Domenico Garozzo

Friday 20 April 2012 10:45:45 am

Hi Domenico,

take a look at createNewVersion method of the content object, it should be what you're looking for. It gives you the options to specify to which language to translate to, whether to create an empty translation or fill it with values of some other translation and which version to use to translate. After using the method, you get the new version which you can use to set the attribute data and then publish the version. Something like this which translates the object from english to croatian, using the latest available version:

$newVersion = $publishedObject->createNewVersion( false, true, 'cro-HR', 'eng-GB' );
 
$dataMap = $newVersion->dataMap();
$dataMap['title']->fromString( 'My translated title' );
$dataMap['title']->store(); 
 
eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $publishedObject->attribute( 'id' ), 'version' => $newVersion->attribute( 'version' ) ) );

Hope it works for you happy.gif Emoticon

Modified on Friday 20 April 2012 10:46:46 am by Edi Modrić

Friday 20 April 2012 10:50:17 am

Hi Domenico,

you can do it like this. I don't know if this is best practice, but i did it like this and you have to set up the translation part somewhere between:

        $object = eZContentObject::fetchByNodeID( $nodeId);
        $lang = $object->attribute('lang'); 
        
        $version = $object->createNewVersion( false, true, $lang );
        $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
        $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
        $version->store();
        
        $contentObjectId = $object->attribute( 'id' );
        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectId,
                                                                                     'version' => $version->attribute( 'version' ) ) );
        $this->db->commit();
 
Hope this helps you. Any doubts? Post a reply :) 
Best Regards
Chris

Friday 20 April 2012 10:53:25 am

Thank you very much!!

SOLVED

I'm very happy!

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from