Forums / Developer / Creating translated content

Creating translated content

Author Message

Antoine W.

Monday 11 April 2005 8:07:49 am

Hello,

I'm trying to move pages from a big website to eZ Publish.
I have made an import script that scans my old database, and create Content Objects in folders.
This script works fine, but i'm trying now to import Content Objects <b>with different translations</b>.
I cant find how to create new translations for an ezContentObject.
Any idea would be greatly appreciated ! :)
Thanks.

Here is a part of my import script :

$class =& eZContentClass::fetch( OBJECTCLASSID );
$ContentObject =& $class->instantiate( $user->id(), $sectionID );

//----------------------------------------------------------
// Create a node for the object in the tree.
//----------------------------------------------------------
$nodeAssignment =& eZNodeAssignment::create( array(
   'contentobject_id' => $ContentObject->attribute( 'id' ),
   'contentobject_version' => 1,
   'parent_node' => $parentNodeID,
   'sort_field' => 2, //Published
   'sort_order' => 1, //Descending
   'is_main' => 1));
$nodeAssignment->store();

//----------------------------------------------------------
// Set a status for the content object version.
//----------------------------------------------------------
$ContentObjectVersion =& $ContentObject->version($ContentObject->attribute( 'current_version' ) );
$ContentObjectVersion->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
$ContentObjectVersion->store();

//----------------------------------------------------------
// Set attributes of the Page.
//----------------------------------------------------------
$ContentObjectAttributes =& $ContentObjectVersion->contentObjectAttributes();
foreach ($ContentObjectAttributes as $id=>$Attribute)
{
   if($Attribute->attribute("contentclass_attribute_identifier")=="title")
   {
      $Attribute->setAttribute("data_text", "my title here");
      $Attribute->store();
   }
   if($Attribute->attribute("contentclass_attribute_identifier")=="body")
   {
      $Attribute->setAttribute("data_text", "body of my page");
      $Attribute->store();
   }
}

$ContentObject->store();

//----------------------------------------------------------
// Now publish the object.
//----------------------------------------------------------
include_once ('lib/ezutils/classes/ezoperationhandler.php');
$operationResult = eZOperationHandler::execute(
   'content',
   'publish',
   array( 
      'object_id' => $ContentObject->attribute( 'id' ),
      'version'   => $ContentObject->attribute('current_version' ) 
   ) 
);

//----------------------------------------------------------
// Fine, my page is now created !
// But how can I create a translation for it ?
// I havent seen public methods to create a translation for an object.
// I'm looking for something like:
//
// $translation=$ContentObject->translate("eng-GB");
// $translationAttributes =& $translation->contentObjectAttributes();
// (...)

Sebastiaan van der Vliet

Tuesday 12 April 2005 12:55:38 am

Hello,

One of our developers has done an import script once for different translations, but for an ezp 3.2 installation. I do not know if the script is still useful, but I have posted the translation part of it below. If you want the complete script, please let me know.

Kind regards,
Sebastiaan

print( "Starting article import\n\r" );
include_once( "lib/ezutils/classes/ezdebug.php" );
include_once( "lib/ezutils/classes/ezmodule.php" );
eZModule::setGlobalPathList( array( "kernel" ) );
include_once( 'lib/ezutils/classes/ezexecution.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( "lib/ezxml/classes/ezxml.php" );
include_once( 'lib/ezlocale/classes/ezdatetime.php' );
include_once( 'import_text2xml.php' );

[.. code for publishing object in default language removed from this example...]

$ContentObject->store();


// ******************** FRENCH TRANSLATION
// Create French language translation by duplicating all English attributes
	
$originalContentAttributes =& $version->contentObjectAttributes();
unset( $translateContentAttributes );
$translateContentAttributes = array();
foreach ( array_keys( $originalContentAttributes ) as $contentAttributeKey ) {
$originalContentAttribute =& $originalContentAttributes[$contentAttributeKey];

//print_r($originalContentAttributes);
$contentAttribute =& $originalContentAttribute->translateTo( 'fre-FR' );

//print_r($contentAttribute);
$contentAttribute->sync();

//print "Created French version\n";
$contentObject->store();

// Translate text fields into French
print "Doing French translation\n";
$translateContentAttributes =& $version->contentObjectAttributes( 'fre-FR' );

// Title - first attribute in the article class
$translateContentAttributes[0]->setAttribute( 'data_text', $articleNameFR );
$translateContentAttributes[0]->store();

// Subtitle - second attribute in the article class
$translateContentAttributes[1]->setAttribute( 'data_text', $articleSubtitleFR );
$translateContentAttributes[1]->store();

// Intro 
$converter = new text2xml( $articleContentFR, $translateContentAttributes[3] );
$converter->validateText( $introtext, $translateContentAttributes[3] );
$translateContentAttributes[3]->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP );
$translateContentAttributes[3]->store();

// body
$converter->validateText( $articleContentFR, $translateContentAttributes[4] );
$translateContentAttributes[4]->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP );
$translateContentAttributes[4]->store(); 

$contentObject->store();

Certified eZ publish developer with over 9 years of eZ publish experience. Available for challenging eZ publish projects as a technical consultant, project manager, trouble shooter or strategic advisor.

Antoine W.

Tuesday 12 April 2005 5:04:45 am

A big thanks for your code, Sebastiaan.
It works fine now :)

Ekkehard Dörre

Tuesday 12 April 2005 5:29:08 am

Hi Antoine,

when it works fine now, it might be useful for others, too ;-)
Do you like to publish it here?

Thanks, ekke

http://www.coolscreen.de - Over 40 years of certified eZ Publish know-how: http://www.cjw-network.com
CJW Newsletter: http://projects.ez.no/cjw_newsletter - http://cjw-network.com/en/ez-publ...w-newsletter-multi-channel-marketing

Antoine W.

Tuesday 12 April 2005 5:56:15 am

Yes, sure ! Here is my code :

$class =& eZContentClass::fetch( OBJECTCLASSID );
$ContentObject =& $class->instantiate( $user->id(), $sectionID );

//----------------------------------------------------------
// Create a node for the object in the tree.
//----------------------------------------------------------
$nodeAssignment =& eZNodeAssignment::create( array(
   'contentobject_id' => $ContentObject->attribute( 'id' ),
   'contentobject_version' => 1,
   'parent_node' => $parentNodeID,
   'sort_field' => 2, //Published
   'sort_order' => 1, //Descending
   'is_main' => 1));
$nodeAssignment->store();

//----------------------------------------------------------
// Set a status for the content object version.
//----------------------------------------------------------
$ContentObjectVersion =& $ContentObject->version($ContentObject->attribute( 'current_version' ) );
$ContentObjectVersion->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
$ContentObjectVersion->store();

//----------------------------------------------------------
// Set attributes of the Page for the default language
//----------------------------------------------------------
$ContentObjectAttributes =& $ContentObjectVersion->contentObjectAttributes();
foreach ($ContentObjectAttributes as $id=>$Attribute)
{
   if($Attribute->attribute("contentclass_attribute_identifier")=="title")
   {
      $Attribute->setAttribute("data_text", "my title here");
      $Attribute->store();
   }
   if($Attribute->attribute("contentclass_attribute_identifier")=="body")
   {
      $Attribute->setAttribute("data_text", "body of my page");
      $Attribute->store();
   }
}

//----------------------------------------------------------
// Set attributes of the Page for another language (french)
//----------------------------------------------------------
foreach ($ContentObjectAttributes as $id=>$Attribute)
{
   $Attribute =& $attribute->translateTo( "fre-FR" );
   $Attribute->sync();
}
$contentObject->store();

$translatedAttributes =& $contentObjectVersion->contentObjectAttributes("fre-FR");
foreach ($translatedAttributes as $id=>$Attribute)
{
   if($Attribute->attribute("contentclass_attribute_identifier")=="title")
   {
      $Attribute->setAttribute("data_text", "mon titre ici");
      $Attribute->store();
   }
   if($Attribute->attribute("contentclass_attribute_identifier")=="body")
   {
      $Attribute->setAttribute("data_text", "le contenu de ma page");
      $Attribute->store();
   }
}
$ContentObject->store();

//You can do this for each language you want to translate your object.

//----------------------------------------------------------
// Now publish the object.
//----------------------------------------------------------
include_once ('lib/ezutils/classes/ezoperationhandler.php');
$operationResult = eZOperationHandler::execute(
   'content',
   'publish',
   array( 
      'object_id' => $ContentObject->attribute( 'id' ),
      'version'   => $ContentObject->attribute('current_version' ) 
   ) 
);

Ekkehard Dörre

Tuesday 12 April 2005 6:01:40 am

Many thanks, Antoine,

Greetings, ekke

http://www.coolscreen.de - Over 40 years of certified eZ Publish know-how: http://www.cjw-network.com
CJW Newsletter: http://projects.ez.no/cjw_newsletter - http://cjw-network.com/en/ez-publ...w-newsletter-multi-channel-marketing

Xavier Dutoit

Thursday 14 April 2005 6:08:07 am

Hi,

thanks Antoine, it solved a related problem (working with multilingual website).

X+

"Oh, Yeah !
Si je porte des chemises à fleurs,
C'est que j'suis en avance de deux ou trois longueurs,"

http://www.sydesy.com

Antoine W.

Monday 25 April 2005 5:44:42 am

About this script :
Do you know how I could set the publication date for imported objects ?

Maybe an attribute to set, or a parameter when calling this function :

$operationResult = eZOperationHandler::execute(
   'content',
   'publish',
   array( 
      'object_id' => $ContentObject->attribute( 'id' ),
      'version'   => $ContentObject->attribute('current_version' ) 
   ) 
);

I have searched on /ez/kernel/content/*.php but didnt find where is the publication code.
Thanks.

Juan Almonte

Monday 25 April 2005 3:14:01 pm

quiero saber

Juan Almonte
809-563-3232 x 222
809-547-3967

*- pike

Friday 30 May 2008 5:53:02 am

Hi

a bit late to reply :-) But ContentObjectAttribute::translateTo() is now //deprecated//:

http://pubsvn.ez.no/doxygen/trunk/html/classeZContentObjectAttribute.html#41a1ddc226225900ba0f0e071455109e

it doesn't say what the alternative is.
my guess is cloneContentObjectAttribute() should be used.

http://pubsvn.ez.no/doxygen/trunk/html/classeZContentObjectAttribute.html#a4ba03b157e8278e4ed18441fe8d8384

$2c,
*-pike

---------------
The class eZContentObjectTreeNode does.