I’m going through some legacy code and adding i18n functions throughout when I stumbled on a localisation snippet I found inside a pre_get_document_title
function: (simplified)
$title="Hello World";
__( $title, 'my_text_domain' );
This returns the correct, translated string. It was recently pointed out to me however, that this isn’t valid syntax as localisation functions should only receive a string; not a variable, nor a function return value. The recommended (and correct) syntax, as seen in numerous themes:
sprintf( __( '%s', 'my_text_domain' ), $title );
Having made this change, I find that all my titles are now still in English. i.e. nothing gets translated. All the language files are still there, all I did was modify the __() calls.
Is there something I missed?
1 Answer
The order of the functions being called is wrong.
With this code:
sprintf( __( '%s', 'my_text_domain' ), $title );
- You are trying to translate the string ‘%s’ in the domain ‘my_text_domain’.
- you are then replacing the translated string for ‘%s’ (which is ‘%s’), by the content of the variable
$title
. - Therefore your code is similar to
sprintf( '%s', $title);
What you should do:
__( sprintf( '%s', $title ), 'my_text_domain' );
sprintf()
will return your title__()
will translate the returned title
By the way, if you plan to use the title of the post only, you probably don’t need to use sprintf()
but instead:
__( $title, 'my_text_domain );