How to make string in variable translatable?

mattyoung - February 17, 2009 - 00:55

I have some strings read from files. They are static inside file system but not defined in php. These strings are presented to the users and they should be translated. But potx complain t() cannot be called with variable as first argument. Basically I have this:

<?php
$strings
= read_from_file();
t($strings[0]);
t($strings[1]);
// more or less like this
?>

What to do?

I'm not sure about this, but

grobemo - February 17, 2009 - 03:08

I'm not sure about this, but try:

<?php
$strings
= read_from_file();
foreach (
$strings as &$string) {
 
$string = t("$string");
}
?>

The double quotes tell PHP to parse the variable in the string. So t() should receive a string as its first argument.

www.divinesents

vishalduvedi - February 17, 2009 - 04:10

fore more

Variables become text at run

cog.rusty - February 17, 2009 - 04:30

Variables become text at run time, probably different text depending on the run time context. For example imagine t($node_title).

Obviously potx can't extract them because it doesn't really run the code.

Ignore potx's complaints and try adding the strings somewhere in dummy code with t(), where potx can see them and extract them. That should work at run time.

>Ignore potx's complaints

mattyoung - February 17, 2009 - 05:31

>Ignore potx's complaints and try adding the strings somewhere in dummy code with t(), where potx can see them and extract them. That should work at run time.

Yep, potx still complain about t("$string");. It's so smart you can't trick it :). Anyway, I guess dummy code is the only way. I wish there is a hook_potx() that potx calls for an array of strings to be part of the extract and a special "@potx don't mind me here" special comment to silence the warnings. I'll go over to potx page and ask for this feature.

The developer of potx tells

mattyoung - February 17, 2009 - 19:36

The developer of potx tells me t() is not designed to work with variable. t() has this in its comment:

* Because t() is designed for handling code-based strings, in almost all
* cases, the actual string and not a variable must be passed through t().

I don't know what it means. Looking at the code, it really doesn't matter how the string is defined. But anyway, I guess passing non-literal string to t() is not good.

They tell me to use the i18nstrings module in the i18n module package. But I prefer not to make my module depend on another non-core module.

 
 

Drupal is a registered trademark of Dries Buytaert.