I’m writing a wordpress plugin and planning it’s drupal implementation i’m wrapping wordpress functions into adapters. So i wrote an adapter for the __() which is simply
class Ai1ec_Wordpress_Template_Adapter implements Ai1ec_Template_Adapter {
...
public function translate( $text ) {
return __( $text, AI1EC_PLUGIN_NAME );
}
I’m not a big expert of gettext and a collegue wrote me:
will fail when the code is parsed by xgettext to generate the .pot
file. WP i18n functions such as __() require that a string literal is
passed as the first argument, never a variable. And IIRC, the same
holds true for Drupal’s t() function.
I’ve read the codex entries
http://codex.wordpress.org/I18n_for_WordPress_Developers#Placeholders
http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/t/7
And couldn’t find out something exactly related to this. Can soemone explain me exactly why this wouldn’t work and how could i write something which could be compatible with WordPress and Drupal?
In general your colleague is correct, if you pass variables to translation functions they will not get translated unless the same string is passed in as a literal elsewhere. This is because generating the translation template does not run the code, it searches over it for particular function names. The translation methods have 2 purposes, one is to translate the parameter, the second is to identify to the translation template generation program which strings should be included in the translation template file.
You need to tell the translation template generation code (usually the xgettext) program that the parameters to your function are translatable strings. With xgettext this can be done with the -k parameter. WordPress may already have its own wrapper to xgettext that you can use.
The drupal document you linked explicitly says you should not pass variables to t() unless you are sure that the text is passed as a literal elsewhere.