Support » Developing with WordPress » Variables as args in translation function

  • Officially you should not use variables in translation functions like below:

    $string = 'An example string';
    echo __( $string, 'text-domain' );

    However I think an exception can be made for the plugin name (basically any main plugin file header entry), because the plugin name string is always added to the .pot file (wp i18n make-pot) or included in the translation strings on translate.wordpress.org from the Plugin Name header in the main plugin file.

    Is my thinking correct ?

    As an example:

    $default_headers = array(
            'Name'        => 'Plugin Name',
            'PluginURI'   => 'Plugin URI',
            'Version'     => 'Version',
            'Description' => 'Description',
     );
     
     $plugin_data = get_file_data( __FILE__, $default_headers );
    
     $plugin_name = $plugin_data['Name'];
    
     echo sprintf( __( 'Welcome to the %s plugin !', 'text-domain' ), __( $plugin_name, 'text-domain' ) );

    Note1: I’ve intentionally left out loading the plugin translation.
    Note2: For simplicity I’ve not used any actions/filters.
    Note3: Using the get_plugin_data() function will by default attempt to load the plugin translation as well as translate any header data.

    • This topic was modified 7 months, 1 week ago by nlpro.
    • This topic was modified 7 months, 1 week ago by nlpro.
    • This topic was modified 7 months, 1 week ago by nlpro.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You can add variables to translated strings by using sprintf(), but you can’t translate those variables.

    In your example, you have this code

    __( $plugin_name, 'text-domain' )

    There’s no point wrapping this variable in a translation function, as you can’t translate variables. This is for technical reasons, as variables are not processed into their values when translation files are generated.

    If the variable is from a source that has already been translated, then you can just use it without the translation function:

    echo sprintf( __( 'Welcome to the %s plugin !', 'text-domain' ), $plugin_name );

    Thread Starter nlpro

    (@nlpro)

    Hi Jacob,

    Thank you for your feedback.

    Though I think you might be missing the point of my post.
    You are basically saying it won’t work.

    So I created a full fledged single file plugin. Tested it and … it works.

    So there is no plugin name literal string used by a translation function in the code of the plugin.
    However the WP-CLI command “wp i18n make-pot” still adds the plugin name (and the other header string values) as a translatable string. And that’s why it works.

    So IMHO there is an exception to the rule for all strings in the header of the main plugin file.

    Hope this clarifies things a bit.

    Additionally: When creating a .pot file (or when a plugin translation is added to translate.wordpress.org) the header strings from the main plugin file are always automatically added to the translation.

    • This reply was modified 7 months, 1 week ago by nlpro.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Variables as args in translation function’ is closed to new replies.