I really often see a common mistake made when translating strings in WordPress so I thought I’d write a blog post in order to shed more light on the issue.
But first, here’s a quick refresher on how to internationalize code in WordPress:
<?php _e( 'Welcome to my blog!', 'my-plugin-text-domain' ); ?>
For dynamic strings, it’d be something like this:
<?php printf( __( 'Welcome to my blog, %s!', 'my-plugin-text-domain' ), $name ); ?>
But what about when you have multiple variables to use in your string? A common mistake is to do something like this:
<?php printf( __( 'Welcome to my blog, %s! Today\'s date is %s.', 'my-plugin-text-domain' ), $name, $date ); ?>
The issue with this is that you’re requiring the person’s name to always come before the date. If for internationalization reasons it needs to be in a different order, then it won’t work. You’ll end up with something like
Today's date is Alex. Welcome to my blog, November 2nd!.
The solution is to use standard
sprintf() argument swapping parameters:
<?php printf( __( 'Welcome to my blog, %1$s! Today\'s date is %2$s.', 'my-plugin-text-domain' ), $name, $date ); ?>
Now translators are free to re-order the string to whatever makes the most sense for the language in question without having to worry the order of the variables.
For a more in-depth review of this, check out the WordPress Codex where many real world examples can be found.