Jump to content

Code Internationalization (i18n) doesn´t work within included php files


Recommended Posts

Hey community,

first of all: I love this CMS! But unfortunately my Code Internationalization (i18n) doesn´t work within included php files:

I´m using the module Pro Fields Page Table, the regular Languages Support Module and no template caching. The website has 2 Languages (DE as default, EN as second language)

Problem:

Using

<?php echo(__('Test')); ?>

within /site/templates/home.php works without any problems and gets translated correctly
However using

<?php echo($this->_('Headline'));?>

within /site/templates/components/component.php does not work.

Note: using

<?php echo(__('Headline'));?>

in .../component.php throws an error and therefore cannot be used.

 

Example:

/site/templates/ce-includer.php (Template with the dynamic text-field 'filepath'):

include('components/'.$page->filepath);

this correclty includes the file "/site/templates/components/home-content.php"

Code in home-content.php:

<h1><?php echo($this->_('Headline'));?></h1>


Output for both pages (DE and EN version - the string was translated to "Headline DE" in german and "Headline EN" in english):

<h1>Headline</h1>

Does anyone know how to use Code Internationalization in such a szenario?

All the best and keep up the great work!
Daniel

Link to comment
Share on other sites

3 hours ago, daniel_puehringer said:

Hey community,

first of all: I love this CMS!

Welcome to ProcessWire and the forums @daniel_puehringer.

3 hours ago, daniel_puehringer said:
<?php echo($this->_('Headline'));?>

$this__() is only for use inside a class. 

3 hours ago, daniel_puehringer said:

Note: using

<?php echo(__('Headline'));?>

in .../component.php throws an error and therefore cannot be used.

Have you tried echo __('Headline')? According to the docs, the (__('Headine')) syntax is for use with the placeholder syntax (printf() or sprintf()). Not sure why it worked inside your home.php though. Maybe someone more knowledgeable will provide a better answer. Meanwhile, just in case you missed them, here are the internationalization docs.

https://processwire.com/docs/multi-language-support/code-i18n/

Edited by kongondo
Link to comment
Share on other sites

Hi @kongondo

Thanks for your reply ?

On 11/25/2021 at 6:35 PM, kongondo said:

Have you tried echo __('Headline')?

Sadly this did not work either, however I have now found an error message which sounds promising:

2021-11-29 10:20:35	guest	https://example.com/processwire/contentelements/ce-33/	Fatal Error:  Uncaught Error: Call to undefined function __() in
/var/www/html/site/templates/components/home-content.php:75 Stack trace: 
#0 /var/www/html/site/templates/ce-includer.php(10): include() 
#1 /var/www/html/wire/core/TemplateFile.php(327): require('/var/www/html/s...') 
#2 /var/www/html/wire/core/Wire.php(414): ProcessWire\TemplateFile->___render() 
#3 /var/www/html/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod('___render', Array) 
#4 /var/www/html/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks(Object(ProcessWire\TemplateFile), 'render', Array) 
#5 /var/www/html/wire/modules/PageRender.module(554): ProcessWire\Wire->__call('render', Array) 
#6 /var/www/html/wire/core/Wire.php(417): ProcessWire\PageRender->___renderPage(Object(ProcessWire\HookEvent)) 
#7 /var/www/html/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod('___renderPage', Array) 
#8 /var/www/html/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageRender), 'renderPage', Array) 
#9 /var/www/html/wir (line 75 of /var/www/html/site/templates/components/home-content.php)

It seems to me as if there was a problem with the scope:, however I don´t know what to do with this information since I have little experience with the Wire Objecs and Hooks.

Maybe someone else does ?

Greetings,
Daniel
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...