note 93141 deleted from function.gettext by cmb
| From: | cmb@php.net | Date: | Mon, 31 Aug 2015 19:17:59 +0000 |
| Subject: | note 93141 deleted from function.gettext by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203536@lists.php.net to get a copy of this message | ||
Note Submitter: nonproffessional at clockworkgeek dot com
----
If you want to have variables in your text to be translated the usual way would be with string
formatting like so:
<?php
printf(gettext("Hello %s!"), $world);
?>
This works because the string substitution is made after gettext performs. The human translator
needs to be aware of what %s represents, when there are many variables in a line it can get
confusing.
We can evaluate the string as PHP code instead of using string formatting:
<?php
eval('echo "' . addslashes(gettext('Hello $world!')) .
'";');
?>
Notice the text to be translated is in single quotes so $world does not get evaluated too soon.
Quotes in the translated text are escaped to prevent bad parsing.
Now the human translator will see $world which is more meaningful than "%s". He must be
careful not to translate the variable name but leave it intact.
It is also easier to change the order in which variables are used instead of printf's arcane
"%2$s". For advanced use the translator could take advantage of functions like ucwords().
The downside of this method is any PHP code can be evaluated which might be damaging. This is
equivalent to an injection attack. Make sure to vet all translated text to prevent other code
sneaking in.