note 93141 added to function.gettext

From: Date: Tue, 25 Aug 2009 13:46:54 +0000
Subject: note 93141 added to function.gettext
Groups: php.notes 
Request: Send a blank email to php-notes+get-159868@lists.php.net to get a copy of this message
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. ---- Server IP: 92.48.74.199 Probable Submitter: 89.145.198.195 ---- Manual Page -- http://www.php.net/manual/en/function.gettext.php Edit -- https://master.php.net/note/edit/93141 Del: integrated -- https://master.php.net/note/delete/93141/integrated Del: useless -- https://master.php.net/note/delete/93141/useless Del: bad code -- https://master.php.net/note/delete/93141/bad+code Del: spam -- https://master.php.net/note/delete/93141/spam Del: non-english -- https://master.php.net/note/delete/93141/non-english Del: in docs -- https://master.php.net/note/delete/93141/in+docs Del: other reasons-- https://master.php.net/note/delete/93141 Reject -- https://master.php.net/note/reject/93141 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159868) next »