Just like you can get WordPress in your language thanks to language files, you can do the same for your theme, and plugins as well for that matter. And just like with WordPress, you need to provide a .mo file that is tailored for your theme, which in turn is created from a POT file via the .po file. It is all a pretty messy business actually, since a lot of the tools used to generate these files are pretty clunky to use, but using the software is worth it. First of all, you need to understand why a certain language file gets loaded if it is available to Word- Press. There is (usually) no menu setting for what language you want a specific theme of plugin to be in; rather, WordPress looks for the one defined in the installation, which is to say the one you added to WPLANG in wp-config.php. So that means that if your Word- Press install is in German, then a localized theme containing German language files will use those if possible.
A little history lesson is in order before you get your theme ready for internationalization efforts. First of all, there’s the POT (Portable Object Template) file. This is basically a file created by a program by analyzing your theme or plugin. The software of your choice (see the “Working With Language Files” section for links) will parse your theme and build a POT file containing all the words and phrases you have marked for translation. Step 2 would be to create a .po (Portable Object) file from your POT file. What this does is save the original language (usually English), and the translated language in one file. This is where you do your translation.
The third and final step in the translation process would be the .mo (Machine Object) file. This file is created from the .po file, so that it becomes machine-readable, and that makes the translation a lot faster to read and hence output. The .mo file is the file used by WordPress and your theme. So that’s that! But how does the software that generates the POT file know what should be made available for translation? This is where you come in, as the theme (or plugin) author. You need to mark the parts of your theme that should be up for translation, and you do that by wrapping the word or phrase in a PHP snippet, and then applying it to a domain. The domain is really just a translation set, and that in turn will be defined in functions.php. Say you want to make the text “Hello you!” available for translation. That would look like this:
<?php _e('Hello you!', 'mydomain'); ?>
That would output “Hello you!” as if written in plain text, but if there is a translation available and mydomain is defined in functions.php, you’ll get that instead. If not, you’ll just get “Hello you!” in plain old English. You can also write it like this:
<?php __('Hello you!', 'mydomain'); ?>
Same result, just two underscores before the opening parenthesis rather than one and the e. Call up the trusty Notes Blog Core theme for an example. In the index.php file you’ll find the following result output (within PHP code tags, so skip them) whenever a search result is called for:
_e('Your search result:', 'notesblog');
That would output “Your search result:” if there was a defined domain called notesblog. Sometimes you want these translations within tags. This is how you’ve done it with the the_content() template tag, to make the “read more” link output by the <!-more-> code snippet in posts available for translation:
<?php the_content(__('Read more', 'notesblog')); ?>
A double underscore, and then the translation, does the same as _e(). So why the double underscore method? Well, the _e() is an echo statement, while the double underscore, or __(), is a return statement. Doing echo within PHP functions can create some weird results. So how, then, do you tell the theme to use the translation file in the first place? Just dropping translation file in the theme won’t do it; you need to declare that a specific text domain should be used (“notesblog” in the examples earlier). Just add this in the functions.php file:
load_theme_textdomain('notesblog');
Within the PHP tags of course, just like almost everything else in the functions.php file. Now, what does this tell you?
The actual translation should be in a .mo file, and that in turn comes from a .po file, which you created from a POT file. And the POT file is generated by your chosen software (again, see the “Working with Language Files” section for links and suggestions), which in turn has parsed your theme looking for your translation strings, the __() and _().
Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
This article was sent to us by:
Ron Dornes at
05192010
1. When to Use Wordpress functions php
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved.