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

(avery) #1

CHAPTER 6 • Advanced Theme Usage 163


Call up the trusty Twenty Twelve theme, a former default theme, for an example. In the index.
php file, you’ll find the following result outputted whenever a page isn’t found:



That would output “Nothing Found” if there was a defined domain called twentytwelve. If
there was a translation for this particular string, it would output said translation instead.


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 outputted by the <!-more->
code snippet in posts available for translation:


<?php the_content( __( 'Read more', 'twentytwelve' ) ); ?>


A double underscore and then the translation. So why the double underscore method? Well,
the _e() is an echo, and the double underscore, or __(), is a return. Doing echo within
PHP functions can create some weird results because it prints out the content, whereas
return saves it for future use.


So how, then, do you tell the theme to use the translation file in the first place? Just dropping
the translation file in the theme won’t do it; you need to declare that a specific text domain
should be used (twentytwelve in the earlier examples). Just add this line in the functions.
php file (within the PHP tags, of course, just like almost everything else in the functions.php
file):


load_theme_textdomain( 'twentytwelve' );


This tells you


◾ That the theme is internationalized; otherwise, there’s not much point in using the
load_theme_textdomain functionality.
◾ That every translation string with the domain 'twentytwelve' is considered.
◾ That you still need a translation.

The actual translation should be in an .mo file, and that in turn comes from a .po file, which
you created from a POT file, as mentioned earlier. And the POT file is generated by your
chosen software (again, see the following subsection for links and suggestions), which in turn
has parsed your theme looking for your translation strings, the _() and ().


WORKING WITH LANGUAGE FILES


There are several ways to work with the portable language files. One of the most popular
programs available is Poedit (www.poedit.net), which is available across platforms. You
can easily open and edit available .po files in Poedit and then save them as .mo files to be used
with your theme or plugin.

Free download pdf