Make Your WordPress Theme Ready for Translation

If you want your WordPress themes to be used by more people, you can translate them to other languages, or ask your theme user to do this for you. But first of all, you need to make your theme texts translatable.
To do this, you need know how WordPress do the translation. WordPress uses GNU gettext for localization. With this framework, you can mark your theme texts for translation. You can use the following two ways to mark texts.
- __($text) : this means $text need to be translated, and returns the result.
- _e($text) : this means $text need to be translated, and prints out the result to the webpage, equal to echo __($text).
Let’s see some examples, the first one is the easiest one, when you have no posts to display, we will show a message: ‘Not Found’, here is the code:
<h2>Not Found</h2>
Because this message will directly print to the screen, we use _e():
<h2><?php _e(‘Not Found’) ?></h2>
This is very easy, isn’t it. The second example is about the comment number, here is the code:
<?php comments_number(‘No Responses’, ‘One Response’, ‘% Responses’ );?>
The translations are not directly print out, but to be used for the function. So we are using __() to do this:
<?php comments_number(__(‘No Responses’), __(‘One Response’), __(‘% Responses’) );?>
There is one more situation, which is you need to translate a sentence with parameter. Here is one example:
by <?php the_author(); ?>
You can’t only translate ‘by’, because in some languages, you need translate the whole sentence so that it will make sense. Here is how to do it:
<?php printf(__(‘by %s’), the_author()); ?>
We use PHP function printf() to insert the_auther() to __(‘by %s’), so that you can translate the whole sentence.
Now you know how to make your theme translatable, next time I will write about how to translate your theme.
.
.





Pingback: Tutorials for Translating WordPress Themes | Zack Live - WordPress Themes, Web Designer and Design Inspiration