Fetching nodes in different translations

Author Message

Atle Pedersen

Friday 17 November 2006 8:12:33 am

On a site I'm working on I sometimes need to fetch a node (for use with node_view_gui) in a different language/translation than what is specified in the current siteaccess. Is there a way to set a variable (or add an argument to a fetch) that would make eZ do this?

What I would like to do ideally is equivalent to change translation mid-template. Sometimes it is usefull to fetch a node in a different language than the one specified for the siteaccess.

Claudia Kosny

Friday 17 November 2006 11:53:34 am

Hi Atle

This is actually a good question - I could not find a way to fetch a node in a specific language. Most likely you can somehow access the translations from the node object but I did not see a good way to do this, maybe someone else can help you there.

Here my ideas how to solve the problem:
Depending on what exactly you want, you could fetch the content attributes for the object that is encapsulated by the current node:

{def $attributes = fetch( 'content', 'contentobject_attributes',
                        hash( 'version', $node.object.current ,
                              'language_code', 'fre-FR') )}
 
{foreach $attributes as $attribute}
    {$attribute.content} <br />
{/foreach}

Alternatively you could write your own template operator, like this:

<?php
include_once('kernel/classes/ezcontentobjecttreenode.php');

class MyEZFunctions
{
   var $Operators;

   function MyEZFunctions()
   {
       $this->Operators = array('my_fetch_node_translation');
   }

   function &operatorList()
   {
       return $this->Operators;
   }

   function namedParameterPerOperator()
   {
       return true;
   }

   function namedParameterList()
   {
       return array('my_fetch_node_translation' => array('node_id' => array('type' => 'integer',
                                                                'required' => true,
                                                                'default' => '' ),
                                       'language_code' => array('type' => 'string',
                                                                'required' => false,
                                                                'default' => false )));
   }

   function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace,
                    &$currentNamespace, &$operatorValue, &$namedParameters )
   {
       switch ( $operatorName )
       {
           case 'my_fetch_node_translation':
               $operatorValue =& eZContentObjectTreeNode::fetch($namedParameters['node_id'],
                                                                $namedParameters['language_code']);
           break;
       }
   }
}
?>

Please note that the above code is an excerpt from one of my template operator classes, so there might some copy and paste errors.

If you should find a native EZ function that does this, please post it here.

Have a nice weekend

Claudia

Atle Pedersen

Monday 20 November 2006 7:00:02 am

Thank you Claudia, this was helpful. I feared I had to go the 'extension way' in order to do this, but wanted to ask first in case I had missed a simpler solution.

I've not found a native function that solves my problem. A couple of related posibilities are adding '(language)/eng-GB' at the end of a URL or using the 'content/versionview/1/4/eng-GB' with correct object and version information. But those didn't help in my case.

Atle

Atle Pedersen

Monday 20 November 2006 12:34:35 pm

Unfortunately, I still have problems fetching nodes in for a given language. Although the above method can fetch the node content for any given locale passed as a parameter, any fetch functions in the template for the fetched node will fetch new nodes in the default language.

The result is mixed language nodes in a way that's not very pretty.

In eZContentObjectTreeNode there is a setLanguage(...) function. Anyone know if it would be possible to use this on some main object causing it to return nodes in the chosen language for the rest of the current page? Is there another variable that could be set in order to achieve the required result?

Claudia Kosny

Monday 20 November 2006 2:38:52 pm

Yes, it gets complicated if you want to fetch more than just a single node. With some fetch functions you can specify the language you want, but this is not always possible.

Unfortunately I don't know whether it is possible to do what you want. Unless someone else has an idea, you might want to post an enhancement request.

Good luck

Claudia

Atle Pedersen

Thursday 23 November 2006 1:27:11 am

It looks like it is possible to do what I want. Making a template operator call the following code will actually work!

    function setLanguage($language_code){
        $ini =& eZINI::instance();
        $ini->BlockValues['RegionalSettings']['Locale']=$language_code;
        $ini->BlockValues['RegionalSettings']['ContentObjectLocale']=$language_code;
        $ini->BlockValues['RegionalSettings']['SiteLanguageList']=array($language_code);
        eZContentLanguage::clearPrioritizedLanguages();
        eZContentLanguage::prioritizedLanguages(array($language_code));
        return;
    }

Disclaimer:
I don't now how and why this works, only that it does work for version 3.8.6. Possibly it will break for other versions. Probably there are better ways to do this, but I have not had time to look into this further yet. I still decided to post it though, in case it would be helpful to you, Claudia, or to somebody else.

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 19 2025 00:07:51
Script start
Timing: Jan 19 2025 00:07:51
Module start 'layout'
Timing: Jan 19 2025 00:07:51
Module start 'content'
Timing: Jan 19 2025 00:07:53
Module end 'content'
Timing: Jan 19 2025 00:07:53
Script end

Main resources:

Total runtime1.3088 sec
Peak memory usage4,096.0000 KB
Database Queries66

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0070 588.0469152.6406
Module start 'layout' 0.00700.0033 740.687539.4766
Module start 'content' 0.01031.2972 780.1641552.0625
Module end 'content' 1.30750.0013 1,332.226620.1250
Script end 1.3088  1,352.3516 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.2368160.0002
Check MTime0.00130.0971160.0001
Mysql Total
Database connection0.00090.070210.0009
Mysqli_queries1.242494.9211660.0188
Looping result0.00070.0499640.0000
Template Total1.269397.020.6346
Template load0.00180.137120.0009
Template processing1.267596.840020.6337
Template load and register function0.00010.010910.0001
states
state_id_array0.00110.084710.0011
state_identifier_array0.00150.115320.0008
Override
Cache load0.00160.1196390.0000
Sytem overhead
Fetch class attribute can translate value0.00060.047420.0003
Fetch class attribute name0.00150.111270.0002
XML
Image XML parsing0.00210.157420.0010
class_abstraction
Instantiating content class attribute0.00000.0017100.0000
General
dbfile0.00330.2505160.0002
String conversion0.00000.000640.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
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
10content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 27
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs