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.www.ads-software.com 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.
- The topic ‘Variables as args in translation function’ is closed to new replies.