GetText allows translating texts in themes and plugins, but in order to work right, you must create texts that translate well. We’ll show you frequent mistakes and how to correct them.
GetText is a GNU’s text localization package. It allows replacing text in one language with texts in another language.
We’ve already talked about using GetText to build multilingual themes. Now, we’ll talk about how to make the theme’s texts translatable, from the translator’s point of view.
Complete sentences only, please
Translators cannot accurately translate single words. They need to see the entire sentence in order to translate correctly.
For example, what does You must be mean?
In English, the word be has one meaning. But, in Spanish, be mean what you are (ser) or where you are (estar). There’s no way a translator can translate this correctly without seeing the entire sentence.
This is the code that’s responsible for the text:
<?php _e('You must be') ?> <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>"><?php _e('logged in') ?></a> <?php _e('to post a comment.') ?>
So, what do we do?
Using full sentences with placeholders
What we’ve actually done in this example is concatenate several parts into a sentence. The way we did it broke the sentence into different pieces, losing all meaning in the process.
Here is this same thing, built with a single sentence a placeholder:
<?php printf(__( 'You must be <a%s>logged in</a> to post a comment' ), ' href="'.get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode(get_permalink()) . '"') ?>
What we’ve done is:
- Create one large text that includes a placeholder.
- Use printf to replace the placeholder with an actual value.
The translator now needs to translate this sentence:
You must be logged in to post a comment
The translator can easily translate this entire sentence. No questions to ask and no problems translating!
Besides being easier to translate, we need to remember that in different languages words appear in different order. Supplying the complete sentence with placeholders will allow the translator to flip the order of words and produce grammatically correct (and sensible) transaltions.
Really advanced case (appears in every WP site)
Look at this PHP code. It appears in almost every WordPress site in the comments section.
<h3><?php comments_number(__('No Responses'), __('One Response'), __('% Responses') );?>
<?php _e('to') ?> “<?php the_title(); ?>”</h3>
The translator would have to translate the word to. But, it’s impossible without understanding how to is used in the sentence.
The word to is part of a sentence, which would be:
3 Responses to new widgets
Once the translator sees the entire sentence, it all becomes clear and easy to translate.
To turn this into a single sentence, we’ll use this PHP code:
<h3><?php printf(__('%s to %s'),
get_comments_number_ml(__('No Responses'), __('One Response'), __('% responses') ),
'“' . get_the_title() . '”')?></h3>
This code produces a full sentence to translate. Now, the translator can produce correct translation to your comments heading.
Note: we’ve replaced the standard WP get_comments_number with an identical function that returns the comment count as a value and doesn’t echo to the screen. You can download this PHP code from get_comments_number_ml.zip.
What about your themes?
Are your multilingual themes built for correct translation?
An easy way to test is to open your .po file in poedit and review the strings. If you’re seeing anything that isn’t clear by itself, look for it in the source and revise.