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.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 19:24:33
Script start
Timing: Jan 18 2025 19:24:33
Module start 'layout'
Timing: Jan 18 2025 19:24:33
Module start 'content'
Timing: Jan 18 2025 19:24:34
Module end 'content'
Timing: Jan 18 2025 19:24:34
Script end

Main resources:

Total runtime1.0195 sec
Peak memory usage4,096.0000 KB
Database Queries86

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0071 589.3438152.6250
Module start 'layout' 0.00710.0032 741.968839.4453
Module start 'content' 0.01031.0078 781.4141801.6563
Module end 'content' 1.01810.0014 1,583.070328.1641
Script end 1.0195  1,611.2344 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00350.3403160.0002
Check MTime0.00150.1439160.0001
Mysql Total
Database connection0.00100.099510.0010
Mysqli_queries0.930191.2297860.0108
Looping result0.00090.0883840.0000
Template Total0.984696.620.4923
Template load0.00220.213020.0011
Template processing0.982596.362520.4912
Template load and register function0.00010.011810.0001
states
state_id_array0.00150.151810.0015
state_identifier_array0.00180.175720.0009
Override
Cache load0.00200.1942610.0000
Sytem overhead
Fetch class attribute can translate value0.00090.086260.0001
Fetch class attribute name0.00190.1861130.0001
XML
Image XML parsing0.00220.216460.0004
class_abstraction
Instantiating content class attribute0.00000.0036140.0000
General
dbfile0.00130.1246290.0000
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
10content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
17content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
9content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 46
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs