Translation key doesn't work after code change in Template file

Hello,

I have made some changes to the template file
plugins/oaiMetadataFormats/marcxml/record.tpl [OJS-Version: 2.4.6.0]

When I change the order of two template blocks, I get a strange effect:
a translation key from the second template block won’t be translated any more:

##submission.copyrightStatement##

OLD CODE:

<datafield tag="786" ind1="0" ind2=" ">
    <subfield code="n">{$journal->getTitle($journal->getPrimaryLocale())|escape}; {$issue->getIssueIdentification()|escape}</subfield>
</datafield>

<datafield tag="540" ind1=" " ind2=" ">
    <subfield code="a">{translate key="submission.copyrightStatement" copyrightYear=$article->getCopyrightYear() copyrightHolder=$article->getCopyrightHolder($journal->getPrimaryLocale())|escape}</subfield>
</datafield>

NEW CODE:

<datafield tag="540" ind1=" " ind2=" ">
        <subfield code="a">{translate key="submission.copyrightStatement" copyrightYear=$article->getCopyrightYear() copyrightHolder=$article->getCopyrightHolder($journal->getPrimaryLocale())|escape}</subfield>
</datafield>
    
<datafield tag="786" ind1="0" ind2=" ">
    <subfield code="n">{$journal->getTitle($journal->getPrimaryLocale())|escape}; {$issue->getIssueIdentification()|escape}</subfield>
</datafield>

I’ve figured out that the function call $issue->getIssueIdentification() from the 786 block has to be called before the translate command, then the key will be translated correctly again:

WORKAROUND

<!--{$issue->getIssueIdentification()}-->

<datafield tag="540" ind1=" " ind2=" ">
    <subfield code="a">{translate key="submission.copyrightStatement" copyrightYear=$article->getCopyrightYear() copyrightHolder=$article->getCopyrightHolder($journal->getPrimaryLocale())|escape}</subfield>
</datafield>

What is the reason for that behavior?
Is there a better workaround? :wink:

Hi @uwords,

This is happening because the locale file that have this locale key is not being loaded until the $issue->getIssueIdentification() call.

The locale file that needs to be loaded is a common one, and generally this kind of file is loaded by the handler. OAI requests are handled by the OAIHandler, which is the one that should be calling $this->setupTemplate() so all basic locale files are loaded. But OAI requests doesn’t present a whole web page, so it’s generally not worth it to load all locale files because they are not going to be used.

The correct solution in your case would be to load the locale file on the plugins/oaiMetadataFormats/marc/OAIMetadataFormat_MARC.inc.php file, in the toXml method. You can add this line before the $templateMgr->fetch() call:

AppLocale::requireComponents(array(LOCALE_COMPONENT_APPLICATION_COMMON));

Then you are free to use that locale key anywhere inside the template file, without the need to call any other method there.

Cheers,
Bruno

Hello Bruno,

yes, it works now.
By diving into the abyss of the ojs source code I also found that line with LOCALE_COMPONENT_APPLICATION_COMMON, but only your explanation made it clear to me, why and where it should be used.

Many thanks for your fast reply!

Greetings from
Ursula

Glad I could help. Once you understand the way OJS handle the requests, it is much easier to follow the code and see what’s happening. :smile:

Cheers,
Bruno