How to make use of multiple language support in WordPress


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?

  1. That the theme is localized, o therwise there’s not much point in using the load_theme_textdomain functionality.
  2. That every translation string with the domain 'notesblog' is considered.
  3. That you still need a translation.

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 _().

Legal Disclaimer

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

Related Articles

1. When to Use Wordpress functions php
When to Use functions.php When, then, is it really a good idea to use functions.php? I have a rule for that too: only use functions. php when the added function...

2. Are you using WordPress as a CMS
WordPress as a CMS Using WordPress for things other than blogging is something that comes naturally to a lot of developers today, but not so much for the ge...

3. Things to Consider When Using WordPress as a CMS
Things to Consider When Using WordPress as a CMS So you’re considering using WordPress as a CMS for a project huh? Great, and probably a good choice too...

4. Trimming WordPress to the Essentials
Trimming WordPress to the Essentials Usually, when doing work for clients or other people within your organization, you’ll have to think a little bit diff...

5. Wordpress Static Pages and News Content
Static Pages and News Content I touched upon static Pages and categories as a news model previously. It is truly a great tool whenever you need to roll out a ty...

6. Making Widgets a Little More Dynamic
Putting Widgets to Good Use Widgets and widget areas are your friends when rolling out WordPress as a CMS. It is perhaps not as important for the small and stat...

7. Wordpress The Header and Footer Templates
The Job Board The first special project we’ll work on is a job board. You have probably seen this kind of site already, where people and companies can pos...

8. When using TDO Mini Forms to build this solution
Receiving the Jobs There are numerous ways to receive the actual content for the job listings. The easiest way would be to just have people e-mail listings to y...