Forums / Developer / Fetching nodes in different translations

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.