Jump to content

Localization doesn't work with files in subfolders?


abdus
 Share

Recommended Posts

I'm trying to build a multilanguage website with Turkish as main and English as alternative language. I replaced hardcoded strings in template files and used built-in translation tools to translate template files. I'm using the functions ProcessWire provides:

// using __()
<?= __("There are no products to display.") ?>
// or _x()
<html lang="<?= echo _x('en', 'HTML language code'); ?>">
// Both works when I use _main.php

When I disable auto including _main.php from config.php file, and move or rename _main.php to something else or use another file (other than  _main.php that comes with site-languages profile) for template specific markups, say markup/products.php like below, translations stop working.

// config.php, disabled auto append
// $config->appendTemplateFile = '_main.php';

// home.php
include_once("./_main.php");
wireIncludeFile("./_main.php");
// TRANSLATION WORKS

include_once("./_home.php");
// DOESNT WORK

wireIncludeFile("markup/_main.php");
include_once("markup/_main.php");
// DOESNT WORK

// NOTE: home.php and main.php has the same content, same permissions, same ownership

I debugged the issue, and the reason, as ridiculous as it sounds, originates from using file other than _main.php. I searched the forums, google, but this seems like a unique issue. Is there anything I'm missing and how can I fix this, because I can't think of anything else after hours lost trying to find the underlying cause?

Thanks in advance.

Link to comment
Share on other sites

If you go to the Setup / Languages and click on a language, are _main.php added to the Site Translation Files? Try adding _home.php too.

I refreshed the file list, and added translations for home.php, but nothing changes.

Language page

2ScRW7L.png

File translations page

U0rETjE.png

Translating markup/home.php

1DNkEwI.png

Below is my home template file and its markup file.

/* home.php */
<?php namespace ProcessWire;
...
..
.
include("markup/home.php");

/* markup/home.php */
<?php namespace ProcessWire;
?>
<!doctype html>
<html lang="<?php echo _x('en', 'HTML language code'); ?>">
...
..
.
</html>

// html output is wrong without _main.php
<!doctype html>
<html lang="en">

// correct with _main.php
<!doctype html>
<html lang="tr">

Link to comment
Share on other sites

It's fixed on 3.0 (branch "devns"). Not sure if Ryan backported the fix to PW 2.7.x.

Compiled template files are a 3.x feature, so this specific issue won't affect 2.7.x. I can't really tell from the screenshots which specific version of 3.x @abdus is using, but it sounds like updating the system to the latest version from GitHub just might do the trick.

Link to comment
Share on other sites

Compiled template files are a 3.x feature, so this specific issue won't affect 2.7.x. I can't really tell from the screenshots which specific version of 3.x @abdus is using, but it sounds like updating the system to the latest version from GitHub just might do the trick.

I'm using the latest version 3.0.7 from devns branch, and I solved it by disabling the compilation. As the purpose of the compilation is stated:

Enables ProcessWire to update your template file for namespace and apply any installed FileCompiler modules. 
Recommended unless it causes errors with your template file.

I don't need it anyway since I am already using the ProcessWire namespace in template files. 

However, I tested it once again by enabling compilation, but still the default value returns, not its translation.

Link to comment
Share on other sites

  • 1 month later...

Hi All

There seems to be another problem with translation. 

we add an include via wireRenderFile with the normal replacement string in access.inc like __('Direkt zur Navigation') and so on, 

$accessibility = wireRenderFile("includes/access.inc");

that can be properly translated in the language admin. But the output remains in the default language.

we then changed wireRenderFile to and used variables 

$accessibility = wireRenderFile("includes/access.inc", array('home' => $home, 'toNav' => $toNav, 'toContent' => $toContent, 'toHome' => $toHome));

This is in my opinion not really dynamic.

Does anyone now a better solution to this problem?

Your help is much appreciated

Link to comment
Share on other sites

  • 1 month later...

Hello for all.

I am also having same problem with multilanguage in PW 3.0.17.

Translation of static strings doesn't work if "view" file is in subfolder.

But one thing what I really don't like is template compilation - that gives me more problems in development than benefits.

Also when I do this:

$config->templateCompile = false;

or

$config->moduleCompile = false;

or switch off template compilation in template settings, I get this :

"Call to undefined function __()" or "Call to undefined function _x()"

Also there is one old topic about "PW slow in backend" and that is my problem after ~2.7 upgrade.

Because all of that probably I will back to PW 2.6 ... 2.7

System: localhost, win7 64b, wamp 3.0.0, apache 2.4.17, php 5.6.16

Regards.

Link to comment
Share on other sites

Hello OLSA,

I don't know exactly how the structure of your template folder is, but I had the same error message "Call to undefined function __()", when I tried to use an translatable string inside an php include file like this:

echo __("String to translate");

I solved this error by using the $this->_() wrap mentioned in the documentation:

echo $this->_("String to translate");

Maybe this will solve your problem.

Personally I never had problems with the template compilation, but If you have and want to disable the template compilation, you should be sure to add the ProcessWire namespace at the beginning of your template files. I wouldn't disable the module compilation, because most of the modules aren't yet compatible with PW3 without the module it.

If Wamp supports PHP7 you could try if it improves your back end performance.

Regards, Andreas

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

Sorry if i answer to and old post, but i have the same problem. I call some php files in a folder called /includes, from a template.

Well, all translations in the main are working properly, but all inside the /includes folder not.
I have to use a $this->_('translation') syntax in the sub-files  (in the main, instead, i use the regular expression), or i get an error (
I have no errors, but if i switch language, these translations don't change!

I call these sub-files in this way, in the template

region('content', files()->render('./includes/news-page.php', array(
	'page' => $page,
	'related' => $related,
)));

 

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...