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();

eZ debug

Timing: Jan 31 2025 00:39:00
Script start
Timing: Jan 31 2025 00:39:00
Module start 'content'
Timing: Jan 31 2025 00:39:00
Module end 'content'
Timing: Jan 31 2025 00:39:00
Script end

Main resources:

Total runtime0.1625 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0076 588.0625370.2734
Module start 'content' 0.00760.0146 958.33591,001.5938
Module end 'content' 0.02210.1403 1,959.92973,890.5391
Script end 0.1625  5,850.4688 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00462.8002200.0002
Check MTime0.00140.8893200.0001
Mysql Total
Database connection0.00100.619010.0010
Mysqli_queries0.074645.87831410.0005
Looping result0.00090.58181390.0000
Template Total0.139986.110.1399
Template load0.00080.512610.0008
Template processing0.139085.561810.1390
Override
Cache load0.00060.359410.0006
Sytem overhead
Fetch class attribute can translate value0.00181.091510.0018
XML
Image XML parsing0.00020.144110.0002
General
dbfile0.00704.3062200.0003
String conversion0.00000.003730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs