Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

162 PART II • Designing and Developing WordPress Themes


MULTIPLE LANGUAGE SUPPORT


Just as you can get WordPress in your language of choice thanks to language files, you can do
the same for your theme, and plugins as well for that matter. Just as with WordPress, you need
to provide an .mo file that is tailored to your theme, which in turn is created from a POT
(portable object template) file via the .po file. It is all a pretty messy business because a lot of
the tools used to generate these files are pretty clunky to use, but using the software is
worth it.

First you need to understand why a certain language file gets loaded if it is available to
WordPress. There is (usually) no menu setting for what language you want a specific theme or
plugin to be in; rather, WordPress looks for the setting defined in the installation, which is the
one you added to WPLANG in wp-config.php (see Chapter 1, “The Anatomy of a WordPress
Install,” for more detail). This means that if your WordPress install is in German, a translated
theme containing German language files will use those if possible.

The translation process consists of three steps. First you need to create a POT file. The
software of your choice (see the following “Working with Language Files” subsection for
related links) will parse your theme and build a POT file containing all the words and phrases
you have marked for translation.

Step two is to create a .po (portable object) file from your POT file. This process saves the
original language (usually English) and the translated language in one file. This is where the
translation actually occurs, the actual localization.

The third step in the translation process is to create the .mo (machine object) file, using
appropriate software. 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 thus output. The .mo file is the file used
by WordPress and your theme.

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 have to mark
the parts of your theme that should be up for translation 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, using two underscores before the opening parenthesis rather
than one and the e:

<?php echo __( 'Hello you!', 'mydomain' ); ?>
Free download pdf